fixed layer drawFunc bug. Now using call() for layer beforeDraw and afterDraw custom functions so that they are executed from the layer's context

This commit is contained in:
Eric Rowell
2012-05-19 22:18:33 -07:00
parent 7fcf5f156a
commit 97ddb507ea
3 changed files with 36 additions and 38 deletions

35
dist/kinetic-core.js vendored
View File

@@ -2315,7 +2315,6 @@ Kinetic.Layer = function(config) {
this.lastDrawTime = 0; this.lastDrawTime = 0;
this.beforeDrawFunc = undefined; this.beforeDrawFunc = undefined;
this.afterDrawFunc = undefined; this.afterDrawFunc = undefined;
this.drawFunc = undefined;
this.canvas = document.createElement('canvas'); this.canvas = document.createElement('canvas');
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d');
@@ -2338,11 +2337,11 @@ Kinetic.Layer.prototype = {
var date = new Date(); var date = new Date();
var time = date.getTime(); var time = date.getTime();
var timeDiff = time - this.lastDrawTime; var timeDiff = time - this.lastDrawTime;
var tt = 1000 / throttle; var tt = 1000 / throttle;
if(timeDiff >= tt) { if(timeDiff >= tt) {
this._draw(); this._draw();
if(this.drawTimeout !== undefined) { if(this.drawTimeout !== undefined) {
clearTimeout(this.drawTimeout); clearTimeout(this.drawTimeout);
this.drawTimeout = undefined; this.drawTimeout = undefined;
@@ -2354,12 +2353,12 @@ Kinetic.Layer.prototype = {
*/ */
else if(this.drawTimeout === undefined) { else if(this.drawTimeout === undefined) {
var that = this; var that = this;
/* /*
* wait 17ms before trying again (60fps) * wait 17ms before trying again (60fps)
*/ */
this.drawTimeout = setTimeout(function() { this.drawTimeout = setTimeout(function() {
that.draw(); that.draw();
}, 17); }, 17);
} }
}, },
/** /**
@@ -2429,29 +2428,29 @@ Kinetic.Layer.prototype = {
* private draw children * private draw children
*/ */
_draw: function() { _draw: function() {
var date = new Date(); var date = new Date();
var time = date.getTime(); var time = date.getTime();
this.lastDrawTime = time; this.lastDrawTime = time;
// before draw handler // before draw handler
if(this.beforeDrawFunc !== undefined) { if(this.beforeDrawFunc !== undefined) {
this.beforeDrawFunc(); this.beforeDrawFunc.call(this);
} }
this.clear(); this.clear();
if(this.attrs.visible) { if(this.attrs.visible) {
// draw custom func // draw custom func
if (this.drawFunc !== undefined) { if(this.attrs.drawFunc !== undefined) {
this.drawFunc(); this.attrs.drawFunc.call(this);
} }
// draw children // draw children
this._drawChildren(); this._drawChildren();
} }
// after draw handler // after draw handler
if(this.afterDrawFunc !== undefined) { if(this.afterDrawFunc !== undefined) {
this.afterDrawFunc(); this.afterDrawFunc.call(this);
} }
} }
}; };

File diff suppressed because one or more lines are too long

View File

@@ -18,7 +18,6 @@ Kinetic.Layer = function(config) {
this.lastDrawTime = 0; this.lastDrawTime = 0;
this.beforeDrawFunc = undefined; this.beforeDrawFunc = undefined;
this.afterDrawFunc = undefined; this.afterDrawFunc = undefined;
this.drawFunc = undefined;
this.canvas = document.createElement('canvas'); this.canvas = document.createElement('canvas');
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d');
@@ -41,11 +40,11 @@ Kinetic.Layer.prototype = {
var date = new Date(); var date = new Date();
var time = date.getTime(); var time = date.getTime();
var timeDiff = time - this.lastDrawTime; var timeDiff = time - this.lastDrawTime;
var tt = 1000 / throttle; var tt = 1000 / throttle;
if(timeDiff >= tt) { if(timeDiff >= tt) {
this._draw(); this._draw();
if(this.drawTimeout !== undefined) { if(this.drawTimeout !== undefined) {
clearTimeout(this.drawTimeout); clearTimeout(this.drawTimeout);
this.drawTimeout = undefined; this.drawTimeout = undefined;
@@ -57,12 +56,12 @@ Kinetic.Layer.prototype = {
*/ */
else if(this.drawTimeout === undefined) { else if(this.drawTimeout === undefined) {
var that = this; var that = this;
/* /*
* wait 17ms before trying again (60fps) * wait 17ms before trying again (60fps)
*/ */
this.drawTimeout = setTimeout(function() { this.drawTimeout = setTimeout(function() {
that.draw(); that.draw();
}, 17); }, 17);
} }
}, },
/** /**
@@ -132,29 +131,29 @@ Kinetic.Layer.prototype = {
* private draw children * private draw children
*/ */
_draw: function() { _draw: function() {
var date = new Date(); var date = new Date();
var time = date.getTime(); var time = date.getTime();
this.lastDrawTime = time; this.lastDrawTime = time;
// before draw handler // before draw handler
if(this.beforeDrawFunc !== undefined) { if(this.beforeDrawFunc !== undefined) {
this.beforeDrawFunc(); this.beforeDrawFunc.call(this);
} }
this.clear(); this.clear();
if(this.attrs.visible) { if(this.attrs.visible) {
// draw custom func // draw custom func
if (this.drawFunc !== undefined) { if(this.attrs.drawFunc !== undefined) {
this.drawFunc(); this.attrs.drawFunc.call(this);
} }
// draw children // draw children
this._drawChildren(); this._drawChildren();
} }
// after draw handler // after draw handler
if(this.afterDrawFunc !== undefined) { if(this.afterDrawFunc !== undefined) {
this.afterDrawFunc(); this.afterDrawFunc.call(this);
} }
} }
}; };