mirror of
https://github.com/konvajs/konva.git
synced 2025-12-04 19:08:24 +08:00
made isPointInShape() a public method for the purpose of collision detection
This commit is contained in:
42
src/Shape.js
42
src/Shape.js
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user