I have done similar work in my Web based application, where user can create multiple Svg Elements and then select them using Selection Box.
Now About Selecting the svg element in selection box, i have used 'Webworkers' for complex mathematical calculations. Below would be the basic Algorithm to check whether an svg element is inside Selection rect or not:-
1) First Create a Webwroker and pass list of whole elements that are created on your canvas to it.
2) This list include bbox of each element
3) now compare each element bbox with your Rect . check the below function:-
isInsideSelectionBox = function(selectionBox){
var inside = false;
if(element.bbox.p1.x >= selectionBox.p1.x && element.bbox.p1.x <= selectionBox.p3.x && element.bbox.p1.y >= selectionBox.p1.y && element.bbox.p1.y <= selectionBox.p3.y){
inside = true;
}else if(element.bbox.p2.x >= selectionBox.p1.x && element.bbox.p2.x <= selectionBox.p3.x && element.bbox.p2.y >= selectionBox.p1.y && element.bbox.p2.y <= selectionBox.p3.y){
inside = true;
}else if(element.bbox.p3.x >= selectionBox.p1.x && element.bbox.p3.x <= selectionBox.p3.x && element.bbox.p3.y >= selectionBox.p1.y && element.bbox.p3.y <= selectionBox.p3.y){
inside = true;
}else if(element.bbox.p4.x >= selectionBox.p1.x && element.bbox.p4.x <= selectionBox.p3.x && element.bbox.p4.y >= selectionBox.p1.y && element.bbox.p4.y <= selectionBox.p3.y){
inside = true;
}
return inside;
};
This will help you. You need to call this function on mousemove event of selection rectangle.
- If your Application is small, dealing with 1 to 30 elements, than your do no need Webworkers