made isPointInShape() a public method for the purpose of collision detection

This commit is contained in:
Eric Rowell
2012-04-15 09:18:30 -07:00
parent c698005adc
commit 78e4022126
5 changed files with 92 additions and 47 deletions

View File

@@ -173,6 +173,27 @@ Kinetic.Shape.prototype = {
clearData: function() {
this.data = [];
},
/**
* custom isPointInPath method which can use path detection
* or pixel detection
*/
isPointInShape: function(pos) {
var stage = this.getStage();
if(this.attrs.detectionType === 'path') {
var pathLayer = stage.pathLayer;
var pathLayerContext = pathLayer.getContext();
this._draw(pathLayer);
return pathLayerContext.isPointInPath(pos.x, pos.y);
}
else {
var w = stage.attrs.width;
var alpha = this.data[((w * pos.y) + pos.x) * 4 + 3];
return (alpha !== undefined && alpha !== 0);
}
},
/**
* draw shape
* @param {Layer} layer Layer that the shape will be drawn on
@@ -212,27 +233,6 @@ Kinetic.Shape.prototype = {
this.drawFunc.call(this);
context.restore();
}
},
/**
* custom isPointInPath method which can use path detection
* or pixel detection
*/
_isPointInShape: function(pos) {
var stage = this.getStage();
if(this.attrs.detectionType === 'path') {
var pathLayer = stage.pathLayer;
var pathLayerContext = pathLayer.getContext();
this._draw(pathLayer);
return pathLayerContext.isPointInPath(pos.x, pos.y);
}
else {
var w = stage.attrs.width;
var alpha = this.data[((w * pos.y) + pos.x) * 4 + 3];
return (alpha !== undefined && alpha !== 0);
}
}
};
// extend Node

View File

@@ -364,7 +364,7 @@ Kinetic.Stage.prototype = {
this.targetFound = true;
}
if(shape.attrs.visible && pos !== undefined && shape._isPointInShape(pos)) {
if(shape.attrs.visible && pos !== undefined && shape.isPointInShape(pos)) {
// handle onmousedown
if(!isDragging && this.mouseDown) {
this.mouseDown = false;