2016-03-16 14:44:23 +01:00
|
|
|
/* KUTE.js - The Light Tweening Engine
|
|
|
|
* package - Attributes Plugin
|
2016-12-11 02:48:37 +01:00
|
|
|
* desc - enables animation for color attributes and any numeric presentation attribute
|
2016-03-16 14:44:23 +01:00
|
|
|
* by dnp_theme
|
|
|
|
* Licensed under MIT-License
|
|
|
|
*/
|
|
|
|
|
2016-10-03 18:26:17 +02:00
|
|
|
(function (root,factory) {
|
2016-03-16 14:44:23 +01:00
|
|
|
if (typeof define === 'function' && define.amd) {
|
2017-01-27 18:16:14 +01:00
|
|
|
define(['./kute.js'], factory);
|
2016-10-07 22:10:34 +02:00
|
|
|
} else if(typeof module == 'object' && typeof require == 'function') {
|
2017-01-27 18:16:14 +01:00
|
|
|
module.exports = factory(require('./kute.js'));
|
2016-10-03 18:26:17 +02:00
|
|
|
} else if ( typeof root.KUTE !== 'undefined' ) {
|
2016-10-07 21:59:35 +02:00
|
|
|
factory(root.KUTE);
|
2016-03-16 14:44:23 +01:00
|
|
|
} else {
|
2016-10-07 21:59:35 +02:00
|
|
|
throw new Error("Attributes Plugin require KUTE.js.");
|
2016-03-16 14:44:23 +01:00
|
|
|
}
|
2016-10-03 18:26:17 +02:00
|
|
|
}(this, function (KUTE) {
|
2016-03-16 14:44:23 +01:00
|
|
|
'use strict';
|
2016-09-03 17:35:49 +02:00
|
|
|
|
2016-11-25 21:54:27 +01:00
|
|
|
var g = typeof global !== 'undefined' ? global : window, // connect to KUTE object and global
|
2016-11-30 17:12:11 +01:00
|
|
|
K = KUTE, DOM = K.dom, prepareStart = K.prepareStart, parseProperty = K.parseProperty,
|
2016-11-25 21:54:27 +01:00
|
|
|
trueColor = K.truC, trueDimension = K.truD, crossCheck = K.crossCheck,
|
2016-11-30 17:12:11 +01:00
|
|
|
unit = g.Interpolate.unit, number = g.Interpolate.number, color = g.Interpolate.color;
|
2016-11-25 21:54:27 +01:00
|
|
|
|
|
|
|
// here we go with the plugin
|
|
|
|
var getCurrentValue = function(e,a){ return e.getAttribute(a); }, // get current attribute value
|
2016-12-11 02:48:37 +01:00
|
|
|
svgColors = ['fill','stroke','stop-color'], attributes,
|
2016-09-20 05:30:53 +02:00
|
|
|
replaceUppercase = function(a) {
|
2016-10-03 18:26:17 +02:00
|
|
|
return a.replace(/[A-Z]/g, "-$&").toLowerCase();
|
2017-01-27 18:16:14 +01:00
|
|
|
};
|
|
|
|
|
2016-11-30 17:12:11 +01:00
|
|
|
prepareStart.attr = function(p,v){
|
2016-11-24 21:57:33 +01:00
|
|
|
var attrStartValues = {};
|
2016-03-16 14:44:23 +01:00
|
|
|
for (var a in v){
|
2016-11-23 19:42:01 +01:00
|
|
|
var attribute = replaceUppercase(a).replace(/_+[a-z]+/,''),
|
2016-11-30 17:12:11 +01:00
|
|
|
currentValue = getCurrentValue(this.element,attribute); // get the value for 'fill-opacity' not fillOpacity
|
2016-11-25 21:54:27 +01:00
|
|
|
attrStartValues[attribute] = svgColors.indexOf(attribute) !== -1 ? (currentValue || 'rgba(0,0,0,0)') : (currentValue || (/opacity/i.test(a) ? 1 : 0));
|
2016-03-16 14:44:23 +01:00
|
|
|
}
|
2016-11-24 21:57:33 +01:00
|
|
|
return attrStartValues;
|
2016-03-16 14:44:23 +01:00
|
|
|
};
|
2017-01-27 18:16:14 +01:00
|
|
|
|
|
|
|
// process attributes object K.pp.attr(t[x])
|
2016-03-16 14:44:23 +01:00
|
|
|
// and also register their render functions
|
2016-11-30 17:12:11 +01:00
|
|
|
parseProperty.attr = function(a,o){
|
2016-09-18 00:03:06 +02:00
|
|
|
if (!('attr' in DOM)) {
|
2016-11-23 19:42:01 +01:00
|
|
|
DOM.attr = function(l,p,a,b,v) {
|
2016-09-18 00:03:06 +02:00
|
|
|
for ( var o in b ){
|
|
|
|
DOM.attributes[o](l,o,a[o],b[o],v);
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 02:48:37 +01:00
|
|
|
attributes = DOM.attributes = {}
|
2016-09-18 00:03:06 +02:00
|
|
|
}
|
|
|
|
|
2016-12-11 02:48:37 +01:00
|
|
|
var attributesObject = {};
|
2016-10-13 05:09:23 +02:00
|
|
|
for ( var p in o ) {
|
2016-11-30 17:12:11 +01:00
|
|
|
var prop = replaceUppercase(p), regex = /(%|[a-z]+)$/, cv = getCurrentValue(this.element,prop.replace(/_+[a-z]+/,''));
|
2016-11-25 21:54:27 +01:00
|
|
|
if ( svgColors.indexOf(prop) === -1) {
|
2016-11-30 17:12:11 +01:00
|
|
|
if ( cv !== null && regex.test(cv) ) {
|
|
|
|
var prefix = trueDimension(cv).u || trueDimension(o[p]).u, s = /%/.test(prefix) ? '_percent' : '_'+prefix;
|
2016-12-11 02:48:37 +01:00
|
|
|
if (!(prop+s in attributes)) {
|
2016-11-30 17:12:11 +01:00
|
|
|
if (/%/.test(prefix)) {
|
2016-12-11 02:48:37 +01:00
|
|
|
attributes[prop+s] = function(l,p,a,b,v) {
|
2016-11-28 02:07:16 +01:00
|
|
|
var _p = _p || p.replace(s,'');
|
2017-10-03 00:10:01 +02:00
|
|
|
l.setAttribute(_p, ((number(a.v,b.v,v) * 1000>>0)/1000) + b.u );
|
2016-11-28 02:07:16 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-11 02:48:37 +01:00
|
|
|
attributes[prop+s] = function(l,p,a,b,v) {
|
2016-11-28 02:07:16 +01:00
|
|
|
var _p = _p || p.replace(s,'');
|
2017-10-03 00:10:01 +02:00
|
|
|
l.setAttribute(_p, ( (number(a.v,b.v,v)*1000>>0)/1000) + b.u );
|
2016-11-28 02:07:16 +01:00
|
|
|
}
|
2016-11-25 21:54:27 +01:00
|
|
|
}
|
2016-03-16 14:44:23 +01:00
|
|
|
}
|
2017-01-27 18:16:14 +01:00
|
|
|
attributesObject[prop+s] = trueDimension(o[p]);
|
2016-11-30 17:12:11 +01:00
|
|
|
} else if ( !regex.test(o[p]) || cv === null || cv !== null && !regex.test(cv) ) {
|
2016-12-11 02:48:37 +01:00
|
|
|
if (!(prop in attributes)) {
|
2016-11-28 02:07:16 +01:00
|
|
|
if (/opacity/i.test(p)) {
|
2016-12-11 02:48:37 +01:00
|
|
|
attributes[prop] = function(l,o,a,b,v) {
|
2017-10-03 00:10:01 +02:00
|
|
|
l.setAttribute(o, (number(a,b,v) * 1000 >> 0) / 1000 );
|
2016-11-28 02:07:16 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-11 02:48:37 +01:00
|
|
|
attributes[prop] = function(l,o,a,b,v) {
|
2017-10-03 00:10:01 +02:00
|
|
|
l.setAttribute(o, (number(a,b,v) * 1000 >> 0) / 1000 );
|
2016-11-28 02:07:16 +01:00
|
|
|
}
|
2016-11-25 21:54:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 18:16:14 +01:00
|
|
|
attributesObject[prop] = parseFloat(o[p]);
|
|
|
|
}
|
2016-11-25 21:54:27 +01:00
|
|
|
} else {
|
2016-12-11 02:48:37 +01:00
|
|
|
if (!(prop in attributes)) {
|
|
|
|
attributes[prop] = function(l,u,a,b,v) {
|
2016-11-23 19:42:01 +01:00
|
|
|
l.setAttribute(u, color(a,b,v,o.keepHex));
|
2016-09-24 05:02:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 02:48:37 +01:00
|
|
|
attributesObject[prop] = trueColor(o[p]);
|
2016-03-16 14:44:23 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 02:48:37 +01:00
|
|
|
return attributesObject;
|
2016-03-16 14:44:23 +01:00
|
|
|
}
|
|
|
|
|
2016-11-25 21:54:27 +01:00
|
|
|
return this;
|
2016-03-16 14:44:23 +01:00
|
|
|
}));
|