diff options
author | wout <wout@impinc.co.uk> | 2013-02-20 19:10:03 +0100 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2013-02-20 19:10:03 +0100 |
commit | ae878fd63077d4c95f2de9053a6f0951b55239eb (patch) | |
tree | 5dba7bd42a8b5702faeb6cc12d2b4d4099841e51 /src/fx.js | |
parent | 5020240e4029a61a9620f21d7be4d9764e7723d1 (diff) | |
download | svg.js-ae878fd63077d4c95f2de9053a6f0951b55239eb.tar.gz svg.js-ae878fd63077d4c95f2de9053a6f0951b55239eb.zip |
Created separate classes for SVG.ViewBox anSVG.BBox0.6
Diffstat (limited to 'src/fx.js')
-rw-r--r-- | src/fx.js | 130 |
1 files changed, 65 insertions, 65 deletions
@@ -1,44 +1,44 @@ -SVG.FX = function FX(element) { +SVG.FX = function(element) { /* store target element */ - this.target = element; -}; + this.target = element +} // SVG.extend(SVG.FX, { // Add animation parameters and start animation animate: function(duration, ease) { /* ensure default duration and easing */ - duration = duration == null ? 1000 : duration; - ease = ease || '<>'; + duration = duration == null ? 1000 : duration + ease = ease || '<>' var akeys, tkeys, tvalues, element = this.target, fx = this, start = (new Date).getTime(), - finish = start + duration; + finish = start + duration /* start animation */ this.interval = setInterval(function(){ // This code was borrowed from the emile.js micro framework by Thomas Fuchs, aka MadRobby. var index, time = (new Date).getTime(), - pos = time > finish ? 1 : (time - start) / duration; + pos = time > finish ? 1 : (time - start) / duration /* collect attribute keys */ if (akeys == null) { - akeys = []; + akeys = [] for (var key in fx.attrs) - akeys.push(key); - }; + akeys.push(key) + } /* collect transformation keys */ if (tkeys == null) { - tkeys = []; + tkeys = [] for (var key in fx.trans) - tkeys.push(key); + tkeys.push(key) - tvalues = {}; - }; + tvalues = {} + } /* apply easing */ pos = ease == '<>' ? @@ -51,110 +51,110 @@ SVG.extend(SVG.FX, { pos : typeof ease == 'function' ? ease(pos) : - pos; + pos /* run all position properties */ if (fx._move) - element.move(fx._at(fx._move.x, pos), fx._at(fx._move.y, pos)); + element.move(fx._at(fx._move.x, pos), fx._at(fx._move.y, pos)) else if (fx._center) - element.move(fx._at(fx._center.x, pos), fx._at(fx._center.y, pos)); + element.move(fx._at(fx._center.x, pos), fx._at(fx._center.y, pos)) /* run all size properties */ if (fx._size) - element.size(fx._at(fx._size.width, pos), fx._at(fx._size.height, pos)); + element.size(fx._at(fx._size.width, pos), fx._at(fx._size.height, pos)) /* animate attributes */ for (index = akeys.length - 1; index >= 0; index--) - element.attr(akeys[index], fx._at(fx.attrs[akeys[index]], pos)); + element.attr(akeys[index], fx._at(fx.attrs[akeys[index]], pos)) /* animate transformations */ if (tkeys.length > 0) { for (index = tkeys.length - 1; index >= 0; index--) - tvalues[tkeys[index]] = fx._at(fx.trans[tkeys[index]], pos); + tvalues[tkeys[index]] = fx._at(fx.trans[tkeys[index]], pos) - element.transform(tvalues); + element.transform(tvalues) } /* finish off animation */ if (time > finish) { - clearInterval(fx.interval); - fx._after ? fx._after.apply(element, [fx]) : fx.stop(); + clearInterval(fx.interval) + fx._after ? fx._after.apply(element, [fx]) : fx.stop() } - }, duration > 10 ? 10 : duration); + }, duration > 10 ? 10 : duration) - return this; + return this }, // Add animatable attributes attr: function(a, v, n) { if (typeof a == 'object') for (var key in a) - this.attr(key, a[key]); + this.attr(key, a[key]) else - this.attrs[a] = { from: this.target.attr(a), to: v }; + this.attrs[a] = { from: this.target.attr(a), to: v } return this; }, // Add animatable transformations transform: function(o) { for (var key in o) - this.trans[key] = { from: this.target.trans[key], to: o[key] }; + this.trans[key] = { from: this.target.trans[key], to: o[key] } - return this; + return this }, // Add animatable move move: function(x, y) { - var box = this.target.bbox(); + var box = this.target.bbox() this._move = { x: { from: box.x, to: x }, y: { from: box.y, to: y } - }; + } - return this; + return this }, // Add animatable size size: function(width, height) { - var box = this.target.bbox(); + var box = this.target.bbox() this._size = { width: { from: box.width, to: width }, height: { from: box.height, to: height } - }; + } - return this; + return this }, // Add animatable center center: function(x, y) { - var box = this.target.bbox(); + var box = this.target.bbox() this._move = { x: { from: box.cx, to: x }, y: { from: box.cy, to: y } - }; + } - return this; + return this }, // Callback after animation after: function(after) { - this._after = after; + this._after = after - return this; + return this }, // Stop running animation stop: function() { /* stop current animation */ - clearInterval(this.interval); + clearInterval(this.interval) /* reset storage for properties that need animation */ - this.attrs = {}; - this.trans = {}; - this._move = null; - this._size = null; - this._after = null; + this.attrs = {} + this.trans = {} + this._move = null + this._size = null + this._after = null - return this; + return this }, // Private: at position according to from and to _at: function(o, pos) { @@ -167,45 +167,45 @@ SVG.extend(SVG.FX, { this._color(o, pos) : /* for all other values wait until pos has reached 1 to return the final value */ - pos < 1 ? o.from : o.to; + pos < 1 ? o.from : o.to }, // Private: tween color _color: function(o, pos) { - var from, to; + var from, to /* convert FROM hex to rgb */ - from = this._h2r(o.from || '#000'); + from = this._h2r(o.from || '#000') /* convert TO hex to rgb */ - to = this._h2r(o.to); + to = this._h2r(o.to) /* tween color and return hex */ return this._r2h({ r: ~~(from.r + (to.r - from.r) * pos), g: ~~(from.g + (to.g - from.g) * pos), b: ~~(from.b + (to.b - from.b) * pos) - }); + }) }, // Private: convert hex to rgb object _h2r: function(hex) { /* parse full hex */ - var match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(this._fh(hex)); + var match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(this._fh(hex)) /* if the hex is successfully parsed, return it in rgb, otherwise return black */ return match ? { r: parseInt(match[1], 16), g: parseInt(match[2], 16), b: parseInt(match[3], 16) - } : { r: 0, g: 0, b: 0 }; + } : { r: 0, g: 0, b: 0 } }, // Private: convert rgb object to hex string _r2h: function(rgb) { - return '#' + this._c2h(rgb.r) + this._c2h(rgb.g) + this._c2h(rgb.b); + return '#' + this._c2h(rgb.r) + this._c2h(rgb.g) + this._c2h(rgb.b) }, // Private: convert component to hex _c2h: function(c) { - var hex = c.toString(16); - return hex.length == 1 ? '0' + hex : hex; + var hex = c.toString(16) + return hex.length == 1 ? '0' + hex : hex }, // Private: force potential 3-based hex to 6-based _fh: function(hex) { @@ -214,26 +214,26 @@ SVG.extend(SVG.FX, { hex.substring(1, 2), hex.substring(1, 2), hex.substring(2, 3), hex.substring(2, 3), hex.substring(3, 4), hex.substring(3, 4) - ].join('') : hex; + ].join('') : hex } -}); +}) // SVG.extend(SVG.Element, { // Get fx module or create a new one, then animate with given duration and ease animate: function(duration, ease) { - return (this.fx || (this.fx = new SVG.FX(this))).stop().animate(duration, ease); + return (this.fx || (this.fx = new SVG.FX(this))).stop().animate(duration, ease) }, // Stop current animation; this is an alias to the fx instance stop: function() { - this.fx.stop(); + this.fx.stop() - return this; + return this } -}); +}) // Usage: // rect.animate(1500, '>').move(200, 300).after(function() { -// this.fill({ color: '#f06' }); -// }); +// this.fill({ color: '#f06' }) +// }) |