[canvaskit] In Canvas2d, move matrix to internal canvas
This is similar to how Chrome does it - more or less unapplying
the matrix as it changes to keep everything in the same
coordinate space.
We still need to keep the matrix around for things and do the math
ourselves for things like gradients.
This simplifies a bunch of logic, which makes me happy.
Bug: skia:
Change-Id: I66ee379a501cc6701f51c0f0b51d4e0634a4a692
Reviewed-on: https://skia-review.googlesource.com/c/174843
Reviewed-by: Florin Malita <fmalita@chromium.org>
diff --git a/experimental/canvaskit/interface.js b/experimental/canvaskit/interface.js
index 2cb6216..e1b9d1f 100644
--- a/experimental/canvaskit/interface.js
+++ b/experimental/canvaskit/interface.js
@@ -12,6 +12,10 @@
// Add some helpers for matrices. This is ported from SkMatrix.cpp
// to save complexity and overhead of going back and forth between
// C++ and JS layers.
+ // I would have liked to use something like DOMMatrix, except it
+ // isn't widely supported (would need polyfills) and it doesn't
+ // have a mapPoints() function (which could maybe be tacked on here).
+ // If DOMMatrix catches on, it would be worth re-considering this usage.
CanvasKit.SkMatrix = {};
function sdot(a, b, c, d, e, f) {
e = e || 0;
@@ -27,6 +31,22 @@
];
};
+ // Return the inverse (if it exists) of this matrix.
+ // Otherwise, return the identity.
+ CanvasKit.SkMatrix.invert = function(m) {
+ var det = m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7]
+ - m[2]*m[4]*m[6] - m[1]*m[3]*m[8] - m[0]*m[5]*m[7];
+ if (!det) {
+ SkDebug('Warning, uninvertible matrix');
+ return CanvasKit.SkMatrix.identity();
+ }
+ return [
+ (m[4]*m[8] - m[5]*m[7])/det, (m[2]*m[7] - m[1]*m[8])/det, (m[1]*m[5] - m[2]*m[4])/det,
+ (m[5]*m[6] - m[3]*m[8])/det, (m[0]*m[8] - m[2]*m[6])/det, (m[2]*m[3] - m[0]*m[5])/det,
+ (m[3]*m[7] - m[4]*m[6])/det, (m[1]*m[6] - m[0]*m[7])/det, (m[0]*m[4] - m[1]*m[3])/det,
+ ];
+ };
+
// Maps the given points according to the passed in matrix.
// Results are done in place.
// See SkMatrix.h::mapPoints for the docs on the math.