From 98d2d6ffe00ed67c1dae72cee3348093a683c834 Mon Sep 17 00:00:00 2001 From: Anton Lavrenov Date: Wed, 1 Aug 2018 09:26:13 +0700 Subject: [PATCH] better toDataURL for shapes --- CHANGELOG.md | 1 + src/Node.js | 13 +++++------ test/unit/BaseLayer-test.js | 2 ++ test/unit/Node-test.js | 45 ++++++++++++++++++++++++++++++++++++- test/unit/Stage-test.js | 3 ++- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a37307b..3809a708 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Some `Konva.Transformer` fixes * Typescript fixes * `stage.toDataURL()` fixes when it has hidden layers +* `shape.toDataURL()` automatically adjust position and size of resulted image ## [2.1.7][2018-07-03] diff --git a/src/Node.js b/src/Node.js index 44f4bd9a..8d82dc08 100644 --- a/src/Node.js +++ b/src/Node.js @@ -1582,17 +1582,16 @@ _toKonvaCanvas: function(config) { config = config || {}; + var box = this.getClientRect(); + var stage = this.getStage(), - x = config.x || 0, - y = config.y || 0, + x = config.x || box.x, + y = config.y || box.y, pixelRatio = config.pixelRatio || 1, canvas = new Konva.SceneCanvas({ - width: - config.width || this.getWidth() || (stage ? stage.getWidth() : 0), + width: config.width || box.width || (stage ? stage.getWidth() : 0), height: - config.height || - this.getHeight() || - (stage ? stage.getHeight() : 0), + config.height || box.height || (stage ? stage.getHeight() : 0), pixelRatio: pixelRatio }), context = canvas.getContext(); diff --git a/test/unit/BaseLayer-test.js b/test/unit/BaseLayer-test.js index b07eb0d6..eb535e71 100644 --- a/test/unit/BaseLayer-test.js +++ b/test/unit/BaseLayer-test.js @@ -1,6 +1,7 @@ suite('BaseLayer', function() { // ====================================================== test('width and height', function() { + Konva.showWarnings = false; var stage = addStage(); var layer = new Konva.FastLayer(); @@ -52,5 +53,6 @@ suite('BaseLayer', function() { stage.height(), 'while layer is on stage changing height doing nothing' ); + Konva.showWarnings = true; }); }); diff --git a/test/unit/Node-test.js b/test/unit/Node-test.js index a339bd43..12c54436 100644 --- a/test/unit/Node-test.js +++ b/test/unit/Node-test.js @@ -292,7 +292,7 @@ suite('Node', function() { // ====================================================== test('toDataURL + HDPI', function(done) { - this.timeout(5000); + // this.timeout(5000); var oldRatio = Konva.pixelRatio; Konva.pixelRatio = 2; @@ -338,6 +338,49 @@ suite('Node', function() { }); }); + // ====================================================== + test('toDataURL of moved shape', function() { + var stage = addStage(); + var layer = new Konva.Layer(); + stage.add(layer); + + var circle = new Konva.Circle({ + fill: 'green', + radius: 50 + }); + layer.add(circle); + + var oldURL = circle.toDataURL(); + + circle.x(100); + circle.y(100); + + var newURL = circle.toDataURL(); + assert.equal(oldURL, newURL); + }); + + // ====================================================== + test('toDataURL of transformer shape', function() { + var stage = addStage(); + var layer = new Konva.Layer(); + stage.add(layer); + + var text = new Konva.Text({ + fill: 'green', + text: 'hello, test', + rotation: 45 + }); + layer.add(text); + + var oldURL = text.toDataURL(); + + text.x(100); + text.y(100); + + var newURL = text.toDataURL(); + assert.equal(oldURL, newURL); + }); + // ====================================================== test("listen and don't listen", function() { var stage = addStage(); diff --git a/test/unit/Stage-test.js b/test/unit/Stage-test.js index 8c84a27f..b1a88e1b 100644 --- a/test/unit/Stage-test.js +++ b/test/unit/Stage-test.js @@ -1057,7 +1057,8 @@ suite('Stage', function() { }); layer.add(circle); stage.add(layer); - assert.equal(stage.toDataURL(), layer.toDataURL()); + + compareCanvases(stage.toCanvas(), layer.toCanvas()); }); test('toDataURL with hidden layer', function() {