I have code to find images in my selections. But when nothing in selection (loop FOR) and selection is kinda big, it's kinda laggy. How can I improve my code?
var toStageCoord = map.stageXYToCoordinates(toStage.x + selection.x1, toStage.y + selection.y1);
var element1 = document.elementFromPoint(toStage.x + selection.x1, toStage.y + selection.y1);
var element2 = document.elementFromPoint(toStage.x + selection.x2, toStage.y + selection.y2);
var element3 = document.elementFromPoint(toStage.x + selection.x2, toStage.y + selection.y1);
var element4 = document.elementFromPoint(toStage.x + selection.x1, toStage.y + selection.y2);
var found = false;
if(element1.localName == "image" || element2.localName == "image" || element3.localName == "image" || element4.localName == "image") {
found = true;console.log("foundfirst");
}
for(var y = toStage.y + selection.y1;y < toStage.y + selection.y2 ;y++) {
if(found){break;}
for(var x = toStage.x + selection.x1;x < toStage.x + selection.x2;x++) {
var element = document.elementFromPoint(x,y);
if(element.localName === "image") {
console.log("found");
found = true;
break;
}
}
}