KineticJS and shapes inside area

感情迁移 提交于 2019-12-11 19:23:00

问题


I am looking for the fastest way to find all shapes inside an area. Please try this example in Chrome or FireFox: http://jsfiddle.net/markusleth/FBjKY/

I know how to iterate and compare coordinates, but I am worried about performance. Any suggestions are appreciated.

var x1, x2, y1, y2;
var shapes = stage.get("Rect");
shapes.each(function (shape) {
   // calculate if shape is inside x1, x2, y1, y2
});

回答1:


Well, KineticJS has a few intersection functions:

intersects(point) Kinetic.Shape#intersects

getAllIntersections(pos) Kinetic.Container#getAllIntersections

getIntersection(pos) Kinetic.Stage#getIntersection

Although getAllIntersections is probably the function you need, it looks like the author of KineticJS strongly recommends to use getIntersection IF possible over getAllIntersections. This is because getAllIntersections has poor performance when called multiple times consecutively, which is probably a problem for you.

In my experience, getIntersection only retrieves 1 object that intersects the point and it seems to only return the latest object added to the stage, that intersects the point! I'm not sure how you would use this in your situation.

User EliteOctagon wrote his own collision detection function with better success (and better performance!): HTML5 / kineticJS getIntersection function implementation This might be your best bet. Good luck!

Oh and another small tip on performance: if you are trying to select shapes rather than just "Rectangles", it would work better if you named all selectable shapes the same name, and use the .get() function on the name instead of just .get("Rect").

For example:

new Kinetic.Rect({
  name: "selectableShape"
});

new Kinetic.Ellipse({
  name: "selectableShape"
});

var selectableShapes = stage.get(".selectableShape");


来源:https://stackoverflow.com/questions/17486155/kineticjs-and-shapes-inside-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!