mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 04:42:02 +08:00
made isPointInShape() a public method for the purpose of collision detection
This commit is contained in:
parent
c698005adc
commit
78e4022126
46
dist/kinetic-core.js
vendored
46
dist/kinetic-core.js
vendored
@ -3,7 +3,7 @@
|
|||||||
* http://www.kineticjs.com/
|
* http://www.kineticjs.com/
|
||||||
* Copyright 2012, Eric Rowell
|
* Copyright 2012, Eric Rowell
|
||||||
* Licensed under the MIT or GPL Version 2 licenses.
|
* Licensed under the MIT or GPL Version 2 licenses.
|
||||||
* Date: Apr 14 2012
|
* Date: Apr 15 2012
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 - 2012 by Eric Rowell
|
* Copyright (C) 2011 - 2012 by Eric Rowell
|
||||||
*
|
*
|
||||||
@ -1492,7 +1492,7 @@ Kinetic.Stage.prototype = {
|
|||||||
this.targetFound = true;
|
this.targetFound = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(shape.attrs.visible && pos !== undefined && shape._isPointInShape(pos)) {
|
if(shape.attrs.visible && pos !== undefined && shape.isPointInShape(pos)) {
|
||||||
// handle onmousedown
|
// handle onmousedown
|
||||||
if(!isDragging && this.mouseDown) {
|
if(!isDragging && this.mouseDown) {
|
||||||
this.mouseDown = false;
|
this.mouseDown = false;
|
||||||
@ -2288,6 +2288,27 @@ Kinetic.Shape.prototype = {
|
|||||||
clearData: function() {
|
clearData: function() {
|
||||||
this.data = [];
|
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
|
* draw shape
|
||||||
* @param {Layer} layer Layer that the shape will be drawn on
|
* @param {Layer} layer Layer that the shape will be drawn on
|
||||||
@ -2327,27 +2348,6 @@ Kinetic.Shape.prototype = {
|
|||||||
this.drawFunc.call(this);
|
this.drawFunc.call(this);
|
||||||
context.restore();
|
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
|
// extend Node
|
||||||
|
4
dist/kinetic-core.min.js
vendored
4
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
42
src/Shape.js
42
src/Shape.js
@ -173,6 +173,27 @@ Kinetic.Shape.prototype = {
|
|||||||
clearData: function() {
|
clearData: function() {
|
||||||
this.data = [];
|
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
|
* draw shape
|
||||||
* @param {Layer} layer Layer that the shape will be drawn on
|
* @param {Layer} layer Layer that the shape will be drawn on
|
||||||
@ -212,27 +233,6 @@ Kinetic.Shape.prototype = {
|
|||||||
this.drawFunc.call(this);
|
this.drawFunc.call(this);
|
||||||
context.restore();
|
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
|
// extend Node
|
||||||
|
@ -364,7 +364,7 @@ Kinetic.Stage.prototype = {
|
|||||||
this.targetFound = true;
|
this.targetFound = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(shape.attrs.visible && pos !== undefined && shape._isPointInShape(pos)) {
|
if(shape.attrs.visible && pos !== undefined && shape.isPointInShape(pos)) {
|
||||||
// handle onmousedown
|
// handle onmousedown
|
||||||
if(!isDragging && this.mouseDown) {
|
if(!isDragging && this.mouseDown) {
|
||||||
this.mouseDown = false;
|
this.mouseDown = false;
|
||||||
|
@ -1281,6 +1281,51 @@ Test.prototype.tests = {
|
|||||||
star.setDetectionType('pixel');
|
star.setDetectionType('pixel');
|
||||||
test(star.getDetectionType() === 'pixel', 'detection type should be pixel');
|
test(star.getDetectionType() === 'pixel', 'detection type should be pixel');
|
||||||
},
|
},
|
||||||
|
'SHAPES - test isPointInPath()': function(containerId) {
|
||||||
|
var stage = new Kinetic.Stage({
|
||||||
|
container: containerId,
|
||||||
|
width: 578,
|
||||||
|
height: 200
|
||||||
|
});
|
||||||
|
var layer = new Kinetic.Layer();
|
||||||
|
var rect = new Kinetic.Rect({
|
||||||
|
x: 200,
|
||||||
|
y: 100,
|
||||||
|
width: 100,
|
||||||
|
height: 50,
|
||||||
|
fill: 'green',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4
|
||||||
|
});
|
||||||
|
|
||||||
|
layer.add(rect);
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
test(rect.isPointInShape({
|
||||||
|
x: 200,
|
||||||
|
y: 100
|
||||||
|
}) === true, 'problem with point in shape');
|
||||||
|
|
||||||
|
test(rect.isPointInShape({
|
||||||
|
x: 199,
|
||||||
|
y: 99
|
||||||
|
}) === false, 'problem with point in shape');
|
||||||
|
|
||||||
|
test(rect.isPointInShape({
|
||||||
|
x: 250,
|
||||||
|
y: 125
|
||||||
|
}) === true, 'problem with point in shape');
|
||||||
|
|
||||||
|
test(rect.isPointInShape({
|
||||||
|
x: 300,
|
||||||
|
y: 150
|
||||||
|
}) === true, 'problem with point in shape');
|
||||||
|
|
||||||
|
test(rect.isPointInShape({
|
||||||
|
x: 301,
|
||||||
|
y: 151
|
||||||
|
}) === false, 'problem with point in shape');
|
||||||
|
},
|
||||||
'Text - add text': function(containerId) {
|
'Text - add text': function(containerId) {
|
||||||
var stage = new Kinetic.Stage({
|
var stage = new Kinetic.Stage({
|
||||||
container: containerId,
|
container: containerId,
|
||||||
|
Loading…
Reference in New Issue
Block a user