diff --git a/experimental/canvaskit/canvaskit/example.html b/experimental/canvaskit/canvaskit/example.html index 0dc02750a9..8ec6c147bd 100644 --- a/experimental/canvaskit/canvaskit/example.html +++ b/experimental/canvaskit/canvaskit/example.html @@ -845,7 +845,7 @@ strokeWidth: 2, testFn: (ctx, x, y) => ctx.isPointInStroke(x * SCALE, y * SCALE), }, - ] + ]; for (let canvas of [skcanvas, realCanvas]) { let ctx = canvas.getContext('2d'); diff --git a/experimental/canvaskit/canvaskit_bindings.cpp b/experimental/canvaskit/canvaskit_bindings.cpp index 9798df67de..330e604119 100644 --- a/experimental/canvaskit/canvaskit_bindings.cpp +++ b/experimental/canvaskit/canvaskit_bindings.cpp @@ -149,6 +149,13 @@ SkMatrix toSkMatrix(const SimpleMatrix& sm) { sm.pers0 , sm.pers1 , sm.pers2); } +SimpleMatrix toSimpleSkMatrix(const SkMatrix& sm) { + SimpleMatrix m {sm[0], sm[1], sm[2], + sm[3], sm[4], sm[5], + sm[6], sm[7], sm[8]}; + return m; +} + struct SimpleImageInfo { int width; int height; @@ -640,6 +647,10 @@ EMSCRIPTEN_BINDINGS(Skia) { })) .function("drawVertices", select_overload&, SkBlendMode, const SkPaint&)>(&SkCanvas::drawVertices)) .function("flush", &SkCanvas::flush) + .function("getTotalMatrix", optional_override([](const SkCanvas& self)->SimpleMatrix { + SkMatrix m = self.getTotalMatrix(); + return toSimpleSkMatrix(m); + })) .function("_readPixels", optional_override([](SkCanvas& self, SimpleImageInfo di, uintptr_t /* uint8_t* */ pPtr, size_t dstRowBytes, int srcX, int srcY) { diff --git a/experimental/canvaskit/externs.js b/experimental/canvaskit/externs.js index dacc600c20..eaff31179f 100644 --- a/experimental/canvaskit/externs.js +++ b/experimental/canvaskit/externs.js @@ -77,9 +77,9 @@ var CanvasKit = { SkCanvas: { // public API (from C++ bindings) clear: function() {}, - concat: function() {}, - clipRect: function() {}, clipPath: function() {}, + clipRect: function() {}, + concat: function() {}, drawImage: function() {}, drawImageRect: function() {}, drawPaint: function() {}, @@ -89,6 +89,7 @@ var CanvasKit = { drawText: function() {}, drawVertices: function() {}, flush: function() {}, + getTotalMatrix: function() {}, restore: function() {}, rotate: function() {}, save: function() {}, diff --git a/experimental/canvaskit/htmlcanvas/canvas2d.js b/experimental/canvaskit/htmlcanvas/canvas2d.js index 4d8bdc7aad..62410fdc2f 100644 --- a/experimental/canvaskit/htmlcanvas/canvas2d.js +++ b/experimental/canvaskit/htmlcanvas/canvas2d.js @@ -275,7 +275,6 @@ var sx2 = pts[2]; var sy2 = pts[3]; - // Maybe refactor _scalefactor() on which this is taken? var sx = currentTransform[0]; var sy = currentTransform[4]; var scaleFactor = (Math.abs(sx) + Math.abs(sy))/2; @@ -418,13 +417,13 @@ 'f' : this._currentTransform[5], }; }, + // @param {DOMMatrix} matrix set: function(matrix) { if (matrix.a) { // if we see a property named 'a', guess that b-f will // also be there. - this._currentTransform = [matrix.a, matrix.c, matrix.e, - matrix.b, matrix.d, matrix.f, - 0, 0, 1]; + this.setTransform(matrix.a, matrix.b, matrix.c, + matrix.d, matrix.e, matrix.f); } } }); @@ -1268,7 +1267,8 @@ this._currentPath.transform(this._currentTransform); var inverted = CanvasKit.SkMatrix.invert(this._currentTransform); this._canvas.concat(inverted); - this._currentTransform = CanvasKit.SkMatrix.identity(); + // This should be identity, modulo floating point drift. + this._currentTransform = this._canvas.getTotalMatrix(); } this.restore = function() { @@ -1285,7 +1285,6 @@ ); this._currentPath.transform(combined); - this._currentTransform = newState.ctm; this._lineDashList = newState.ldl; this._strokeWidth = newState.sw; this._paint.setStrokeWidth(this._strokeWidth); @@ -1306,8 +1305,9 @@ this._imageFilterQuality = newState.isq; //TODO: font, textAlign, textBaseline, direction - // restores the clip + // restores the clip and ctm this._canvas.restore(); + this._currentTransform = this._canvas.getTotalMatrix(); } this.rotate = function(radians) { @@ -1318,10 +1318,8 @@ // path so it cancels out when we apply the transform at draw time. var inverted = CanvasKit.SkMatrix.rotated(-radians); this._currentPath.transform(inverted); - this._currentTransform = CanvasKit.SkMatrix.multiply( - this._currentTransform, - CanvasKit.SkMatrix.rotated(radians)); this._canvas.rotate(radiansToDegrees(radians), 0, 0); + this._currentTransform = this._canvas.getTotalMatrix(); } this.save = function() { @@ -1371,10 +1369,8 @@ // path so it cancels out when we apply the transform at draw time. var inverted = CanvasKit.SkMatrix.scaled(1/sx, 1/sy); this._currentPath.transform(inverted); - this._currentTransform = CanvasKit.SkMatrix.multiply( - this._currentTransform, - CanvasKit.SkMatrix.scaled(sx, sy)); this._canvas.scale(sx, sy); + this._currentTransform = this._canvas.getTotalMatrix(); } this.setLineDash = function(dashes) { @@ -1514,10 +1510,8 @@ // path so it cancels out when we apply the transform at draw time. var inverted = CanvasKit.SkMatrix.translated(-dx, -dy); this._currentPath.transform(inverted); - this._currentTransform = CanvasKit.SkMatrix.multiply( - this._currentTransform, - CanvasKit.SkMatrix.translated(dx, dy)); this._canvas.translate(dx, dy); + this._currentTransform = this._canvas.getTotalMatrix(); } this.transform = function(a, b, c, d, e, f) { @@ -1529,9 +1523,7 @@ var inverted = CanvasKit.SkMatrix.invert(newTransform); this._currentPath.transform(inverted); this._canvas.concat(newTransform); - this._currentTransform = CanvasKit.SkMatrix.multiply( - this._currentTransform, - newTransform); + this._currentTransform = this._canvas.getTotalMatrix(); } // Not supported operations (e.g. for Web only)