aboutsummaryrefslogtreecommitdiffstats
path: root/src/image.js
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2014-02-05 00:06:36 +0100
committerwout <wout@impinc.co.uk>2014-02-05 00:06:36 +0100
commit9995944cb253ba93441e2d65cef415daa3541a52 (patch)
tree9d69d943841bec9a25039ee417d58a3125249723 /src/image.js
parent43fd91ccce8f9bce60ed7c566e32152be42f9774 (diff)
downloadsvg.js-9995944cb253ba93441e2d65cef415daa3541a52.tar.gz
svg.js-9995944cb253ba93441e2d65cef415daa3541a52.zip
Added loader on SVG.Image and included svg.pattern.js to the core
Diffstat (limited to 'src/image.js')
-rwxr-xr-xsrc/image.js37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/image.js b/src/image.js
index 24333c7..60d57b1 100755
--- a/src/image.js
+++ b/src/image.js
@@ -9,7 +9,39 @@ SVG.Image = SVG.invent({
, extend: {
// (re)load image
load: function(url) {
- return (url ? this.attr('href', (this.src = url), SVG.xlink) : this)
+ if (!url) return this
+
+ var self = this
+ , img = document.createElement('img')
+
+ /* preload image */
+ img.onload = function() {
+ var p = self.doc(SVG.Pattern)
+
+ /* ensure image size */
+ if (self.width() == 0 && self.height() == 0)
+ self.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, {
+ width: img.width
+ , height: img.height
+ , ratio: img.width / img.height
+ , url: url
+ })
+ }
+
+ return this.attr('href', (img.src = this.src = url), SVG.xlink)
+ }
+ // Add loade callback
+ , loaded: function(loaded) {
+ this._loaded = loaded
+ return this
}
}
@@ -17,8 +49,7 @@ SVG.Image = SVG.invent({
, construct: {
// Create image element, load image and set its size
image: function(source, width, height) {
- width = width != null ? width : 100
- return this.put(new SVG.Image().load(source).size(width, height != null ? height : width))
+ return this.put(new SVG.Image).load(source).size(width || 0, height || width || 0)
}
}
}) \ No newline at end of file