Skip to content

Commit

Permalink
Proper computations with optional rotate() parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Apr 6, 2015
1 parent 5c82e7b commit 97a260b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
21 changes: 5 additions & 16 deletions plugins/_transforms.js
Expand Up @@ -32,19 +32,6 @@ exports.transform2js = function(transformString) {
} else {
// then split it into [10, 50] and collect as context.data
current.data = item.split(regTransformDataSplit).map(Number);

// 'rotate' has optional parameters <cx> <cy> which specify the point to rotate about.
// equivalent to 'translate(<cx>, <cy>) rotate(<rotate-angle>) translate(-<cx>, -<cy>)'
if (current.name == 'rotate' && current.data.length == 3) {
transforms.push({
name: 'translate',
data: [-current.data[1], -current.data[2]]
});
transforms.splice(transforms.length - 2, 0, {
name: 'translate',
data: current.data.splice(1)
});
}
}
}

Expand Down Expand Up @@ -247,11 +234,13 @@ var transformToMatrix = exports.transformToMatrix = function(transform) {
matrix = [transform.data[0], 0, 0, transform.data[1] || transform.data[0], 0, 0];
break;
case 'rotate':
// [cos(a), sin(a), -sin(a), cos(a), 0, 0]
// [cos(a), sin(a), -sin(a), cos(a), x, y]
var cos = mth.cos(transform.data[0]),
sin = mth.sin(transform.data[0]);
sin = mth.sin(transform.data[0]),
cx = transform.data[1] || 0,
cy = transform.data[2] || 0;

matrix = [cos, sin, -sin, cos, 0, 0];
matrix = [cos, sin, -sin, cos, (1 - cos) * cx + sin * cy, (1 - cos) * cy - sin * cx];
break;
case 'skewX':
// [1, 0, tan(a), 1, 0, 0]
Expand Down
2 changes: 1 addition & 1 deletion plugins/convertTransform.js
Expand Up @@ -198,7 +198,7 @@ function removeUseless(transforms) {

return transforms.filter(function(transform) {

// translate(0), rotate(0), skewX(0), skewY(0)
// translate(0[, 0]), rotate(0[, cx, cy]), skewX(0), skewY(0)
if (
['translate', 'rotate', 'skewX', 'skewY'].indexOf(transform.name) > -1 &&
(transform.data.length === 1 || transform.name === 'rotate') &&
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/convertTransform.02.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 97a260b

Please sign in to comment.