summaryrefslogtreecommitdiffstats
path: root/src/Image.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-25 23:26:38 +0200
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-10-25 23:26:38 +0200
commit464af8b747389b7fdb569a933591c863b9be0f6b (patch)
treea23da0d70a26c142616207b0a0a489affd2f3ac6 /src/Image.js
parentf46aedf58fbc93483cb21017ffed10e439830108 (diff)
downloadsvg.js-464af8b747389b7fdb569a933591c863b9be0f6b.tar.gz
svg.js-464af8b747389b7fdb569a933591c863b9be0f6b.zip
Rename files so that they reflect their exported classes (see next commit)
Diffstat (limited to 'src/Image.js')
-rw-r--r--src/Image.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Image.js b/src/Image.js
new file mode 100644
index 0000000..f9395eb
--- /dev/null
+++ b/src/Image.js
@@ -0,0 +1,57 @@
+SVG.Image = SVG.invent({
+ // Initialize node
+ create: 'image',
+
+ // Inherit from
+ inherit: SVG.Shape,
+
+ // Add class methods
+ extend: {
+ // (re)load image
+ load: function (url, callback) {
+ if (!url) return this
+
+ var img = new window.Image()
+
+ SVG.on(img, 'load', function (e) {
+ var p = this.parent(SVG.Pattern)
+
+ // ensure image size
+ if (this.width() === 0 && this.height() === 0) {
+ this.size(img.width, img.height)
+ }
+
+ 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
+ })
+ }
+ }, this)
+
+ SVG.on(img, 'load error', function () {
+ // dont forget to unbind memory leaking events
+ SVG.off(img)
+ })
+
+ 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, callback) {
+ return this.put(new SVG.Image()).size(0, 0).load(source, callback)
+ }
+ }
+})