From ec89611d5d02f775ca5971122b212825930799da Mon Sep 17 00:00:00 2001 From: Mariusz Jamro Date: Mon, 3 Aug 2015 21:05:12 +0200 Subject: [PATCH] Added methods for performing skew transform on the Matrix objects. --- src/matrix.js | 37 +++++++++++++++++++++++++++++++++++++ test/matrix.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/matrix.js b/src/matrix.js index 957beda..1b95144 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -153,6 +153,43 @@ Snap.plugin(function (Snap, Element, Paper, glob, Fragment) { this.add(cos, sin, -sin, cos, x, y); return this.add(1, 0, 0, 1, -x, -y); }; + /*\ + * Matrix.skewX + [ method ] + ** + * Skews the matrix along the x-axis + - x (number) Angle to skew along the x-axis (in degrees). + \*/ + matrixproto.skewX = function (x) { + return this.skew(x, 0); + }; + /*\ + * Matrix.skewY + [ method ] + ** + * Skews the matrix along the y-axis + - y (number) Angle to skew along the y-axis (in degrees). + \*/ + matrixproto.skewY = function (y) { + return this.skew(0, y); + }; + /*\ + * Matrix.skew + [ method ] + ** + * Skews the matrix + - y (number) Angle to skew along the y-axis (in degrees). + - x (number) Angle to skew along the x-axis (in degrees). + \*/ + matrixproto.skew = function (x, y) { + x = x || 0; + y = y || 0; + x = Snap.rad(x); + y = Snap.rad(y); + var c = math.tan(x).toFixed(9); + var b = math.tan(y).toFixed(9); + return this.add(1, b, c, 1, 0, 0); + }; /*\ * Matrix.x [ method ] diff --git a/test/matrix.js b/test/matrix.js index e446d7a..02242dd 100644 --- a/test/matrix.js +++ b/test/matrix.js @@ -183,6 +183,42 @@ describe("Matrix methods", function () { f: 18 }); }); + it("Matrix.skewX", function() { + var matrix = new Snap.Matrix(1, 0, 0, 1, 20, 30); + matrix.skewX(45); + expect(matrix).to.eql({ + a: 1, + b: 0, + c: 1, + d: 1, + e: 20, + f: 30 + }); + }); + it("Matrix.skewY", function() { + var matrix = new Snap.Matrix(1, 0, 0, 1, 20, 30); + matrix.skewY(45); + expect(matrix).to.eql({ + a: 1, + b: 1, + c: 0, + d: 1, + e: 20, + f: 30 + }); + }); + it("Matrix.skew", function() { + var matrix = new Snap.Matrix(1, 0, 0, 1, 20, 30); + matrix.skew(45, -45); + expect(matrix).to.eql({ + a: 1, + b: -1, + c: 1, + d: 1, + e: 20, + f: 30 + }); + }); it("Matrix.x", function() { var matrix = new Snap.Matrix(1, 0, 0, 1, 20, 30); var result = matrix.x(10, -10);