turns out there was no performance problems with the path (made a dumb mistake with implementation). However, during the analysis, I ended up adding event detection throttling which is pretty handy. It cuts down unecessary mousemove event handling dramatically and greatly improves performance when you have thousands of shapes

This commit is contained in:
Eric Rowell
2012-05-27 23:41:05 -07:00
parent cc35abd0f6
commit a2defa4bed
7 changed files with 95 additions and 66 deletions

View File

@@ -14,10 +14,12 @@
Kinetic.Stage = function(config) {
this.setDefaultAttrs({
width: 400,
height: 200
height: 200,
throttle: 80
});
this.nodeType = 'Stage';
this.lastEventTime = 0;
/*
* if container is a string, assume it's an id for
@@ -549,11 +551,26 @@ Kinetic.Stage.prototype = {
return false;
},
_handleStageEvent: function(evt) {
var throttle = this.attrs.throttle;
var date = new Date();
var time = date.getTime();
var timeDiff = time - this.lastEventTime;
var tt = 1000 / throttle;
if(timeDiff >= tt) {
this._handleStageEventContinue(evt);
}
},
/**
* handle incoming event
* @param {Event} evt
*/
_handleStageEvent: function(evt) {
_handleStageEventContinue: function(evt) {
var date = new Date();
var time = date.getTime();
this.lastEventTime = time;
var go = Kinetic.GlobalObject;
if(!evt) {
evt = window.event;
@@ -623,10 +640,11 @@ Kinetic.Stage.prototype = {
that._handleStageEvent(evt);
that.clickStart = false;
}, false);
this.content.addEventListener('mouseover', function(evt) {
that._handleStageEvent(evt);
}, false);
/*
this.content.addEventListener('mouseover', function(evt) {
that._handleStageEvent(evt);
}, false);
*/
this.content.addEventListener('mouseout', function(evt) {
// if there's a current target shape, run mouseout handlers
@@ -641,14 +659,14 @@ Kinetic.Stage.prototype = {
this.content.addEventListener('touchstart', function(evt) {
evt.preventDefault();
that.touchStart = true;
/*
* init stage drag and drop
*/
if(that.attrs.draggable) {
that._initDrag();
}
that._handleStageEvent(evt);
}, false);

View File

@@ -23,24 +23,20 @@ Kinetic.Path = function(config) {
var c = ca[n].command;
var p = ca[n].points;
switch(c) {
case 'M':
context.moveTo(p[0], p[1]);
break;
case 'L':
context.lineTo(p[0], p[1]);
break;
case 'M':
context.moveTo(p[0], p[1]);
break;
case 'z':
context.closePath();
break;
}
}
context.closePath();
//this.fill();
context.fillStyle = '#999';
context.fill();
context.strokeStyle = '#555';
context.stroke();
//this.stroke();
this.fill();
this.stroke();
};
// call super constructor
Kinetic.Shape.apply(this, [config]);