Implement SVG transform attribute parser, to completely remove dependency on baseVal.

This commit is contained in:
Jürg Lehni 2013-05-09 02:31:10 -07:00
parent 8cc74820a8
commit 9e5b47c1ac

View file

@ -273,12 +273,42 @@ new function() {
function applyTransform(item, value, name, node) {
// 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();
for (var i = 0, l = transforms && transforms.numberOfItems; i < l; i++) {
var mx = transforms.getItem(i).matrix;
matrix.concatenate(
new Matrix(mx.a, mx.b, mx.c, mx.d, mx.e, mx.f));
for (var i = 0, l = transforms.length; i < l; i++) {
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(
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);
}