mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 00:14:29 +08:00
pixel detection performance was horrible for large amounts of shapes. Added some optimizations which require knowledge of a shape's perceivable position and size based on transforms. This will be added in later. For the time being, only Kinetic.Images will have access to the detectionType property. Image cannot be transformed or offset with the center offset. Support for these will come soon as well.
This commit is contained in:
parent
313c6a1541
commit
72c0555d0b
31
dist/kinetic-core.js
vendored
31
dist/kinetic-core.js
vendored
@ -2108,21 +2108,40 @@ Kinetic.Shape.prototype = {
|
|||||||
return pathLayerContext.isPointInPath(pos.x, pos.y);
|
return pathLayerContext.isPointInPath(pos.x, pos.y);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
var ax = this.getAbsolutePosition().x;
|
||||||
|
var ay = this.getAbsolutePosition().y;
|
||||||
|
var aw = this.getWidth();
|
||||||
|
var ah = this.getHeight();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: need to also take into account absolute
|
||||||
|
* rotation, absolute scale, and center offsets
|
||||||
|
* to calculate the correct aw, ah, ax, and ay values. Also need
|
||||||
|
* to implement getHeight and getWidth methods for each Shape
|
||||||
|
* object
|
||||||
|
*/
|
||||||
|
|
||||||
|
// only check pixels if it's possibly in range
|
||||||
|
if(pos.x >= ax && pos.x <= (ax + aw) && pos.y >= ay && pos.y <= ay + ah) {
|
||||||
var bufferLayer = stage.bufferLayer;
|
var bufferLayer = stage.bufferLayer;
|
||||||
var bufferLayerContext = bufferLayer.getContext();
|
var bufferLayerContext = bufferLayer.getContext();
|
||||||
|
|
||||||
this._draw(bufferLayer);
|
this._draw(bufferLayer);
|
||||||
|
|
||||||
var w = stage.width;
|
var px = pos.x - ax;
|
||||||
var h = stage.height;
|
var py = pos.y - ay;
|
||||||
var x = pos.x;
|
|
||||||
var y = pos.y;
|
// only get the image data for possible area
|
||||||
var imageData = bufferLayerContext.getImageData(0, 0, w, h);
|
var imageData = bufferLayerContext.getImageData(ax, ay, aw, ah);
|
||||||
var data = imageData.data;
|
var data = imageData.data;
|
||||||
var alpha = data[((w * y) + x) * 4 + 3];
|
var alpha = data[((aw * py) + px) * 4 + 3];
|
||||||
|
|
||||||
return (alpha !== undefined && alpha !== 0);
|
return (alpha !== undefined && alpha !== 0);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// extend Node
|
// extend Node
|
||||||
|
2
dist/kinetic-core.min.js
vendored
2
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
31
src/Shape.js
31
src/Shape.js
@ -192,21 +192,40 @@ Kinetic.Shape.prototype = {
|
|||||||
return pathLayerContext.isPointInPath(pos.x, pos.y);
|
return pathLayerContext.isPointInPath(pos.x, pos.y);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
var ax = this.getAbsolutePosition().x;
|
||||||
|
var ay = this.getAbsolutePosition().y;
|
||||||
|
var aw = this.getWidth();
|
||||||
|
var ah = this.getHeight();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: need to also take into account absolute
|
||||||
|
* rotation, absolute scale, and center offsets
|
||||||
|
* to calculate the correct aw, ah, ax, and ay values. Also need
|
||||||
|
* to implement getHeight and getWidth methods for each Shape
|
||||||
|
* object
|
||||||
|
*/
|
||||||
|
|
||||||
|
// only check pixels if it's possibly in range
|
||||||
|
if(pos.x >= ax && pos.x <= (ax + aw) && pos.y >= ay && pos.y <= ay + ah) {
|
||||||
var bufferLayer = stage.bufferLayer;
|
var bufferLayer = stage.bufferLayer;
|
||||||
var bufferLayerContext = bufferLayer.getContext();
|
var bufferLayerContext = bufferLayer.getContext();
|
||||||
|
|
||||||
this._draw(bufferLayer);
|
this._draw(bufferLayer);
|
||||||
|
|
||||||
var w = stage.width;
|
var px = pos.x - ax;
|
||||||
var h = stage.height;
|
var py = pos.y - ay;
|
||||||
var x = pos.x;
|
|
||||||
var y = pos.y;
|
// only get the image data for possible area
|
||||||
var imageData = bufferLayerContext.getImageData(0, 0, w, h);
|
var imageData = bufferLayerContext.getImageData(ax, ay, aw, ah);
|
||||||
var data = imageData.data;
|
var data = imageData.data;
|
||||||
var alpha = data[((w * y) + x) * 4 + 3];
|
var alpha = data[((aw * py) + px) * 4 + 3];
|
||||||
|
|
||||||
return (alpha !== undefined && alpha !== 0);
|
return (alpha !== undefined && alpha !== 0);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// extend Node
|
// extend Node
|
||||||
|
@ -553,6 +553,11 @@ Test.prototype.tests = {
|
|||||||
};
|
};
|
||||||
imageObj.src = '../lion.png';
|
imageObj.src = '../lion.png';
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
|
* TODO: need to implement area x, y, width and height
|
||||||
|
* in order to support other shape pixel detection
|
||||||
|
*/
|
||||||
|
/*
|
||||||
'EVENTS - star pixel detection': function(containerId) {
|
'EVENTS - star pixel detection': function(containerId) {
|
||||||
var imageObj = new Image();
|
var imageObj = new Image();
|
||||||
imageObj.onload = function() {
|
imageObj.onload = function() {
|
||||||
@ -591,6 +596,7 @@ Test.prototype.tests = {
|
|||||||
};
|
};
|
||||||
imageObj.src = '../lion.png';
|
imageObj.src = '../lion.png';
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
'EVENTS - drag events click': function(containerId) {
|
'EVENTS - drag events click': function(containerId) {
|
||||||
var stage = new Kinetic.Stage({
|
var stage = new Kinetic.Stage({
|
||||||
container: containerId,
|
container: containerId,
|
||||||
|
Loading…
Reference in New Issue
Block a user