mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Introduce private faster Matrix#_transform* methods that lack arguments checks, and use them internally.
This commit is contained in:
parent
aed1bb0878
commit
c36db3a55c
7 changed files with 40 additions and 32 deletions
|
@ -292,26 +292,34 @@ var Matrix = this.Matrix = Base.extend({
|
|||
* @param {number} numPts The number of points to tranform.
|
||||
*/
|
||||
transform: function(/* point | */ src, srcOff, dst, dstOff, numPts) {
|
||||
if (arguments.length == 5) {
|
||||
var i = srcOff,
|
||||
j = dstOff,
|
||||
srcEnd = srcOff + 2 * numPts;
|
||||
while (i < srcEnd) {
|
||||
var x = src[i++];
|
||||
var y = src[i++];
|
||||
dst[j++] = x * this._m00 + y * this._m01 + this._m02;
|
||||
dst[j++] = x * this._m10 + y * this._m11 + this._m12;
|
||||
}
|
||||
return dst;
|
||||
} else if (arguments.length > 0) {
|
||||
var point = Point.read(arguments),
|
||||
x = point.x, y = point.y;
|
||||
return Point.create(
|
||||
x * this._m00 + y * this._m01 + this._m02,
|
||||
x * this._m10 + y * this._m11 + this._m12
|
||||
);
|
||||
return arguments.length < 5
|
||||
? this._transformPoint(Point.read(arguments))
|
||||
: this._transformCoordinates(src, srcOff, dst, dstOff, numPts);
|
||||
},
|
||||
|
||||
/**
|
||||
* A faster version of transform that only takes one point and does not
|
||||
* attempt to convert it.
|
||||
*/
|
||||
_transformPoint: function(point) {
|
||||
var x = point.x,
|
||||
y = point.y;
|
||||
return Point.create(
|
||||
x * this._m00 + y * this._m01 + this._m02,
|
||||
x * this._m10 + y * this._m11 + this._m12
|
||||
);
|
||||
},
|
||||
|
||||
_transformCoordinates: function(src, srcOff, dst, dstOff, numPts) {
|
||||
var i = srcOff, j = dstOff,
|
||||
srcEnd = srcOff + 2 * numPts;
|
||||
while (i < srcEnd) {
|
||||
var x = src[i++];
|
||||
var y = src[i++];
|
||||
dst[j++] = x * this._m00 + y * this._m01 + this._m02;
|
||||
dst[j++] = x * this._m10 + y * this._m11 + this._m12;
|
||||
}
|
||||
return null;
|
||||
return dst;
|
||||
},
|
||||
|
||||
transformBounds: function(bounds) {
|
||||
|
@ -323,7 +331,7 @@ var Matrix = this.Matrix = Base.extend({
|
|||
br = bounds.getBottomRight(),
|
||||
bl = bounds.getBottomLeft(),
|
||||
coords = [ tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y ];
|
||||
this.transform(coords, 0, coords, 0, 4);
|
||||
this._transformCoordinates(coords, 0, coords, 0, 4);
|
||||
// Loop through all x and y coordinates and update min and max values.
|
||||
// Start with the first coordinate pair for both (coords.slice(0, 2)).
|
||||
var min = coords.slice(0, 2), max = min.slice(0);
|
||||
|
|
|
@ -112,7 +112,7 @@ var Point = this.Point = Base.extend({
|
|||
},
|
||||
|
||||
transform: function(matrix) {
|
||||
return matrix.transform(this);
|
||||
return matrix._transformPoint(this);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,11 +78,11 @@ var DocumentView = this.DocumentView = Base.extend({
|
|||
// TODO: getMousePoint
|
||||
// TODO: artworkToView(rect)
|
||||
artworkToView: function(point) {
|
||||
return this.matrix.transform(point);
|
||||
return this.matrix._transformPoint(point);
|
||||
},
|
||||
|
||||
viewToArtwork: function(point) {
|
||||
// TODO: cache the inverse matrix:
|
||||
return this.matrix.createInverse().transform(point);
|
||||
return this.matrix.createInverse()._transformPoint(point);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -692,7 +692,7 @@ var Item = this.Item = Base.extend({
|
|||
left, bottom
|
||||
];
|
||||
if (matrix)
|
||||
matrix.transform(coords, 0, coords, 0, 4);
|
||||
matrix._transformCoordinates(coords, 0, coords, 0, 4);
|
||||
ctx.beginPath();
|
||||
for (var i = 0; i < 8; i++)
|
||||
ctx[i == 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
|
||||
|
|
|
@ -185,10 +185,10 @@ var Curve = this.Curve = Base.extend({
|
|||
|
||||
transform: function(matrix) {
|
||||
return new Curve(
|
||||
matrix.transform(this._segment1._point),
|
||||
matrix.transform(this._segment1._handleOut),
|
||||
matrix.transform(this._segment2._handleIn),
|
||||
matrix.transform(this._segment2._point));
|
||||
matrix._transformPoint(this._segment1._point),
|
||||
matrix._transformPoint(this._segment1._handleOut),
|
||||
matrix._transformPoint(this._segment2._handleIn),
|
||||
matrix._transformPoint(this._segment2._point));
|
||||
},
|
||||
|
||||
reverse: function() {
|
||||
|
|
|
@ -124,7 +124,7 @@ var Path = this.Path = PathItem.extend({
|
|||
if (!matrix.isIdentity()) {
|
||||
var coords = new Array(6);
|
||||
for (var i = 0, l = this._segments.length; i < l; i++) {
|
||||
this._segments[i]._transformCoordinates(matrix, coords, true);
|
||||
this._segments[i].__transformCoordinates(matrix, coords, true);
|
||||
}
|
||||
}
|
||||
this._changed();
|
||||
|
@ -894,11 +894,11 @@ var Path = this.Path = PathItem.extend({
|
|||
// Make coordinates for first segment available in prevCoords.
|
||||
if (matrix && matrix.isIdentity())
|
||||
matrix = null;
|
||||
first._transformCoordinates(matrix, prevCoords, false);
|
||||
first.__transformCoordinates(matrix, prevCoords, false);
|
||||
var min = prevCoords.slice(0, 2),
|
||||
max = min.slice(0); // clone
|
||||
function processSegment(segment) {
|
||||
segment._transformCoordinates(matrix, coords, false);
|
||||
segment.__transformCoordinates(matrix, coords, false);
|
||||
|
||||
for (var i = 0; i < 2; i++) {
|
||||
var v0 = prevCoords[i], // prev.point
|
||||
|
|
|
@ -265,7 +265,7 @@ var Segment = this.Segment = Base.extend({
|
|||
coords[i++] = handleOut._y + y;
|
||||
}
|
||||
if (matrix) {
|
||||
matrix.transform(coords, 0, coords, 0, i / 2);
|
||||
matrix._transformCoordinates(coords, 0, coords, 0, i / 2);
|
||||
x = coords[0];
|
||||
y = coords[1];
|
||||
if (change) {
|
||||
|
|
Loading…
Reference in a new issue