Move animation methods to an optional module

This commit is contained in:
kangax 2013-07-28 15:25:31 +02:00
parent 0c98f83fa4
commit f270ca0259
9 changed files with 346 additions and 331 deletions

View file

@ -46,7 +46,7 @@ Fabric.js started as a foundation for design editor on [printio.ru](http://print
1. [Install Node.js](https://github.com/joyent/node/wiki/Installation)
2. Build distribution file **[~76K minified, ~22K gzipped]**
2. Build distribution file **[~77K minified, ~20K gzipped]**
$ node build.js
@ -105,6 +105,7 @@ These are the optional modules that could be specified for inclusion, when build
- **freedrawing** — Adds support for free drawing
- **gestures** — Adds support for multitouch gestures with help of [Event.js](https://github.com/mudcube/Event.js)
- **object_straightening** — Adds support for rotating an object to one of 0, 90, 180, 270, etc. depending on which is angle is closer.
- **animation** — Adds support for animation (fabric.util.animate, fabric.util.requestAnimFrame, fabric.Object#animate, fabric.Canvas#fxCenterObjectH/#fxCenterObjectV/#fxRemove)
Additional flags for build script are:
@ -140,8 +141,8 @@ Follow [@fabric.js](http://twitter.com/fabricjs) or [@kangax](http://twitter.com
Questions, suggestions — [fabric.js on Google Groups](http://groups.google.com/group/fabricjs).
See [Fabric questions on Stackoverflow](stackoverflow.com/questions/tagged/fabricjs),
Fabric snippets on [jsfiddle](http://jsfiddle.net/user/fabricjs/fiddles/)
See [Fabric questions on Stackoverflow](stackoverflow.com/questions/tagged/fabricjs),
Fabric snippets on [jsfiddle](http://jsfiddle.net/user/fabricjs/fiddles/)
or [codepen.io](http://codepen.io/tag/fabricjs).
Get help in Fabric's IRC channel — irc://irc.freenode.net/#fabric.js

View file

@ -123,6 +123,7 @@ var filesToInclude = [
'src/util/dom_misc.js',
'src/util/dom_request.js',
ifSpecifiedInclude('animation', 'src/util/animate.js'),
ifSpecifiedInclude('easing', 'src/util/anim_ease.js'),
ifSpecifiedInclude('parser', 'src/parser.js'),
@ -147,7 +148,8 @@ var filesToInclude = [
ifSpecifiedInclude('interaction', 'src/canvas.class.js'),
ifSpecifiedInclude('interaction', 'src/mixins/canvas_events.mixin.js'),
'src/mixins/canvas_animation.mixin.js',
ifSpecifiedInclude('animation', 'src/mixins/animation.mixin.js'),
'src/mixins/canvas_dataurl_exporter.mixin.js',
ifSpecifiedInclude('serialization', 'src/mixins/canvas_serialization.mixin.js'),

330
dist/all.js vendored
View file

@ -2106,64 +2106,6 @@ fabric.Collection = {
return false;
}
/**
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
* @memberOf fabric.util
* @param {Object} [options] Animation options
* @param {Function} [options.onChange] Callback; invoked on every value change
* @param {Function} [options.onComplete] Callback; invoked when value change is completed
* @param {Number} [options.startValue=0] Starting value
* @param {Number} [options.endValue=100] Ending value
* @param {Number} [options.byValue=100] Value to modify the property by
* @param {Function} [options.easing] Easing function
* @param {Number} [options.duration=500] Duration of change
*/
function animate(options) {
options || (options = { });
var start = +new Date(),
duration = options.duration || 500,
finish = start + duration, time,
onChange = options.onChange || function() { },
abort = options.abort || function() { return false; },
easing = options.easing || function(t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},
startValue = 'startValue' in options ? options.startValue : 0,
endValue = 'endValue' in options ? options.endValue : 100,
byValue = options.byValue || endValue - startValue;
options.onStart && options.onStart();
(function tick() {
time = +new Date();
var currentTime = time > finish ? duration : (time - start);
onChange(easing(currentTime, startValue, byValue, duration));
if (time > finish || abort()) {
options.onComplete && options.onComplete();
return;
}
requestAnimFrame(tick);
})();
}
var _requestAnimFrame = fabric.window.requestAnimationFrame ||
fabric.window.webkitRequestAnimationFrame ||
fabric.window.mozRequestAnimationFrame ||
fabric.window.oRequestAnimationFrame ||
fabric.window.msRequestAnimationFrame ||
function(callback) {
fabric.window.setTimeout(callback, 1000 / 60);
};
/**
* requestAnimationFrame polyfill based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* @memberOf fabric.util
* @param {Function} callback Callback to invoke
* @param {DOMElement} element optional Element to associate with animation
*/
var requestAnimFrame = function() {
return _requestAnimFrame.apply(fabric.window, arguments);
};
/**
* Returns klass "Class" object of given fabric.Object type
* @memberOf fabric.util
@ -2554,8 +2496,6 @@ fabric.Collection = {
fabric.util.toFixed = toFixed;
fabric.util.getRandomInt = getRandomInt;
fabric.util.falseFunction = falseFunction;
fabric.util.animate = animate;
fabric.util.requestAnimFrame = requestAnimFrame;
fabric.util.getKlass = getKlass;
fabric.util.loadImage = loadImage;
fabric.util.enlivenObjects = enlivenObjects;
@ -3723,6 +3663,72 @@ fabric.util.string = {
})();
(function() {
/**
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
* @memberOf fabric.util
* @param {Object} [options] Animation options
* @param {Function} [options.onChange] Callback; invoked on every value change
* @param {Function} [options.onComplete] Callback; invoked when value change is completed
* @param {Number} [options.startValue=0] Starting value
* @param {Number} [options.endValue=100] Ending value
* @param {Number} [options.byValue=100] Value to modify the property by
* @param {Function} [options.easing] Easing function
* @param {Number} [options.duration=500] Duration of change
*/
function animate(options) {
options || (options = { });
var start = +new Date(),
duration = options.duration || 500,
finish = start + duration, time,
onChange = options.onChange || function() { },
abort = options.abort || function() { return false; },
easing = options.easing || function(t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},
startValue = 'startValue' in options ? options.startValue : 0,
endValue = 'endValue' in options ? options.endValue : 100,
byValue = options.byValue || endValue - startValue;
options.onStart && options.onStart();
(function tick() {
time = +new Date();
var currentTime = time > finish ? duration : (time - start);
onChange(easing(currentTime, startValue, byValue, duration));
if (time > finish || abort()) {
options.onComplete && options.onComplete();
return;
}
requestAnimFrame(tick);
})();
}
var _requestAnimFrame = fabric.window.requestAnimationFrame ||
fabric.window.webkitRequestAnimationFrame ||
fabric.window.mozRequestAnimationFrame ||
fabric.window.oRequestAnimationFrame ||
fabric.window.msRequestAnimationFrame ||
function(callback) {
fabric.window.setTimeout(callback, 1000 / 60);
};
/**
* requestAnimationFrame polyfill based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* @memberOf fabric.util
* @param {Function} callback Callback to invoke
* @param {DOMElement} element optional Element to associate with animation
*/
var requestAnimFrame = function() {
return _requestAnimFrame.apply(fabric.window, arguments);
};
fabric.util.animate = animate;
fabric.util.requestAnimFrame = requestAnimFrame;
})();
(function() {
/**
@ -9791,6 +9797,107 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
}
});
fabric.util.object.extend(fabric.Object.prototype, {
/**
* Animates object's properties
* @param {String|Object} property to animate (if string) or properties to animate (if object)
* @param {Number|Object} value to animate property to (if string was given first) or options object
* @return {fabric.Object} thisArg
* @chainable
*
* As object multiple properties
*
* object.animate({ left: ..., top: ... });
* object.animate({ left: ..., top: ... }, { duration: ... });
*
* As string one property
*
* object.animate('left', ...);
* object.animate('left', { duration: ... });
*
*/
animate: function() {
if (arguments[0] && typeof arguments[0] === 'object') {
var propsToAnimate = [ ], prop, skipCallbacks;
for (prop in arguments[0]) {
propsToAnimate.push(prop);
}
for (var i = 0, len = propsToAnimate.length; i<len; i++) {
prop = propsToAnimate[i];
skipCallbacks = i !== len - 1;
this._animate(prop, arguments[0][prop], arguments[1], skipCallbacks);
}
}
else {
this._animate.apply(this, arguments);
}
return this;
},
/**
* @private
* @param {String} property
* @param {String} to
* @param {Object} [options]
* @param {Boolean} [skipCallbacks]
*/
_animate: function(property, to, options, skipCallbacks) {
var obj = this, propPair;
to = to.toString();
if (!options) {
options = { };
}
else {
options = fabric.util.object.clone(options);
}
if (~property.indexOf('.')) {
propPair = property.split('.');
}
var currentValue = propPair
? this.get(propPair[0])[propPair[1]]
: this.get(property);
if (!('from' in options)) {
options.from = currentValue;
}
if (~to.indexOf('=')) {
to = currentValue + parseFloat(to.replace('=', ''));
}
else {
to = parseFloat(to);
}
fabric.util.animate({
startValue: options.from,
endValue: to,
byValue: options.by,
easing: options.easing,
duration: options.duration,
onChange: function(value) {
if (propPair) {
obj[propPair[0]][propPair[1]] = value;
}
else {
obj.set(property, value);
}
if (skipCallbacks) return;
options.onChange && options.onChange();
},
onComplete: function() {
if (skipCallbacks) return;
obj.setCoords();
options.onComplete && options.onComplete();
}
});
}
});
fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.StaticCanvas.prototype */ {
@ -11133,105 +11240,6 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
return this.set('shadow', new fabric.Shadow(options));
},
/**
* Animates object's properties
* @param {String|Object} property to animate (if string) or properties to animate (if object)
* @param {Number|Object} value to animate property to (if string was given first) or options object
* @return {fabric.Object} thisArg
* @chainable
*
* As object multiple properties
*
* object.animate({ left: ..., top: ... });
* object.animate({ left: ..., top: ... }, { duration: ... });
*
* As string one property
*
* object.animate('left', ...);
* object.animate('left', { duration: ... });
*
*/
animate: function() {
if (arguments[0] && typeof arguments[0] === 'object') {
var propsToAnimate = [ ], prop, skipCallbacks;
for (prop in arguments[0]) {
propsToAnimate.push(prop);
}
for (var i = 0, len = propsToAnimate.length; i<len; i++) {
prop = propsToAnimate[i];
skipCallbacks = i !== len - 1;
this._animate(prop, arguments[0][prop], arguments[1], skipCallbacks);
}
}
else {
this._animate.apply(this, arguments);
}
return this;
},
/**
* @private
* @param {String} property
* @param {String} to
* @param {Object} [options]
* @param {Boolean} [skipCallbacks]
*/
_animate: function(property, to, options, skipCallbacks) {
var obj = this, propPair;
to = to.toString();
if (!options) {
options = { };
}
else {
options = fabric.util.object.clone(options);
}
if (~property.indexOf('.')) {
propPair = property.split('.');
}
var currentValue = propPair
? this.get(propPair[0])[propPair[1]]
: this.get(property);
if (!('from' in options)) {
options.from = currentValue;
}
if (~to.indexOf('=')) {
to = currentValue + parseFloat(to.replace('=', ''));
}
else {
to = parseFloat(to);
}
fabric.util.animate({
startValue: options.from,
endValue: to,
byValue: options.by,
easing: options.easing,
duration: options.duration,
onChange: function(value) {
if (propPair) {
obj[propPair[0]][propPair[1]] = value;
}
else {
obj.set(property, value);
}
if (skipCallbacks) return;
options.onChange && options.onChange();
},
onComplete: function() {
if (skipCallbacks) return;
obj.setCoords();
options.onComplete && options.onComplete();
}
});
},
/**
* Centers object horizontally on canvas to which it was added last
* @return {fabric.Object} thisArg
@ -15508,9 +15516,7 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
replacement.height = imgEl.height;
if (fabric.isLikelyNode) {
// cut off data:image/png;base64, part in the beginning
var base64str = canvasEl.toDataURL('image/png').substring(22);
replacement.src = new Buffer(base64str, 'base64');
replacement.src = canvasEl.toBuffer();
// onload doesn't fire in some node versions, so we invoke callback manually
_this._element = replacement;

12
dist/all.min.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/all.min.js.gz vendored

Binary file not shown.

View file

@ -108,3 +108,104 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
return this;
}
});
fabric.util.object.extend(fabric.Object.prototype, {
/**
* Animates object's properties
* @param {String|Object} property to animate (if string) or properties to animate (if object)
* @param {Number|Object} value to animate property to (if string was given first) or options object
* @return {fabric.Object} thisArg
* @chainable
*
* As object multiple properties
*
* object.animate({ left: ..., top: ... });
* object.animate({ left: ..., top: ... }, { duration: ... });
*
* As string one property
*
* object.animate('left', ...);
* object.animate('left', { duration: ... });
*
*/
animate: function() {
if (arguments[0] && typeof arguments[0] === 'object') {
var propsToAnimate = [ ], prop, skipCallbacks;
for (prop in arguments[0]) {
propsToAnimate.push(prop);
}
for (var i = 0, len = propsToAnimate.length; i<len; i++) {
prop = propsToAnimate[i];
skipCallbacks = i !== len - 1;
this._animate(prop, arguments[0][prop], arguments[1], skipCallbacks);
}
}
else {
this._animate.apply(this, arguments);
}
return this;
},
/**
* @private
* @param {String} property
* @param {String} to
* @param {Object} [options]
* @param {Boolean} [skipCallbacks]
*/
_animate: function(property, to, options, skipCallbacks) {
var obj = this, propPair;
to = to.toString();
if (!options) {
options = { };
}
else {
options = fabric.util.object.clone(options);
}
if (~property.indexOf('.')) {
propPair = property.split('.');
}
var currentValue = propPair
? this.get(propPair[0])[propPair[1]]
: this.get(property);
if (!('from' in options)) {
options.from = currentValue;
}
if (~to.indexOf('=')) {
to = currentValue + parseFloat(to.replace('=', ''));
}
else {
to = parseFloat(to);
}
fabric.util.animate({
startValue: options.from,
endValue: to,
byValue: options.by,
easing: options.easing,
duration: options.duration,
onChange: function(value) {
if (propPair) {
obj[propPair[0]][propPair[1]] = value;
}
else {
obj.set(property, value);
}
if (skipCallbacks) return;
options.onChange && options.onChange();
},
onComplete: function() {
if (skipCallbacks) return;
obj.setCoords();
options.onComplete && options.onComplete();
}
});
}
});

View file

@ -1003,105 +1003,6 @@
return this.set('shadow', new fabric.Shadow(options));
},
/**
* Animates object's properties
* @param {String|Object} property to animate (if string) or properties to animate (if object)
* @param {Number|Object} value to animate property to (if string was given first) or options object
* @return {fabric.Object} thisArg
* @chainable
*
* As object multiple properties
*
* object.animate({ left: ..., top: ... });
* object.animate({ left: ..., top: ... }, { duration: ... });
*
* As string one property
*
* object.animate('left', ...);
* object.animate('left', { duration: ... });
*
*/
animate: function() {
if (arguments[0] && typeof arguments[0] === 'object') {
var propsToAnimate = [ ], prop, skipCallbacks;
for (prop in arguments[0]) {
propsToAnimate.push(prop);
}
for (var i = 0, len = propsToAnimate.length; i<len; i++) {
prop = propsToAnimate[i];
skipCallbacks = i !== len - 1;
this._animate(prop, arguments[0][prop], arguments[1], skipCallbacks);
}
}
else {
this._animate.apply(this, arguments);
}
return this;
},
/**
* @private
* @param {String} property
* @param {String} to
* @param {Object} [options]
* @param {Boolean} [skipCallbacks]
*/
_animate: function(property, to, options, skipCallbacks) {
var obj = this, propPair;
to = to.toString();
if (!options) {
options = { };
}
else {
options = fabric.util.object.clone(options);
}
if (~property.indexOf('.')) {
propPair = property.split('.');
}
var currentValue = propPair
? this.get(propPair[0])[propPair[1]]
: this.get(property);
if (!('from' in options)) {
options.from = currentValue;
}
if (~to.indexOf('=')) {
to = currentValue + parseFloat(to.replace('=', ''));
}
else {
to = parseFloat(to);
}
fabric.util.animate({
startValue: options.from,
endValue: to,
byValue: options.by,
easing: options.easing,
duration: options.duration,
onChange: function(value) {
if (propPair) {
obj[propPair[0]][propPair[1]] = value;
}
else {
obj.set(property, value);
}
if (skipCallbacks) return;
options.onChange && options.onChange();
},
onComplete: function() {
if (skipCallbacks) return;
obj.setCoords();
options.onComplete && options.onComplete();
}
});
},
/**
* Centers object horizontally on canvas to which it was added last
* @return {fabric.Object} thisArg

64
src/util/animate.js Normal file
View file

@ -0,0 +1,64 @@
(function() {
/**
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
* @memberOf fabric.util
* @param {Object} [options] Animation options
* @param {Function} [options.onChange] Callback; invoked on every value change
* @param {Function} [options.onComplete] Callback; invoked when value change is completed
* @param {Number} [options.startValue=0] Starting value
* @param {Number} [options.endValue=100] Ending value
* @param {Number} [options.byValue=100] Value to modify the property by
* @param {Function} [options.easing] Easing function
* @param {Number} [options.duration=500] Duration of change
*/
function animate(options) {
options || (options = { });
var start = +new Date(),
duration = options.duration || 500,
finish = start + duration, time,
onChange = options.onChange || function() { },
abort = options.abort || function() { return false; },
easing = options.easing || function(t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},
startValue = 'startValue' in options ? options.startValue : 0,
endValue = 'endValue' in options ? options.endValue : 100,
byValue = options.byValue || endValue - startValue;
options.onStart && options.onStart();
(function tick() {
time = +new Date();
var currentTime = time > finish ? duration : (time - start);
onChange(easing(currentTime, startValue, byValue, duration));
if (time > finish || abort()) {
options.onComplete && options.onComplete();
return;
}
requestAnimFrame(tick);
})();
}
var _requestAnimFrame = fabric.window.requestAnimationFrame ||
fabric.window.webkitRequestAnimationFrame ||
fabric.window.mozRequestAnimationFrame ||
fabric.window.oRequestAnimationFrame ||
fabric.window.msRequestAnimationFrame ||
function(callback) {
fabric.window.setTimeout(callback, 1000 / 60);
};
/**
* requestAnimationFrame polyfill based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* @memberOf fabric.util
* @param {Function} callback Callback to invoke
* @param {DOMElement} element optional Element to associate with animation
*/
var requestAnimFrame = function() {
return _requestAnimFrame.apply(fabric.window, arguments);
};
fabric.util.animate = animate;
fabric.util.requestAnimFrame = requestAnimFrame;
})();

View file

@ -104,64 +104,6 @@
return false;
}
/**
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
* @memberOf fabric.util
* @param {Object} [options] Animation options
* @param {Function} [options.onChange] Callback; invoked on every value change
* @param {Function} [options.onComplete] Callback; invoked when value change is completed
* @param {Number} [options.startValue=0] Starting value
* @param {Number} [options.endValue=100] Ending value
* @param {Number} [options.byValue=100] Value to modify the property by
* @param {Function} [options.easing] Easing function
* @param {Number} [options.duration=500] Duration of change
*/
function animate(options) {
options || (options = { });
var start = +new Date(),
duration = options.duration || 500,
finish = start + duration, time,
onChange = options.onChange || function() { },
abort = options.abort || function() { return false; },
easing = options.easing || function(t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},
startValue = 'startValue' in options ? options.startValue : 0,
endValue = 'endValue' in options ? options.endValue : 100,
byValue = options.byValue || endValue - startValue;
options.onStart && options.onStart();
(function tick() {
time = +new Date();
var currentTime = time > finish ? duration : (time - start);
onChange(easing(currentTime, startValue, byValue, duration));
if (time > finish || abort()) {
options.onComplete && options.onComplete();
return;
}
requestAnimFrame(tick);
})();
}
var _requestAnimFrame = fabric.window.requestAnimationFrame ||
fabric.window.webkitRequestAnimationFrame ||
fabric.window.mozRequestAnimationFrame ||
fabric.window.oRequestAnimationFrame ||
fabric.window.msRequestAnimationFrame ||
function(callback) {
fabric.window.setTimeout(callback, 1000 / 60);
};
/**
* requestAnimationFrame polyfill based on http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* @memberOf fabric.util
* @param {Function} callback Callback to invoke
* @param {DOMElement} element optional Element to associate with animation
*/
var requestAnimFrame = function() {
return _requestAnimFrame.apply(fabric.window, arguments);
};
/**
* Returns klass "Class" object of given fabric.Object type
* @memberOf fabric.util
@ -552,8 +494,6 @@
fabric.util.toFixed = toFixed;
fabric.util.getRandomInt = getRandomInt;
fabric.util.falseFunction = falseFunction;
fabric.util.animate = animate;
fabric.util.requestAnimFrame = requestAnimFrame;
fabric.util.getKlass = getKlass;
fabric.util.loadImage = loadImage;
fabric.util.enlivenObjects = enlivenObjects;