mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
improved event edge detection algo
This commit is contained in:
parent
96a9bd2248
commit
658064b5ef
28
src/Layer.js
28
src/Layer.js
@ -44,22 +44,26 @@
|
||||
* method for determining if a point intersects a shape or not
|
||||
* @method
|
||||
* @memberof Kinetic.Layer.prototype
|
||||
* @param {Kinetic.Shape} shape
|
||||
* @param {Kinetic.Shape|null} shape
|
||||
*/
|
||||
getIntersection: function() {
|
||||
var pos = Kinetic.Util._getXY(Array.prototype.slice.call(arguments)),
|
||||
shape, i, intersectionOffset;
|
||||
obj, i, intersectionOffset, shape;
|
||||
|
||||
if(this.isVisible()) {
|
||||
for (i=0; i<INTERSECTION_OFFSETS_LEN; i++) {
|
||||
intersectionOffset = INTERSECTION_OFFSETS[i];
|
||||
shape = this._getIntersection({
|
||||
obj = this._getIntersection({
|
||||
x: pos.x + intersectionOffset.x,
|
||||
y: pos.y + intersectionOffset.y
|
||||
});
|
||||
shape = obj.shape;
|
||||
if (shape) {
|
||||
return shape;
|
||||
}
|
||||
else if (!obj.antialiased) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -68,16 +72,26 @@
|
||||
},
|
||||
_getIntersection: function(pos) {
|
||||
var p = this.hitCanvas.context._context.getImageData(pos.x, pos.y, 1, 1).data,
|
||||
p3 = p[3],
|
||||
colorKey, shape;
|
||||
|
||||
// this indicates that a hit pixel may have been found
|
||||
if(p[3] === 255) {
|
||||
// fully opaque pixel
|
||||
if(p3 === 255) {
|
||||
colorKey = Kinetic.Util._rgbToHex(p[0], p[1], p[2]);
|
||||
shape = Kinetic.shapes[HASH + colorKey];
|
||||
return shape;
|
||||
return {
|
||||
shape: shape
|
||||
};
|
||||
}
|
||||
// antialiased pixel
|
||||
else if(p3 > 0) {
|
||||
return {
|
||||
antialiased: true
|
||||
};
|
||||
}
|
||||
// empty pixel
|
||||
else {
|
||||
return null
|
||||
return {};
|
||||
}
|
||||
},
|
||||
drawScene: function(canvas) {
|
||||
|
@ -217,6 +217,8 @@ suite('Stage', function() {
|
||||
assert.equal(stage.getIntersection(371, 93).getId(), 'greenCircle', 'shape should be greenCircle');
|
||||
assert.equal(stage.getIntersection(372, 93).getId(), 'redCircle', 'shape should be greenCircle');
|
||||
|
||||
//console.log(layer.hitCanvas.context._context.getImageData(1, 1, 1, 1).data)
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user