Skip to content

Instantly share code, notes, and snippets.

@mhemesath
Created October 9, 2012 03:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhemesath/3856324 to your computer and use it in GitHub Desktop.
Save mhemesath/3856324 to your computer and use it in GitHub Desktop.
Converts a SVG transform string into a Raphael Matrix object.
function toMatrix(transform) {
var transforms = transform.split(' '),
matrix = Raphael.matrix();
for (var i=0; i<transforms.length; i++) {
var match = /^(\w+)\((.+)\)$/.exec(transforms[i]),
tranform = match[1],
values = toFloatArray(match[2]);
if ('translate' === transform) matrix.translate.apply(matrix, values);
if ('rotate' === transform) matrix.rotate.apply(matrix, values);
if ('scale' === transform) matrix.scale.apply(matrix, values);
}
function toFloatArray() {
var values = vals.split(','),
numbers = [];
for (var i=0; i<values.length; i++) {
numbers.push(parseFloat(values[i], 10));
}
return numbers;
}
return matrix;
}
@mhemesath
Copy link
Author

I need to split the transforms on something better than ' '. Its highly likely that someone will have a space within each individual transform. E.g. translate(20, 10). I'll probably see if I can come up with a regex statement to do this for me.

@mbecica
Copy link

mbecica commented Oct 9, 2012

Nice, I'm testing it out now and seeing how many ways I can break it :)

@mbecica
Copy link

mbecica commented Oct 9, 2012

ah ha! I just realized that the regex's we need are within the raphael source code: https://github.com/DmitryBaranovskiy/raphael/blob/master/raphael.core.js#L228

I'm going to see how far these get me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment