mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Fix all tests for Matrix decomposition.
This commit is contained in:
parent
e438ac8223
commit
aec13ffd43
2 changed files with 34 additions and 51 deletions
|
@ -471,6 +471,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
|
||||||
decompose: function() {
|
decompose: function() {
|
||||||
// http://dev.w3.org/csswg/css3-2d-transforms/#matrix-decomposition
|
// http://dev.w3.org/csswg/css3-2d-transforms/#matrix-decomposition
|
||||||
// http://stackoverflow.com/questions/4361242/
|
// http://stackoverflow.com/questions/4361242/
|
||||||
|
// https://github.com/wisec/DOMinator/blob/master/layout/style/nsStyleAnimation.cpp#L946
|
||||||
var a = this._a, b = this._b, c = this._c, d = this._d;
|
var a = this._a, b = this._b, c = this._c, d = this._d;
|
||||||
if (Numerical.isZero(a * d - b * c))
|
if (Numerical.isZero(a * d - b * c))
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -12,14 +12,14 @@
|
||||||
|
|
||||||
module('Matrix');
|
module('Matrix');
|
||||||
test('Decomposition: rotate()', function() {
|
test('Decomposition: rotate()', function() {
|
||||||
function testAngle(angle, expected) {
|
function testAngle(a, ea) {
|
||||||
equals(new Matrix().rotate(angle).getRotation(),
|
var m = new Matrix().rotate(a),
|
||||||
Base.pick(expected, angle),
|
s = 'new Matrix().rotate(' + a + ')';
|
||||||
'new Matrix().rotate(' + angle + ').getRotation()',
|
equals(m.getRotation(), Base.pick(ea, a),
|
||||||
|
s + '.getRotation()',
|
||||||
Numerical.TOLERANCE);
|
Numerical.TOLERANCE);
|
||||||
equals(new Matrix().rotate(angle).getScaling(),
|
equals(m.getScaling(), new Point(1, 1),
|
||||||
new Point(1, 1),
|
s + '.getScaling()');
|
||||||
'new Matrix().rotate(' + angle + ').getScaling()');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
testAngle(0);
|
testAngle(0);
|
||||||
|
@ -38,58 +38,40 @@ test('Decomposition: rotate()', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Decomposition: scale()', function() {
|
test('Decomposition: scale()', function() {
|
||||||
function testScale(sx, sy) {
|
function testScale(sx, sy, ex, ey, ea) {
|
||||||
var flipped = sx < 0 && sy < 0;
|
var m = new Matrix().scale(sx, sy),
|
||||||
equals(new Matrix().scale(sx, sy).getScaling(),
|
s = 'new Matrix().scale(' + sx + ', ' + sy + ')';
|
||||||
new Point(flipped ? -sx : sx, flipped ? -sy : sy),
|
equals(m.getScaling(), new Point(Base.pick(ex, sx), Base.pick(ey, sy)),
|
||||||
'new Matrix().scale(' + sx + ', ' + sy + ').getScaling()');
|
s + '.getScaling()');
|
||||||
equals(new Matrix().scale(sx, sy).getRotation(),
|
equals(m.getRotation(), ea || 0,
|
||||||
flipped ? 180 : 0,
|
s + '.getRotation()',
|
||||||
'new Matrix().scale(' + sx + ', ' + sy + ').getRotation()',
|
|
||||||
Numerical.TOLERANCE);
|
Numerical.TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
testScale(1, 1);
|
testScale(1, 1);
|
||||||
testScale(1, -1);
|
testScale(1, -1, -1, 1, -180); // Decomposing results in correct flipping
|
||||||
testScale(-1, 1);
|
testScale(-1, 1);
|
||||||
testScale(-1, -1);
|
testScale(-1, -1, 1, 1, 180); // Decomposing results in correct flipping
|
||||||
testScale(2, 4);
|
testScale(2, 4);
|
||||||
testScale(2, -4);
|
testScale(2, -4, -2, 4, -180); // Decomposing results in correct flipping
|
||||||
testScale(4, 2);
|
testScale(4, 2);
|
||||||
testScale(-4, 2);
|
testScale(-4, 2);
|
||||||
testScale(-4, -4);
|
testScale(-4, -4, 4, 4, 180); // Decomposing results in correct flipping
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Decomposition: rotate() & scale()', function() {
|
test('Decomposition: rotate() & scale()', function() {
|
||||||
equals(function() {
|
function testAngleAndScale(sx, sy, a, ex, ey, ea) {
|
||||||
return new Matrix().scale(2, 4).rotate(45).getScaling();
|
var m = new Matrix().scale(sx, sy).rotate(a),
|
||||||
}, new Point(2, 4));
|
s = 'new Matrix().scale(' + sx + ', ' + sy + ').rotate(' + a + ')';
|
||||||
|
equals(m.getScaling(), new Point(Base.pick(ex, sx), Base.pick(ey, sy)),
|
||||||
|
s + '.getScaling()');
|
||||||
|
equals(m.getRotation(), ea || a,
|
||||||
|
s + '.getRotation()',
|
||||||
|
Numerical.TOLERANCE);
|
||||||
|
}
|
||||||
|
|
||||||
equals(function() {
|
testAngleAndScale(2, 4, 45);
|
||||||
return new Matrix().scale(2, 4).rotate(45).getRotation();
|
testAngleAndScale(2, -4, 45, -2, 4, -135);
|
||||||
}, 45);
|
testAngleAndScale(-2, 4, 45);
|
||||||
|
testAngleAndScale(-2, -4, 45, 2, 4, -135);
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(2, -4).rotate(45).getScaling();
|
|
||||||
}, new Point(2, -4));
|
|
||||||
|
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(2, -4).rotate(45).getRotation();
|
|
||||||
}, 45);
|
|
||||||
|
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(-2, 4).rotate(45).getScaling();
|
|
||||||
}, new Point(-2, 4));
|
|
||||||
|
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(-2, 4).rotate(45).getRotation();
|
|
||||||
}, 45);
|
|
||||||
|
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(-2, -4).rotate(45).getScaling();
|
|
||||||
}, new Point(-2, -4));
|
|
||||||
|
|
||||||
equals(function() {
|
|
||||||
return new Matrix().scale(-2, -4).rotate(45).getRotation();
|
|
||||||
}, 45);
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue