2010-07-27 21:58:23 +00:00
|
|
|
(function() {
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2011-09-22 16:40:31 +00:00
|
|
|
/**
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
2011-08-14 21:35:36 +00:00
|
|
|
fabric.util = { };
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-15 02:16:24 +00:00
|
|
|
* Removes value from an array.
|
2010-06-09 22:34:55 +00:00
|
|
|
* Presence of value (and its position in an array) is determined via `Array.prototype.indexOf`
|
2010-07-27 21:58:23 +00:00
|
|
|
* @static
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
2010-07-27 21:58:23 +00:00
|
|
|
* @method removeFromArray
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param {Array} array
|
|
|
|
|
* @param {Any} value
|
|
|
|
|
* @return {Array} original array
|
|
|
|
|
*/
|
|
|
|
|
function removeFromArray(array, value) {
|
|
|
|
|
var idx = array.indexOf(value);
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
|
array.splice(idx, 1);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-15 02:16:24 +00:00
|
|
|
* Returns random number between 2 specified ones.
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
|
|
|
|
* @method getRandomInt
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param {Number} min lower limit
|
|
|
|
|
* @param {Number} max upper limit
|
|
|
|
|
* @return {Number} random value (between min and max)
|
|
|
|
|
*/
|
|
|
|
|
function getRandomInt(min, max) {
|
|
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
var PiBy180 = Math.PI / 180;
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-15 02:16:24 +00:00
|
|
|
* Transforms degrees to radians.
|
2010-07-27 21:58:23 +00:00
|
|
|
* @static
|
|
|
|
|
* @method degreesToRadians
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param {Number} degrees value in degrees
|
|
|
|
|
* @return {Number} value in radians
|
|
|
|
|
*/
|
|
|
|
|
function degreesToRadians(degrees) {
|
|
|
|
|
return degrees * PiBy180;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-15 02:16:24 +00:00
|
|
|
* A wrapper around Number#toFixed, which contrary to native method returns number, not string.
|
2010-07-27 21:58:23 +00:00
|
|
|
* @static
|
|
|
|
|
* @method toFixed
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param {Number | String} number number to operate on
|
|
|
|
|
* @param {Number} fractionDigits number of fraction digits to "leave"
|
|
|
|
|
* @return {Number}
|
|
|
|
|
*/
|
|
|
|
|
function toFixed(number, fractionDigits) {
|
|
|
|
|
return parseFloat(Number(number).toFixed(fractionDigits));
|
|
|
|
|
}
|
2010-07-27 21:58:23 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-10-15 02:16:24 +00:00
|
|
|
* Function which always returns `false`.
|
2010-07-27 21:58:23 +00:00
|
|
|
* @static
|
|
|
|
|
* @method falseFunction
|
2010-10-15 02:16:24 +00:00
|
|
|
* @memberOf fabric.util
|
2010-07-27 21:58:23 +00:00
|
|
|
* @return {Boolean}
|
|
|
|
|
*/
|
|
|
|
|
function falseFunction() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-07-06 18:38:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
|
|
|
|
|
* @method animate
|
|
|
|
|
* @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 {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, pos,
|
|
|
|
|
onChange = options.onChange || function() { },
|
|
|
|
|
abort = options.abort || function() { return false; },
|
|
|
|
|
easing = options.easing || function(pos) { return (-Math.cos(pos * Math.PI) / 2) + 0.5; },
|
|
|
|
|
startValue = 'startValue' in options ? options.startValue : 0,
|
2011-10-29 19:04:20 +00:00
|
|
|
endValue = 'endValue' in options ? options.endValue : 100;
|
2011-07-06 18:38:56 +00:00
|
|
|
|
|
|
|
|
options.onStart && options.onStart();
|
|
|
|
|
|
|
|
|
|
var interval = setInterval(function() {
|
|
|
|
|
time = +new Date();
|
|
|
|
|
pos = time > finish ? 1 : (time - start) / duration;
|
2011-10-29 19:04:20 +00:00
|
|
|
onChange(startValue + (endValue - startValue) * easing(pos));
|
2011-07-06 18:38:56 +00:00
|
|
|
if (time > finish || abort()) {
|
|
|
|
|
clearInterval(interval);
|
|
|
|
|
options.onComplete && options.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
|
|
return interval;
|
|
|
|
|
}
|
2011-08-04 14:59:32 +00:00
|
|
|
|
|
|
|
|
fabric.util.removeFromArray = removeFromArray;
|
|
|
|
|
fabric.util.degreesToRadians = degreesToRadians;
|
|
|
|
|
fabric.util.toFixed = toFixed;
|
|
|
|
|
fabric.util.getRandomInt = getRandomInt;
|
|
|
|
|
fabric.util.falseFunction = falseFunction;
|
|
|
|
|
fabric.util.animate = animate;
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2011-10-29 19:04:20 +00:00
|
|
|
})();
|