0

I am detecting whether the mousepointer is within an ellipse that I have drawn in P5.js. When the mouse is in contact with the ellipse it should be deleted. I can achieve this by iterating over the array that stores the ellipses locations, checking if the mouse pointer is close enough to them to be warranted as inside by accessing the width element.w. This is a way to do it with a simple for loop:

for(var i = 0; i < locations.length; i++){
    var d = dist(mouseX, mouseY, locations[i].x, locations[i].y);
    if(d <= locations[i].w){
        locations.splice(i,1);
    }
}

I am wondering what the forEach loop way to do this would be. I premuse it would be something like the below code, but how do you actually destroy the element?

locations.forEach(function(element) {
    var d = dist(mouseX, mouseY, element.x, element.y);
    if(d <= element.w){
        //Destoy the element??? How do you do that????
    }                   
});
2

3 Answers 3

1

Try this if you don't want to deal with browser compatibility issues for .remove()

function removeDummy() {
    var elem = document.getElementById('dummy');
    elem.parentNode.removeChild(elem);
    return false;
}

The snippet is from this answer https://stackoverflow.com/a/5933167/4724167.

Sign up to request clarification or add additional context in comments.

Comments

1

use filter function of Array, def variable preserve new array

Comments

0

Try using element.remove(), but this has cross-browser compatibility issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.