getIntersections no longer returns visible shapes in the result set, and can also be called from any container

This commit is contained in:
Eric Rowell 2012-07-06 21:45:18 -07:00
parent 1f3d1cc905
commit 9baaee2440
6 changed files with 98 additions and 72 deletions

36
dist/kinetic-core.js vendored
View File

@ -1558,6 +1558,24 @@ Kinetic.Container = Kinetic.Node.extend({
return false; return false;
}, },
/**
* get shapes that intersect a point
* @param {Object} point
*/
getIntersections: function() {
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
var arr = [];
var shapes = this.get('Shape');
for(var n = 0; n < shapes.length; n++) {
var shape = shapes[n];
if(shape.isVisible() && shape.intersects(pos)) {
arr.push(shape);
}
}
return arr;
},
/** /**
* get all shapes inside container * get all shapes inside container
*/ */
@ -1909,24 +1927,6 @@ Kinetic.Stage = Kinetic.Container.extend({
getStage: function() { getStage: function() {
return this; return this;
}, },
/**
* get shapes that intersect a point
* @param {Object} point
*/
getIntersections: function() {
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
var arr = [];
var shapes = this.get('Shape');
for(var n = 0; n < shapes.length; n++) {
var shape = shapes[n];
if(shape.intersects(pos)) {
arr.push(shape);
}
}
return arr;
},
/** /**
* get stage DOM node, which is a div element * get stage DOM node, which is a div element
* with the class name "kineticjs-content" * with the class name "kineticjs-content"

File diff suppressed because one or more lines are too long

View File

@ -153,6 +153,24 @@ Kinetic.Container = Kinetic.Node.extend({
return false; return false;
}, },
/**
* get shapes that intersect a point
* @param {Object} point
*/
getIntersections: function() {
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
var arr = [];
var shapes = this.get('Shape');
for(var n = 0; n < shapes.length; n++) {
var shape = shapes[n];
if(shape.isVisible() && shape.intersects(pos)) {
arr.push(shape);
}
}
return arr;
},
/** /**
* get all shapes inside container * get all shapes inside container
*/ */

View File

@ -281,24 +281,6 @@ Kinetic.Stage = Kinetic.Container.extend({
getStage: function() { getStage: function() {
return this; return this;
}, },
/**
* get shapes that intersect a point
* @param {Object} point
*/
getIntersections: function() {
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments));
var arr = [];
var shapes = this.get('Shape');
for(var n = 0; n < shapes.length; n++) {
var shape = shapes[n];
if(shape.intersects(pos)) {
arr.push(shape);
}
}
return arr;
},
/** /**
* get stage DOM node, which is a div element * get stage DOM node, which is a div element
* with the class name "kineticjs-content" * with the class name "kineticjs-content"

View File

@ -109,7 +109,7 @@ Test.prototype.tests = {
easing: 'bounce-ease-out' easing: 'bounce-ease-out'
}); });
}, },
'TRANSITION - all transition types': function(containerId) { '*TRANSITION - all transition types': function(containerId) {
document.getElementById(containerId).style.height = '300px'; document.getElementById(containerId).style.height = '300px';
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({

View File

@ -412,58 +412,84 @@ Test.prototype.tests = {
//console.log(stage.toJSON()); //console.log(stage.toJSON());
test(stage.toJSON() === json, "problem loading stage with custom shape json"); test(stage.toJSON() === json, "problem loading stage with custom shape json");
}, },
'STAGE - test getShapesInPoint': function(containerId) { 'EVENTS - test getIntersections': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,
height: 200 height: 200,
throttle: 999
}); });
var layer = new Kinetic.Layer(); var layer = new Kinetic.Layer();
var fooLayer = new Kinetic.Layer();
var group = new Kinetic.Group(); var group = new Kinetic.Group();
var blue = new Kinetic.Rect({ var redCircle = new Kinetic.Ellipse({
x: 200, x: 380,
y: 100, y: stage.getHeight() / 2,
width: 100, radius: 70,
height: 50, strokeWidth: 4,
fill: 'blue', fill: 'red',
name: 'blue' stroke: 'black',
id: 'redCircle'
}); });
var red = new Kinetic.Rect({ var greenCircle = new Kinetic.Ellipse({
x: 250, x: 300,
y: 100, y: stage.getHeight() / 2,
width: 100, radius: 70,
height: 50, strokeWidth: 4,
fill: 'red' fill: 'green',
stroke: 'black',
id: 'greenCircle'
}); });
group.add(red); group.add(redCircle);
layer.add(blue);
layer.add(group); layer.add(group);
layer.add(greenCircle);
stage.add(layer); stage.add(layer);
stage.add(fooLayer);
test(stage.getIntersections({ test(stage.getIntersections(350, 118).length === 2, 'getIntersections should return two shapes');
x: 201, test(stage.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
y: 101 test(stage.getIntersections(350, 118)[1].getId() === 'greenCircle', 'second intersection should be greenCircle');
}).length === 1, 'should be 1 shape ');
test(stage.getIntersections(201, 101).length === 1, 'should be 1 shape '); // hide green circle. make sure only red circle is in result set
greenCircle.hide();
layer.draw();
test(stage.getIntersections(201, 101)[0].getName() === 'blue', 'shape name should be blue'); test(stage.getIntersections(350, 118).length === 1, 'getIntersections should return one shape');
test(stage.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
test(stage.getIntersections({ // show green circle again. make sure both circles are in result set
x: 251, greenCircle.show();
y: 101 layer.draw();
}).length === 2, 'should be 2 shapes ');
test(stage.getIntersections({ test(stage.getIntersections(350, 118).length === 2, 'getIntersections should return two shapes');
x: 350, test(stage.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
y: 150 test(stage.getIntersections(350, 118)[1].getId() === 'greenCircle', 'second intersection should be greenCircle');
}).length === 1, 'should be 1 shape ');
// hide red circle. make sure only green circle is in result set
redCircle.hide();
layer.draw();
test(stage.getIntersections(350, 118).length === 1, 'getIntersections should return one shape');
test(stage.getIntersections(350, 118)[0].getId() === 'greenCircle', 'first intersection should be greenCircle');
// show red circle again. make sure both circles are in result set
redCircle.show();
layer.draw();
test(stage.getIntersections(350, 118).length === 2, 'getIntersections should return two shapes');
test(stage.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
test(stage.getIntersections(350, 118)[1].getId() === 'greenCircle', 'second intersection should be greenCircle');
// test from layer
test(layer.getIntersections(350, 118).length === 2, 'getIntersections should return two shapes');
test(layer.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
test(layer.getIntersections(350, 118)[1].getId() === 'greenCircle', 'second intersection should be greenCircle');
// test from group
test(group.getIntersections(350, 118).length === 1, 'getIntersections should return two shapes');
test(group.getIntersections(350, 118)[0].getId() === 'redCircle', 'first intersection should be redCircle');
}, },
'STAGE - scale stage after add layer': function(containerId) { 'STAGE - scale stage after add layer': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({