mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
dynamically set drag time interval to determine the optimal dragging performance. Makes dragging and dropping even smoother and responsive
This commit is contained in:
parent
328f457512
commit
b7f14ca821
26
dist/kinetic-core.js
vendored
26
dist/kinetic-core.js
vendored
@ -3,7 +3,7 @@
|
|||||||
* http://www.kineticjs.com/
|
* http://www.kineticjs.com/
|
||||||
* Copyright 2012, Eric Rowell
|
* Copyright 2012, Eric Rowell
|
||||||
* Licensed under the MIT or GPL Version 2 licenses.
|
* Licensed under the MIT or GPL Version 2 licenses.
|
||||||
* Date: Apr 08 2012
|
* Date: Apr 12 2012
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 - 2012 by Eric Rowell
|
* Copyright (C) 2011 - 2012 by Eric Rowell
|
||||||
*
|
*
|
||||||
@ -44,6 +44,8 @@ Kinetic.GlobalObject = {
|
|||||||
tempNodes: [],
|
tempNodes: [],
|
||||||
animations: [],
|
animations: [],
|
||||||
animIdCounter: 0,
|
animIdCounter: 0,
|
||||||
|
dragTimeInterval: 0,
|
||||||
|
maxDragTimeInterval: 15,
|
||||||
frame: {
|
frame: {
|
||||||
time: 0,
|
time: 0,
|
||||||
timeDiff: 0,
|
timeDiff: 0,
|
||||||
@ -55,7 +57,8 @@ Kinetic.GlobalObject = {
|
|||||||
offset: {
|
offset: {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0
|
y: 0
|
||||||
}
|
},
|
||||||
|
lastDrawTime: 0
|
||||||
},
|
},
|
||||||
extend: function(obj1, obj2) {
|
extend: function(obj1, obj2) {
|
||||||
for(var key in obj2.prototype) {
|
for(var key in obj2.prototype) {
|
||||||
@ -1193,7 +1196,6 @@ Kinetic.Stage.prototype = {
|
|||||||
|
|
||||||
function addLayer(n) {
|
function addLayer(n) {
|
||||||
var dataURL = layers[n].getCanvas().toDataURL();
|
var dataURL = layers[n].getCanvas().toDataURL();
|
||||||
console.log(dataURL);
|
|
||||||
var imageObj = new Image();
|
var imageObj = new Image();
|
||||||
imageObj.onload = function() {
|
imageObj.onload = function() {
|
||||||
bufferContext.drawImage(this, 0, 0);
|
bufferContext.drawImage(this, 0, 0);
|
||||||
@ -1741,10 +1743,27 @@ Kinetic.Stage.prototype = {
|
|||||||
var go = Kinetic.GlobalObject;
|
var go = Kinetic.GlobalObject;
|
||||||
var node = go.drag.node;
|
var node = go.drag.node;
|
||||||
if(node) {
|
if(node) {
|
||||||
|
var date = new Date();
|
||||||
|
var time = date.getTime();
|
||||||
|
|
||||||
|
if(time - go.drag.lastDrawTime > go.dragTimeInterval) {
|
||||||
|
go.drag.lastDrawTime = time;
|
||||||
|
|
||||||
var pos = that.getUserPosition();
|
var pos = that.getUserPosition();
|
||||||
var dc = node.attrs.dragConstraint;
|
var dc = node.attrs.dragConstraint;
|
||||||
var db = node.attrs.dragBounds;
|
var db = node.attrs.dragBounds;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* handle dynamice drag time interval. As the distance between
|
||||||
|
* the mouse and cursor increases, we need to increase the drag
|
||||||
|
* time interval to reduce the number of layer draws so that
|
||||||
|
* the node position can catch back up to the cursor. When the difference
|
||||||
|
* is zero, the time interval is zero. When the difference approahces
|
||||||
|
* infinity, the time interval approaches the max drag time interval
|
||||||
|
*/
|
||||||
|
var dragDiff = Math.abs(pos.x - node.attrs.x);
|
||||||
|
go.dragTimeInterval = go.maxDragTimeInterval * dragDiff / (dragDiff + 1);
|
||||||
|
|
||||||
// default
|
// default
|
||||||
var newNodePos = {
|
var newNodePos = {
|
||||||
x: pos.x - go.drag.offset.x,
|
x: pos.x - go.drag.offset.x,
|
||||||
@ -1817,6 +1836,7 @@ Kinetic.Stage.prototype = {
|
|||||||
// execute user defined ondragmove if defined
|
// execute user defined ondragmove if defined
|
||||||
go.drag.node._handleEvents('ondragmove', evt);
|
go.drag.node._handleEvents('ondragmove', evt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
this.onContent('mouseup touchend mouseout', function(evt) {
|
this.onContent('mouseup touchend mouseout', function(evt) {
|
||||||
|
6
dist/kinetic-core.min.js
vendored
6
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@ -16,6 +16,8 @@ Kinetic.GlobalObject = {
|
|||||||
tempNodes: [],
|
tempNodes: [],
|
||||||
animations: [],
|
animations: [],
|
||||||
animIdCounter: 0,
|
animIdCounter: 0,
|
||||||
|
dragTimeInterval: 0,
|
||||||
|
maxDragTimeInterval: 15,
|
||||||
frame: {
|
frame: {
|
||||||
time: 0,
|
time: 0,
|
||||||
timeDiff: 0,
|
timeDiff: 0,
|
||||||
@ -27,7 +29,8 @@ Kinetic.GlobalObject = {
|
|||||||
offset: {
|
offset: {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0
|
y: 0
|
||||||
}
|
},
|
||||||
|
lastDrawTime: 0
|
||||||
},
|
},
|
||||||
extend: function(obj1, obj2) {
|
extend: function(obj1, obj2) {
|
||||||
for(var key in obj2.prototype) {
|
for(var key in obj2.prototype) {
|
||||||
|
19
src/Stage.js
19
src/Stage.js
@ -161,7 +161,6 @@ Kinetic.Stage.prototype = {
|
|||||||
|
|
||||||
function addLayer(n) {
|
function addLayer(n) {
|
||||||
var dataURL = layers[n].getCanvas().toDataURL();
|
var dataURL = layers[n].getCanvas().toDataURL();
|
||||||
console.log(dataURL);
|
|
||||||
var imageObj = new Image();
|
var imageObj = new Image();
|
||||||
imageObj.onload = function() {
|
imageObj.onload = function() {
|
||||||
bufferContext.drawImage(this, 0, 0);
|
bufferContext.drawImage(this, 0, 0);
|
||||||
@ -709,10 +708,27 @@ Kinetic.Stage.prototype = {
|
|||||||
var go = Kinetic.GlobalObject;
|
var go = Kinetic.GlobalObject;
|
||||||
var node = go.drag.node;
|
var node = go.drag.node;
|
||||||
if(node) {
|
if(node) {
|
||||||
|
var date = new Date();
|
||||||
|
var time = date.getTime();
|
||||||
|
|
||||||
|
if(time - go.drag.lastDrawTime > go.dragTimeInterval) {
|
||||||
|
go.drag.lastDrawTime = time;
|
||||||
|
|
||||||
var pos = that.getUserPosition();
|
var pos = that.getUserPosition();
|
||||||
var dc = node.attrs.dragConstraint;
|
var dc = node.attrs.dragConstraint;
|
||||||
var db = node.attrs.dragBounds;
|
var db = node.attrs.dragBounds;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* handle dynamice drag time interval. As the distance between
|
||||||
|
* the mouse and cursor increases, we need to increase the drag
|
||||||
|
* time interval to reduce the number of layer draws so that
|
||||||
|
* the node position can catch back up to the cursor. When the difference
|
||||||
|
* is zero, the time interval is zero. When the difference approahces
|
||||||
|
* infinity, the time interval approaches the max drag time interval
|
||||||
|
*/
|
||||||
|
var dragDiff = Math.abs(pos.x - node.attrs.x);
|
||||||
|
go.dragTimeInterval = go.maxDragTimeInterval * dragDiff / (dragDiff + 1);
|
||||||
|
|
||||||
// default
|
// default
|
||||||
var newNodePos = {
|
var newNodePos = {
|
||||||
x: pos.x - go.drag.offset.x,
|
x: pos.x - go.drag.offset.x,
|
||||||
@ -785,6 +801,7 @@ Kinetic.Stage.prototype = {
|
|||||||
// execute user defined ondragmove if defined
|
// execute user defined ondragmove if defined
|
||||||
go.drag.node._handleEvents('ondragmove', evt);
|
go.drag.node._handleEvents('ondragmove', evt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
this.onContent('mouseup touchend mouseout', function(evt) {
|
this.onContent('mouseup touchend mouseout', function(evt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user