aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-19 16:41:01 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-19 16:41:01 +0100
commit60d3ad099f3ee1a39b401d12405dba9e4d7e5da2 (patch)
treee4feaf91d7c2d0c8a9c7f174ad4908f3b8b6f1cc /src
parentdc846f3eeadf2aa8aa84fe2339b7e647937715ce (diff)
downloadsvg.js-60d3ad099f3ee1a39b401d12405dba9e4d7e5da2.tar.gz
svg.js-60d3ad099f3ee1a39b401d12405dba9e4d7e5da2.zip
removed `loaded()` and `error()`/`changed load()`
SVG.Image is now constructed with: - container.image(src, callback) - new SVG.Image().load(src, callback)
Diffstat (limited to 'src')
-rw-r--r--src/attr.js2
-rw-r--r--src/image.js60
2 files changed, 22 insertions, 40 deletions
diff --git a/src/attr.js b/src/attr.js
index c88d34c..bc09fe3 100644
--- a/src/attr.js
+++ b/src/attr.js
@@ -32,7 +32,7 @@ SVG.extend(SVG.Element, {
// convert image fill and stroke to patterns
if (a == 'fill' || a == 'stroke') {
if (SVG.regex.isImage.test(v))
- v = this.doc().defs().image(v, 0, 0)
+ v = this.doc().defs().image(v)
if (v instanceof SVG.Image)
v = this.doc().defs().pattern(0, 0, function() {
diff --git a/src/image.js b/src/image.js
index d6101f3..84063de 100644
--- a/src/image.js
+++ b/src/image.js
@@ -8,62 +8,44 @@ SVG.Image = SVG.invent({
// Add class methods
, extend: {
// (re)load image
- load: function(url) {
+ load: function(url, callback) {
if (!url) return this
- var self = this
- , img = new window.Image()
-
- // preload image
- img.onload = function() {
- var p = self.parent(SVG.Pattern)
+ var img = new window.Image()
- if(p === null) return
+ SVG.on(img, 'load', function(e) {
+ var p = this.parent()
+
// ensure image size
- if (self.width() == 0 && self.height() == 0)
- self.size(img.width, img.height)
+ if (this.width() == 0 && this.height() == 0)
+ this.size(img.width, img.height)
- // ensure pattern size if not set
- if (p && p.width() == 0 && p.height() == 0)
- p.size(self.width(), self.height())
-
- // callback
- if (typeof self._loaded === 'function')
- self._loaded.call(self, {
+ if(p instanceof SVG.Pattern) {
+ // ensure pattern size if not set
+ if (p.width() == 0 && p.height() == 0)
+ p.size(this.width(), this.height())
+ }
+
+ if(typeof callback == 'function') {
+ callback.call(this, {
width: img.width
, height: img.height
, ratio: img.width / img.height
, url: url
})
- }
-
- img.onerror = function(e){
- if (typeof self._error === 'function'){
- self._error.call(self, e)
}
- }
+ }, this)
- return this.attr('href', (img.src = this.src = url), SVG.xlink)
- }
- // Add loaded callback
- , loaded: function(loaded) {
- this._loaded = loaded
- return this
- }
-
- , error: function(error) {
- this._error = error
- return this
+ return this.attr('href', (img.src = url), SVG.xlink)
}
}
-
+
// Add parent method
, construct: {
// create image element, load image and set its size
- image: function(source, width, height) {
- return this.put(new SVG.Image).load(source).size(width || 0, height || width || 0)
+ image: function(source, callback) {
+ return this.put(new SVG.Image).size(0, 0).load(source, callback)
}
}
-
-}) \ No newline at end of file
+})