transformer events fixes, fix readme

This commit is contained in:
Anton Lavrenov 2020-02-24 20:39:32 -05:00
parent 801509febc
commit 7ec68fc501
5 changed files with 161 additions and 1371 deletions

View File

@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## Not released: ## Not released:
* Events fixes for `Konva.Transformer`
* Now `Konva` will keep `id` in a cloned node * Now `Konva` will keep `id` in a cloned node
## 4.1.5 - 2020-02-16 ## 4.1.5 - 2020-02-16

View File

@ -79,15 +79,7 @@ Konva supports UMD loading. So you can use all possible variants to load the fra
You can also use a CDN: [https://unpkg.com/konva@^4.0.3/konva.js](https://unpkg.com/konva@^4.0.3/konva.js) You can also use a CDN: [https://unpkg.com/konva@^4.0.3/konva.js](https://unpkg.com/konva@^4.0.3/konva.js)
### 2 Load via AMD (requirejs): ### 2 Install with npm:
```javascript
define(['./konva'], function(Konva) {
// your code
});
```
### 3 Load with npm:
```bash ```bash
npm install konva --save npm install konva --save
@ -97,18 +89,33 @@ npm install konva --save
// The old way (e.g. a CommonJS-style import) // The old way (e.g. a CommonJS-style import)
var Konva = require('konva'); var Konva = require('konva');
// The modern way (e.g. an ES6-style import) // The modern way (e.g. an ES6-style import for webpack, parcel)
import Konva from 'konva'; import Konva from 'konva';
// TypeScript
import Konva from 'konva'; // Allows e.g. "Konva.Stage"
// Or:
import * as Konva from 'konva'; // Allows e.g. "Konva.default.Stage"
// Note that for both TypeScript options, you must supply the TypeScript compiler with the
// "dom" library flag and the "es2015" library flag (or greater)
``` ```
### 4 Minimal bundle #### Typescript usage
Add DOM definitions into your `tsconfig.json` and set `esModuleInterop` to `true`:
```
{
"compilerOptions": {
"esModuleInterop": true,
"lib": [
"es6",
"dom"
]
}
}
```
Then use it:
```javascript
import Konva from 'konva';
```
### 3 Minimal bundle
```javascript ```javascript
import Konva from 'konva/lib/Core'; import Konva from 'konva/lib/Core';
@ -128,9 +135,7 @@ var shape = new Konva.Rect();
import { Blur } from 'konva/lib/filters/Blur'; import { Blur } from 'konva/lib/filters/Blur';
``` ```
### 4 NodeJS env
### 5 NodeJS
We are using [node-canvas](https://github.com/Automattic/node-canvas) to create canvas element. We are using [node-canvas](https://github.com/Automattic/node-canvas) to create canvas element.
Please check installation instructions for it. Then just run Please check installation instructions for it. Then just run

1385
konva.js

File diff suppressed because it is too large Load Diff

View File

@ -426,8 +426,8 @@ export class Transformer extends Group {
this._transforming = true; this._transforming = true;
this._fire('transformstart', { evt: e }); this._fire('transformstart', { evt: e, target: this.getNode() });
this.getNode()._fire('transformstart', { evt: e }); this.getNode()._fire('transformstart', { evt: e, target: this.getNode() });
} }
_handleMouseMove(e) { _handleMouseMove(e) {
var x, y, newHypotenuse; var x, y, newHypotenuse;
@ -740,8 +740,8 @@ export class Transformer extends Group {
y: newAttrs.y - (dy * Math.cos(rotation) + dx * Math.sin(rotation)) y: newAttrs.y - (dy * Math.cos(rotation) + dx * Math.sin(rotation))
}); });
this._fire('transform', { evt: evt }); this._fire('transform', { evt: evt, target: this.getNode() });
this.getNode()._fire('transform', { evt: evt }); this.getNode()._fire('transform', { evt: evt, target: this.getNode() });
this.update(); this.update();
this.getLayer().batchDraw(); this.getLayer().batchDraw();
} }

View File

@ -1499,6 +1499,81 @@ suite('Transformer', function() {
}); });
}); });
test.only('transform events check', function() {
var stage = addStage();
var layer = new Konva.Layer();
stage.add(layer);
var rect = new Konva.Rect({
x: 50,
y: 50,
draggable: true,
width: 100,
height: 100,
fill: 'yellow'
});
layer.add(rect);
var tr = new Konva.Transformer({
node: rect
});
layer.add(tr);
layer.draw();
var callCount = 0;
rect.on('transformstart', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
rect.on('transform', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
rect.on('transformend', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
tr.on('transformstart', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
tr.on('transform', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
tr.on('transformend', function(e) {
callCount += 1;
assert.equal(e.target, rect);
});
stage.simulateMouseDown({
x: 50,
y: 50
});
var top = stage.content.getBoundingClientRect().top;
tr._handleMouseMove({
target: tr.findOne('.top-left'),
clientX: 60,
clientY: 60 + top
});
tr._handleMouseUp({
clientX: 60,
clientY: 60 + top
});
stage.simulateMouseUp({
x: 60,
y: 60
});
assert.equal(callCount, 6);
});
test('on force update should clear transform', function() { test('on force update should clear transform', function() {
var stage = addStage(); var stage = addStage();
var layer = new Konva.Layer(); var layer = new Konva.Layer();
@ -1867,8 +1942,7 @@ suite('Transformer', function() {
var rect = new Konva.Rect({ var rect = new Konva.Rect({
draggable: true, draggable: true,
fill: 'yellow', fill: 'yellow'
}); });
layer.add(rect); layer.add(rect);
@ -1893,7 +1967,7 @@ suite('Transformer', function() {
// stage.simulateMouseDown({x: 0, y: 0}); // stage.simulateMouseDown({x: 0, y: 0});
var target = stage.getIntersection({ x: 0, y: 0}); var target = stage.getIntersection({ x: 0, y: 0 });
var top = stage.content.getBoundingClientRect().top; var top = stage.content.getBoundingClientRect().top;
throw 11; throw 11;
debugger; debugger;
@ -1920,14 +1994,8 @@ suite('Transformer', function() {
}); });
layer.draw(); layer.draw();
assert.equal( assert.equal(rect.width() * rect.scaleX(), -100);
rect.width() * rect.scaleX(), assert.equal(rect.height() * rect.scaleY(), -100);
-100
);
assert.equal(
rect.height() * rect.scaleY(),
-100,
);
throw 1; throw 1;
}); });
@ -2633,7 +2701,6 @@ suite('Transformer', function() {
y: 50 y: 50
}); });
assert.equal(rect.width() * rect.scaleX(), 200); assert.equal(rect.width() * rect.scaleX(), 200);
}); });
}); });