mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Implement SVG transform attribute parser, to completely remove dependency on baseVal.
This commit is contained in:
parent
8cc74820a8
commit
9e5b47c1ac
1 changed files with 35 additions and 5 deletions
|
@ -273,12 +273,42 @@ new function() {
|
||||||
|
|
||||||
function applyTransform(item, value, name, node) {
|
function applyTransform(item, value, name, node) {
|
||||||
// http://www.w3.org/TR/SVG/types.html#DataTypeTransformList
|
// http://www.w3.org/TR/SVG/types.html#DataTypeTransformList
|
||||||
var transforms = node[name] && node[name].baseVal,
|
// Parse SVG transform string. First we split at /)\s*/, to separate
|
||||||
|
// commands
|
||||||
|
var transforms = (node.getAttribute(name) || '').split(/\)\s*/g),
|
||||||
matrix = new Matrix();
|
matrix = new Matrix();
|
||||||
for (var i = 0, l = transforms && transforms.numberOfItems; i < l; i++) {
|
for (var i = 0, l = transforms.length; i < l; i++) {
|
||||||
var mx = transforms.getItem(i).matrix;
|
var transform = transforms[i];
|
||||||
|
if (!transform)
|
||||||
|
break;
|
||||||
|
// Command come before the '(', values after
|
||||||
|
var parts = transform.split('('),
|
||||||
|
command = parts[0],
|
||||||
|
v = parts[1].split(/[\s,]+/g);
|
||||||
|
// Convert values to floats
|
||||||
|
for (var j = 0, m = v.length; j < m; j++)
|
||||||
|
v[j] = parseFloat(v[j]);
|
||||||
|
switch (command) {
|
||||||
|
case 'matrix':
|
||||||
matrix.concatenate(
|
matrix.concatenate(
|
||||||
new Matrix(mx.a, mx.b, mx.c, mx.d, mx.e, mx.f));
|
new Matrix(v[0], v[2], v[1], v[3], v[4], v[5]));
|
||||||
|
break;
|
||||||
|
case 'rotate':
|
||||||
|
matrix.rotate(v[0], v[1], v[2]);
|
||||||
|
break;
|
||||||
|
case 'translate':
|
||||||
|
matrix.translate(v[0], v[1]);
|
||||||
|
break;
|
||||||
|
case 'scale':
|
||||||
|
matrix.scale(v);
|
||||||
|
break;
|
||||||
|
case 'skewX':
|
||||||
|
case 'skewY':
|
||||||
|
var value = Math.tan(v[0] * Math.PI / 180),
|
||||||
|
isX = command == 'skewX';
|
||||||
|
matrix.shear(isX ? value : 0, isX ? 0 : value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
item.transform(matrix);
|
item.transform(matrix);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue