mirror of
https://github.com/konvajs/konva.git
synced 2025-06-27 20:03:08 +08:00
added layer throttling which greatly improves drag and drop and other mousemove drawing performance
This commit is contained in:
parent
04554e0e6a
commit
4e82139b74
32
dist/kinetic-core.js
vendored
32
dist/kinetic-core.js
vendored
@ -2089,8 +2089,15 @@ Kinetic.GlobalObject.extend(Kinetic.Stage, Kinetic.Node);
|
|||||||
* @param {Object} config
|
* @param {Object} config
|
||||||
*/
|
*/
|
||||||
Kinetic.Layer = function(config) {
|
Kinetic.Layer = function(config) {
|
||||||
|
// default attrs
|
||||||
|
if(this.attrs === undefined) {
|
||||||
|
this.attrs = {};
|
||||||
|
}
|
||||||
|
this.attrs.throttle = 12;
|
||||||
|
|
||||||
this.nodeType = 'Layer';
|
this.nodeType = 'Layer';
|
||||||
|
this.lastDrawTime = 0;
|
||||||
|
|
||||||
this.canvas = document.createElement('canvas');
|
this.canvas = document.createElement('canvas');
|
||||||
this.context = this.canvas.getContext('2d');
|
this.context = this.canvas.getContext('2d');
|
||||||
this.canvas.style.position = 'absolute';
|
this.canvas.style.position = 'absolute';
|
||||||
@ -2108,7 +2115,28 @@ Kinetic.Layer.prototype = {
|
|||||||
* or shapes
|
* or shapes
|
||||||
*/
|
*/
|
||||||
draw: function() {
|
draw: function() {
|
||||||
this._draw();
|
var throttle = this.attrs.throttle;
|
||||||
|
var date = new Date();
|
||||||
|
var time = date.getTime();
|
||||||
|
var timeDiff = time - this.lastDrawTime;
|
||||||
|
|
||||||
|
if(timeDiff >= throttle) {
|
||||||
|
this._draw();
|
||||||
|
this.lastDrawTime = time;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* set throttle
|
||||||
|
* @param {Number} throttle in ms
|
||||||
|
*/
|
||||||
|
setThrottle: function(throttle) {
|
||||||
|
this.attrs.throttle = throttle;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* get throttle
|
||||||
|
*/
|
||||||
|
getThrottle: function() {
|
||||||
|
return this.attrs.throttle;
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* clears the canvas context tied to the layer. Clearing
|
* clears the canvas context tied to the layer. Clearing
|
||||||
|
32
src/Layer.js
32
src/Layer.js
@ -10,8 +10,15 @@
|
|||||||
* @param {Object} config
|
* @param {Object} config
|
||||||
*/
|
*/
|
||||||
Kinetic.Layer = function(config) {
|
Kinetic.Layer = function(config) {
|
||||||
|
// default attrs
|
||||||
|
if(this.attrs === undefined) {
|
||||||
|
this.attrs = {};
|
||||||
|
}
|
||||||
|
this.attrs.throttle = 12;
|
||||||
|
|
||||||
this.nodeType = 'Layer';
|
this.nodeType = 'Layer';
|
||||||
|
this.lastDrawTime = 0;
|
||||||
|
|
||||||
this.canvas = document.createElement('canvas');
|
this.canvas = document.createElement('canvas');
|
||||||
this.context = this.canvas.getContext('2d');
|
this.context = this.canvas.getContext('2d');
|
||||||
this.canvas.style.position = 'absolute';
|
this.canvas.style.position = 'absolute';
|
||||||
@ -29,7 +36,28 @@ Kinetic.Layer.prototype = {
|
|||||||
* or shapes
|
* or shapes
|
||||||
*/
|
*/
|
||||||
draw: function() {
|
draw: function() {
|
||||||
this._draw();
|
var throttle = this.attrs.throttle;
|
||||||
|
var date = new Date();
|
||||||
|
var time = date.getTime();
|
||||||
|
var timeDiff = time - this.lastDrawTime;
|
||||||
|
|
||||||
|
if(timeDiff >= throttle) {
|
||||||
|
this._draw();
|
||||||
|
this.lastDrawTime = time;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* set throttle
|
||||||
|
* @param {Number} throttle in ms
|
||||||
|
*/
|
||||||
|
setThrottle: function(throttle) {
|
||||||
|
this.attrs.throttle = throttle;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* get throttle
|
||||||
|
*/
|
||||||
|
getThrottle: function() {
|
||||||
|
return this.attrs.throttle;
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* clears the canvas context tied to the layer. Clearing
|
* clears the canvas context tied to the layer. Clearing
|
||||||
|
@ -193,7 +193,9 @@ Test.prototype.tests = {
|
|||||||
width: 578,
|
width: 578,
|
||||||
height: 200
|
height: 200
|
||||||
});
|
});
|
||||||
var layer = new Kinetic.Layer();
|
var layer = new Kinetic.Layer({
|
||||||
|
throttle: 100
|
||||||
|
});
|
||||||
var rect = new Kinetic.Rect({
|
var rect = new Kinetic.Rect({
|
||||||
x: 200,
|
x: 200,
|
||||||
y: 100,
|
y: 100,
|
||||||
@ -1225,7 +1227,9 @@ Test.prototype.tests = {
|
|||||||
width: 578,
|
width: 578,
|
||||||
height: 200
|
height: 200
|
||||||
});
|
});
|
||||||
var layer = new Kinetic.Layer();
|
var layer = new Kinetic.Layer({
|
||||||
|
throttle: 12
|
||||||
|
});
|
||||||
var circle = new Kinetic.Circle({
|
var circle = new Kinetic.Circle({
|
||||||
x: stage.getWidth() / 2,
|
x: stage.getWidth() / 2,
|
||||||
y: stage.getHeight() / 2,
|
y: stage.getHeight() / 2,
|
||||||
|
@ -88,9 +88,7 @@ Test.prototype.tests = {
|
|||||||
group.add(circle);
|
group.add(circle);
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
//console.log(stage.toJSON())
|
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":12,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}';
|
||||||
|
|
||||||
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}';
|
|
||||||
test(stage.toJSON() === expectedJson, 'problem with serialization');
|
test(stage.toJSON() === expectedJson, 'problem with serialization');
|
||||||
},
|
},
|
||||||
'STAGE - test getAttrs()': function(containerId) {
|
'STAGE - test getAttrs()': function(containerId) {
|
||||||
@ -144,7 +142,7 @@ Test.prototype.tests = {
|
|||||||
height: 200
|
height: 200
|
||||||
});
|
});
|
||||||
|
|
||||||
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}';
|
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":12,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}';
|
||||||
stage.load(json);
|
stage.load(json);
|
||||||
|
|
||||||
test(stage.toJSON() === json, "serialized stage is incorrect");
|
test(stage.toJSON() === json, "serialized stage is incorrect");
|
||||||
@ -180,7 +178,7 @@ Test.prototype.tests = {
|
|||||||
group.add(triangle);
|
group.add(triangle);
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
|
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":12,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
|
||||||
test(stage.toJSON() === expectedJson, "problem with serialization");
|
test(stage.toJSON() === expectedJson, "problem with serialization");
|
||||||
},
|
},
|
||||||
'STAGE - load stage with custom shape using json': function(containerId) {
|
'STAGE - load stage with custom shape using json': function(containerId) {
|
||||||
@ -199,7 +197,7 @@ Test.prototype.tests = {
|
|||||||
context.closePath();
|
context.closePath();
|
||||||
this.fillStroke();
|
this.fillStroke();
|
||||||
};
|
};
|
||||||
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
|
var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":12,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"myTriangle"},"nodeType":"Shape"}]}]}]}';
|
||||||
stage.load(json);
|
stage.load(json);
|
||||||
|
|
||||||
var customShape = stage.get('#myTriangle')[0];
|
var customShape = stage.get('#myTriangle')[0];
|
||||||
@ -832,7 +830,8 @@ Test.prototype.tests = {
|
|||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
|
|
||||||
var json = stage.toJSON();
|
var json = stage.toJSON();
|
||||||
test(json === '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"crop":{"x":0,"y":0},"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":200,"y":60,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":50,"y":150},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"darth"},"nodeType":"Shape","shapeType":"Image"}]}]}', 'problem serializing stage');
|
|
||||||
|
test(json === '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"throttle":12,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"crop":{"x":0,"y":0},"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":200,"y":60,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":50,"y":150},"dragConstraint":"none","dragBounds":{},"draggable":false,"id":"darth"},"nodeType":"Shape","shapeType":"Image"}]}]}');
|
||||||
};
|
};
|
||||||
imageObj.src = '../darth-vader.jpg';
|
imageObj.src = '../darth-vader.jpg';
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user