perf fixes, transformer events

This commit is contained in:
Anton Lavrenov 2020-06-16 12:16:53 -05:00
parent 1d8388eead
commit e22a98d656
6 changed files with 73 additions and 44 deletions

View File

@ -8,7 +8,7 @@
* Konva JavaScript Framework v6.0.0 * Konva JavaScript Framework v6.0.0
* http://konvajs.org/ * http://konvajs.org/
* Licensed under the MIT * Licensed under the MIT
* Date: Wed Jun 10 2020 * Date: Tue Jun 16 2020
* *
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS) * Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
* Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva) * Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
@ -3277,24 +3277,27 @@
* }); * });
*/ */
Node.prototype.setAttrs = function (config) { Node.prototype.setAttrs = function (config) {
var key, method; var _this = this;
if (!config) { this._batchTransformChanges(function () {
return this; var key, method;
} if (!config) {
for (key in config) { return _this;
if (key === CHILDREN) {
continue;
} }
method = SET$1 + Util._capitalize(key); for (key in config) {
// use setter if available if (key === CHILDREN) {
if (Util._isFunction(this[method])) { continue;
this[method](config[key]); }
method = SET$1 + Util._capitalize(key);
// use setter if available
if (Util._isFunction(_this[method])) {
_this[method](config[key]);
}
else {
// otherwise set directly
_this._setAttr(key, config[key]);
}
} }
else { });
// otherwise set directly
this._setAttr(key, config[key]);
}
}
return this; return this;
}; };
/** /**
@ -14801,10 +14804,11 @@
Transformer.prototype._proxyDrag = function (node) { Transformer.prototype._proxyDrag = function (node) {
var _this = this; var _this = this;
var lastPos; var lastPos;
node.on("dragstart." + EVENTS_NAME, function () { node.on("dragstart." + EVENTS_NAME, function (e) {
lastPos = node.getAbsolutePosition(); lastPos = node.getAbsolutePosition();
_this.fire('dragstart', e);
}); });
node.on("dragmove." + EVENTS_NAME, function () { node.on("dragmove." + EVENTS_NAME, function (e) {
if (!lastPos) { if (!lastPos) {
return; return;
} }
@ -14824,6 +14828,11 @@
y: otherAbs.y + dy, y: otherAbs.y + dy,
}); });
otherNode.startDrag(); otherNode.startDrag();
// TODO: how to trigger event after all nodes are dragged?
_this.fire('dragmove', e);
});
node.on("dragend." + EVENTS_NAME, function (e) {
_this.fire('dragend', e);
}); });
lastPos = null; lastPos = null;
}); });

4
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -961,24 +961,26 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
* }); * });
*/ */
setAttrs(config: any) { setAttrs(config: any) {
var key, method; this._batchTransformChanges(() => {
var key, method;
if (!config) {
return this;
}
for (key in config) {
if (key === CHILDREN) {
continue;
}
method = SET + Util._capitalize(key);
// use setter if available
if (Util._isFunction(this[method])) {
this[method](config[key]);
} else {
// otherwise set directly
this._setAttr(key, config[key]);
}
}
});
if (!config) {
return this;
}
for (key in config) {
if (key === CHILDREN) {
continue;
}
method = SET + Util._capitalize(key);
// use setter if available
if (Util._isFunction(this[method])) {
this[method](config[key]);
} else {
// otherwise set directly
this._setAttr(key, config[key]);
}
}
return this; return this;
} }
/** /**

View File

@ -314,10 +314,11 @@ export class Transformer extends Group {
_proxyDrag(node: Node) { _proxyDrag(node: Node) {
let lastPos; let lastPos;
node.on(`dragstart.${EVENTS_NAME}`, () => { node.on(`dragstart.${EVENTS_NAME}`, (e) => {
lastPos = node.getAbsolutePosition(); lastPos = node.getAbsolutePosition();
this.fire('dragstart', e);
}); });
node.on(`dragmove.${EVENTS_NAME}`, () => { node.on(`dragmove.${EVENTS_NAME}`, (e) => {
if (!lastPos) { if (!lastPos) {
return; return;
} }
@ -337,6 +338,11 @@ export class Transformer extends Group {
y: otherAbs.y + dy, y: otherAbs.y + dy,
}); });
otherNode.startDrag(); otherNode.startDrag();
// TODO: how to trigger event after all nodes are dragged?
this.fire('dragmove', e);
});
node.on(`dragend.${EVENTS_NAME}`, (e) => {
this.fire('dragend', e);
}); });
lastPos = null; lastPos = null;
}); });

View File

@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<div id="container"></div> <div id="container"></div>
<script src="../../konva.min.js"></script> <script src="../../konva.js"></script>
<!-- <script src="https://unpkg.com/konva@6.0.0/konva.min.js"></script> --> <!-- <script src="https://unpkg.com/konva@6.0.0/konva.min.js"></script> -->
<script src="http://www.html5canvastutorials.com/lib/stats/stats.js"></script> <script src="http://www.html5canvastutorials.com/lib/stats/stats.js"></script>
<script defer="defer"> <script defer="defer">

View File

@ -3829,6 +3829,18 @@ suite('Transformer', function () {
var tr = new Konva.Transformer({ var tr = new Konva.Transformer({
nodes: [rect1, rect2], nodes: [rect1, rect2],
}); });
// make sure drag also triggers on the transformer.
tr.on('dragstart', () => {
dragstart += 1;
});
tr.on('dragmove', () => {
dragmove += 1;
});
tr.on('dragend', () => {
dragend += 1;
});
layer.add(tr); layer.add(tr);
layer.draw(); layer.draw();
@ -3849,9 +3861,9 @@ suite('Transformer', function () {
// proxy drag to other nodes // proxy drag to other nodes
assert.equal(rect2.x(), 105); assert.equal(rect2.x(), 105);
assert.equal(rect2.y(), 105); assert.equal(rect2.y(), 105);
assert.equal(dragstart, 1); assert.equal(dragstart, 2);
assert.equal(dragmove, 1); assert.equal(dragmove, 2);
assert.equal(dragend, 1); assert.equal(dragend, 2);
}); });
test('reattach from several and drag one', function () { test('reattach from several and drag one', function () {