paper.js/test/tests/Matrix.js

76 lines
2.3 KiB
JavaScript
Raw Normal View History

/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
QUnit.module('Matrix');
test('Decomposition: rotate()', function() {
2014-08-16 13:24:54 -04:00
function testAngle(a, ea) {
var m = new Matrix().rotate(a),
s = 'new Matrix().rotate(' + a + ')';
equals(m.getRotation(), Base.pick(ea, a),
s + '.getRotation()');
equals(m.getScaling(), new Point(1, 1),
2014-08-16 13:24:54 -04:00
s + '.getScaling()');
}
2014-08-16 13:24:54 -04:00
testAngle(0);
testAngle(1);
testAngle(45);
testAngle(90);
testAngle(135);
testAngle(180);
testAngle(270, -90);
testAngle(-1);
testAngle(-45);
testAngle(-90);
testAngle(-135);
testAngle(-180);
testAngle(-270, 90);
});
test('Decomposition: scale()', function() {
2014-08-16 13:24:54 -04:00
function testScale(sx, sy, ex, ey, ea) {
var m = new Matrix().scale(sx, sy),
s = 'new Matrix().scale(' + sx + ', ' + sy + ')';
equals(m.getRotation(), ea || 0,
s + '.getRotation()');
equals(m.getScaling(), new Point(Base.pick(ex, sx), Base.pick(ey, sy)),
2014-08-16 13:24:54 -04:00
s + '.getScaling()');
}
2014-08-16 13:24:54 -04:00
testScale(1, 1);
testScale(1, -1);
testScale(-1, 1, 1, -1, -180); // Decomposing results in correct flipping
testScale(-1, -1, 1, 1, -180); // Decomposing results in correct flipping
2014-08-16 13:24:54 -04:00
testScale(2, 4);
testScale(2, -4);
2014-08-16 13:24:54 -04:00
testScale(4, 2);
testScale(-4, 2, 4, -2, -180); // Decomposing results in correct flipping
testScale(-4, -4, 4, 4, -180); // Decomposing results in correct flipping
});
test('Decomposition: scale() & rotate()', function() {
2014-08-16 13:24:54 -04:00
function testAngleAndScale(sx, sy, a, ex, ey, ea) {
var m = new Matrix().rotate(a).scale(sx, sy),
2014-08-16 13:24:54 -04:00
s = 'new Matrix().scale(' + sx + ', ' + sy + ').rotate(' + a + ')';
equals(m.getRotation(), ea || a,
s + '.getRotation()');
equals(m.getScaling(), new Point(Base.pick(ex, sx), Base.pick(ey, sy)),
2014-08-16 13:24:54 -04:00
s + '.getScaling()');
}
2014-08-16 13:24:54 -04:00
testAngleAndScale(2, 4, 45);
testAngleAndScale(2, -4, 45);
testAngleAndScale(-2, 4, 45, 2, -4, -135);
2014-08-16 13:24:54 -04:00
testAngleAndScale(-2, -4, 45, 2, 4, -135);
});