-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadditive-transform.js
419 lines (387 loc) · 13.7 KB
/
additive-transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* Created by Brett on 26/11/13.
*/
(function(exports,document,undefined){
"use strict";
var IDENTITY_MATRIX = {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
tx: 0.0,
ty: 0.0
};
function forEachNode(nodes, action, thisArg) {
var forEach = Array.prototype.forEach;
forEach.call(nodes, action, thisArg || this);
}
function CSSParser() {
var DEGREES_TO_RADIANS = Math.PI/180;
var GRADIANS_TO_RADIANS = Math.PI/200;
var TURNS_TO_RADIANS = 2*Math.PI;
var REGEXP_ALL_LETTERS_AND_BRACKETS = /[a-zA-Z\(\)]/g;
var REGEXP_MATRIX = /matrix/;
var REGEXP_TRANSLATE = /translate[X|Y]?/;
var REGEXP_SCALE = /scale[X|Y]?/;
var REGEXP_ROTATE = /rotate/;
var REGEXP_SKEW = /skew[X|Y]?/;
var cachedTransformPropertyName = null;
var isUndefined = function(value) {
return typeof value === "undefined";
};
this.getSupportedTransform = function() {
if( cachedTransformPropertyName ) { return cachedTransformPropertyName; }
var transformPropertyName = null;
var vendorTransformOptions = [
'transform',
'WebkitTransform',
'MozTransform',
'msTransform',
'Otransform'
];
var dummy = document.createElement('div');
vendorTransformOptions.some(function(element){
if(!isUndefined(dummy.style[element])) {
transformPropertyName = element;
return true;
}
return false;
});
dummy = null;
cachedTransformPropertyName = transformPropertyName;
return cachedTransformPropertyName;
};
this.cssMatrixToObject = function(cssMatrix) {
var matrixValues = cssMatrix.replace(/matrix/gi, "").replace(/[ \(\)]+/g,"").split(",");
return {
a: parseFloat( matrixValues[0] ),
b: parseFloat( matrixValues[1] ),
c: parseFloat( matrixValues[2] ),
d: parseFloat( matrixValues[3] ),
tx: parseFloat( matrixValues[4] ),
ty: parseFloat( matrixValues[5] )
};
};
this.matrixObjectToCSS = function(matrixObject){
return "matrix("+
matrixObject.a.toFixed(2)+
", "+matrixObject.b.toFixed(2)+
", "+matrixObject.c.toFixed(2)+
", "+matrixObject.d.toFixed(2)+
", "+matrixObject.tx.toFixed(2)+
", "+matrixObject.ty.toFixed(2)+")";
};
var parseCSSValue = function(value, numericValue, outputFormat, unitIdentifiers, isError, errorMessage){
var result,
unitIdentifier,
item;
for(unitIdentifier in unitIdentifiers) {
if(unitIdentifiers.hasOwnProperty(unitIdentifier)) {
item = unitIdentifiers[unitIdentifier];
if(item.check(value)){
result = item.parse[outputFormat].call(this, numericValue);
break;
}
}
}
if(isError(result)){ throw errorMessage(value); }
return result;
};
var parseRotateToRadians = function(value){
var numericValues = value.replace( REGEXP_ALL_LETTERS_AND_BRACKETS, "").replace(/ /g, "").split(",").map(function(item){ return parseFloat(item); });
return parseCSSValue(value, numericValues[0], "toRadians", angleUnitIdentifiers, isUndefined, function(value){
return "Angle unit identifier not recognised for value: "+value;
});
};
var parseScaleToXY = function(value){
var numericValues = value.replace( REGEXP_ALL_LETTERS_AND_BRACKETS, "").replace(/ /g, "").split(",").map(function(item){ return parseFloat(item); });
if(numericValues.length == 1) {
if(/scaleX/.exec(value)) {
numericValues[1] = 1.0;
} else if (/scaleY/.exec(value)) {
numericValues[1] = numericValues[0];
numericValues[0] = 1.0;
} else {
numericValues[1] = numericValues[0];
}
}
return {x: numericValues[0], y: numericValues[1]};
};
var parseTranslateToXY = function(value){
var numericValues = value.replace( REGEXP_ALL_LETTERS_AND_BRACKETS, "").replace(/ /g, "").split(",").map(function(item){ return parseFloat(item); });
var parsedNumericValues = numericValues.map(function(numericValue) {
return parseCSSValue(
value,
numericValue,
"toPixels",
lengthUnitIdentifiers,
isUndefined,
function(value) {
return "Translate unit identifier not recognised for value: " + value;
}
);
});
if(parsedNumericValues.length == 1) {
if(/translateX/.exec(value)) {
parsedNumericValues[1] = 0.0;
} else if(/translateY/.exec(value)) {
parsedNumericValues[1] = parsedNumericValues[0];
parsedNumericValues[0] = 0.0;
} else {
parsedNumericValues[1] = 0.0;
}
}
return {x: parsedNumericValues[0] , y: parsedNumericValues[1] };
};
var parseSkewToXY = function(value){
var numericValues = value.replace( REGEXP_ALL_LETTERS_AND_BRACKETS, "").replace(/ /g, "").split(",").map(function(item){ return parseFloat(item); });
var parsedNumericValues = numericValues.map(function(numericValue) {
return parseCSSValue(
value,
numericValue,
"toRadians",
angleUnitIdentifiers,
isUndefined,
function(value){
return "Skew unit identifier not recognised for value: "+value;
}
);
});
if(parsedNumericValues.length == 1)
{
if(/skewX/.exec(value)) {
parsedNumericValues[1] = 0.0;
} else if(/skewY/.exec(value)) {
parsedNumericValues[1] = parsedNumericValues[0];
parsedNumericValues[0] = 0.0;
} else {
parsedNumericValues[1] = 0.0;
}
}
return { x: parsedNumericValues[0], y: parsedNumericValues[1] };
};
this.transformFunctions2D = {
matrix: {
check: function(testValue){
return REGEXP_MATRIX.exec(testValue);
},
parse: function(value) {
},
transform: function(value, matrix){
}
},
translate: {
check: function(testValue){
return REGEXP_TRANSLATE.exec(testValue);
},
parse: function(value){
return parseTranslateToXY(value);
},
transform: function(value, matrix){
matrix.tx += value.x;
matrix.ty += value.y;
return matrix;
}
},
scale: {
check: function(testValue){
return REGEXP_SCALE.exec(testValue);
},
parse: function(value){
return parseScaleToXY(value);
},
transform: function(value, matrix){
matrix.a *= value.x;
matrix.b *= value.y;
matrix.c *= value.x;
matrix.d *= value.y;
matrix.tx *= value.x;
matrix.ty *= value.y;
return matrix;
}
},
rotate: {
check: function(testValue){
return REGEXP_ROTATE.exec(testValue);
},
parse: function(value){
return parseRotateToRadians(value);
},
transform: function(value, matrix) {
var cosValue = Math.cos( value );
var sinValue = Math.sin( value );
var a = matrix.a;
var b = matrix.b;
var c = matrix.c;
var d = matrix.d;
var tx = matrix.tx;
var ty = matrix.ty;
matrix.a = a*cosValue - b*sinValue;
matrix.b = a*sinValue + b*cosValue;
matrix.c = c*cosValue - d*sinValue;
matrix.d = c*sinValue + d*cosValue;
matrix.tx = tx*cosValue - ty*sinValue;
matrix.ty = tx*sinValue + ty*cosValue;
return matrix;
}
},
skew: {
check: function(testValue){
return REGEXP_SKEW.exec(testValue);
},
parse: function(value){
return parseSkewToXY(value);
},
transform: function(value, matrix){
matrix.b += Math.tan( value.y );
matrix.c += Math.tan( value.x );
return matrix;
}
}
};
///TODO Add 3D transforms
/**
this.transformFunctions3D = {
};
**/
var lengthUnitIdentifiers = {
px: {
check: function(testValue){
return /px/.exec(testValue);
},
parse: {
toPixels: function(value){
return value;
}
}
}
};
var angleUnitIdentifiers = {
deg:{
check: function(testValue){
return /deg/.exec(testValue);
},
parse: {
toRadians: function(value){
return value * DEGREES_TO_RADIANS;
}
}
},
grad:{
check: function(testValue){
return /grad/.exec(testValue);
},
parse: {
toRadians: function(value){
return value * GRADIANS_TO_RADIANS;
}
}
},
rad:{
check: function(testValue){
return /(!g)rad/.exec(testValue);
},
parse: {
toRadians: function(value){
return value;
}
}
},
turn:{
check: function(testValue){
return /turn/.exec(testValue);
},
parse: {
toRadians: function(value){
return value * TURNS_TO_RADIANS;
}
}
}
};
}
var AdditiveTransform = {
_config: {
selector: ".add-transforms",
dataAttribute: "data-transforms"
},
_cssParser: new CSSParser(),
configure: function (config) {
for (var property in this._config) {
if (this._config.hasOwnProperty(property) && property in config) {
this._config[property] = config[property];
}
}
},
transform: function () {
var allNodesToTransform = document.querySelectorAll(this._config.selector);
forEachNode(allNodesToTransform, this.transformNode, this);
},
transformNode: function (node) {
var transformationSelectors = this.getNodeSelectors(node),
nodeState = this.getNodeInitialState(node);
transformationSelectors.forEach(function(transformationSelector) {
nodeState = this.addTransform(node, nodeState, transformationSelector);
}, this);
},
getNodeSelectors: function (node) {
return node.getAttribute(this._config.dataAttribute).replace(/ /g, "").split(",");
},
getNodeInitialState: function (node) {
var initialStateCSS = getComputedStyle(node, null)[this._cssParser.getSupportedTransform()];
return (initialStateCSS === "none") ?
IDENTITY_MATRIX :
this._cssParser.cssMatrixToObject(initialStateCSS);
},
addTransform: function (node, nodeState, selector) {
var cssTransformationRules = this.getCSSRulesFor(selector);
cssTransformationRules.forEach(function(rule) {
if (/transform/.exec(rule.property)) {
var matrix = this.transformMatrix(nodeState, rule.value);
node.style[this._cssParser.getSupportedTransform()] = this._cssParser.matrixObjectToCSS(matrix);
nodeState = matrix;
}
}, this);
return nodeState;
},
getCSSRulesFor: function (selector) {
var cssRules = [];
forEachNode(document.styleSheets, function(sheet) {
if (!sheet.cssRules) {
return;
}
forEachNode(sheet.cssRules, function(rule) {
if (rule.selectorText && rule.selectorText.split(',').indexOf(selector) !== -1) {
var styles = rule.style;
forEachNode(rule.style, function(style) {
cssRules.push({
property: style,
value: styles[style]
});
});
}
});
});
return cssRules;
},
transformMatrix: function (source, transform) {
var values = transform.split(") "); // split into array if multiple values
var matrix = Object.create(source);
values.forEach(function (value) {
var transformFunction;
var transformFunctionName;
for (transformFunctionName in this._cssParser.transformFunctions2D) {
if (this._cssParser.transformFunctions2D.hasOwnProperty(transformFunctionName)) {
transformFunction = this._cssParser.transformFunctions2D[transformFunctionName];
if (transformFunction.check(value)) {
var transformValue = transformFunction.parse(value);
matrix = transformFunction.transform(transformValue, matrix);
break;
}
}
}
}, this);
return matrix;
}
};
exports.AdditiveTransform = exports.AdditiveTransform || AdditiveTransform;
})(window,document);