Optimise Path#transformContent() by using Matrix#transform() version that handles arrays of multiple point values for much improved performance.

This commit is contained in:
Jürg Lehni 2011-02-20 01:55:43 +01:00
parent ed752ed744
commit 6c4121db4b

View file

@ -109,21 +109,28 @@ Path = PathItem.extend({
transformContent: function(matrix, flags) { transformContent: function(matrix, flags) {
for (var i = 0, l = this._segments.length; i < l; i++) { for (var i = 0, l = this._segments.length; i < l; i++) {
var segment = this._segments[i]; var segment = this._segments[i];
// Use matrix.transform version() that takes arrays of multiple
// points for largely improved performance, as no calls to
// Point.read() and Point constructors are necessary.
var point = segment.point;
var handleIn = segment.handleIn;
var handleOut = segment.handleOut;
var x = point.x, y = point.y;
// We need to convert handles to absolute coordinates in order // We need to convert handles to absolute coordinates in order
// to transform them. // to transform them.
// TODO: Is transformation even required if they are [0, 0]? // TODO: Is transformation even required if they are [0, 0]?
// TODO: Can we optimise this by using the matrix.transform() var coords = [
// version that takes arrays as in and output values, and just x, y,
// modifying points rather than producing new ones? This would handleIn.x + x, handleIn.y + y,
// consume less memory for sure. handleOut.x + x, handleOut.y + y,
var point = segment.point; ]
var handleIn = segment.handleIn.add(point); matrix.transform(coords, 0, coords, 0, 3);
var handleOut = segment.handleOut.add(point); point.x = x = coords[0];
point = matrix.transform(point); point.y = y = coords[1];
segment.point = point; handleIn.x = coords[2] - x;
// Convert handles back to relative values after transformation handleIn.y = coords[3] - y;
segment.handleIn = matrix.transform(handleIn).subtract(point); handleOut.x = coords[4] - x;
segment.handleOut = matrix.transform(handleOut).subtract(point); handleOut.y = coords[5] - y;
} }
}, },