Working with circles on the canvas

I am trying to create a button that adds a circle and another button that deletes the last circle created. I thought adding circles to a list to help with that, but that does not seem to work. Any suggestions?

That does seem like the right idea. It’s a hard problem to debug without any code. Would you please share a link to your code so we can see what you’re looking at?

That would be helpful.

setActiveCanvas("canvas1");

var circleList=[];


onEvent("button1", "click", function( ) {
	appendItem(circleList,circle(randomNumber(50,250),randomNumber(50,300),randomNumber(10,100)));
	
});

onEvent("button2", "click", function( ) {
	removeItem(circleList,circleList.length-1);
	
});```

I think you would have to do something like this

setActiveCanvas("canvas1");

var x =[];
var y =[];
var rad =[];

onEvent("button1", "click", function( ) {
  appendItem(x, randomNumber(50,250));
  appendItem(y, randomNumber(50,300));
  appendItem(rad, randomNumber(10,100));

  for (var i = 0; i < x.length; i++){
	  circle(x[i],y[i],rad[i]);
  }
});

onEvent("button2", "click", function( ) {
        clearCanvas();
		removeItem(x,x.length-1);
		removeItem(y,y.length-1);
		removeItem(rad,rad.length-1);
		for (var i = 0; i < x.length; i++){
	             circle(x[i],y[i],rad[i]);
	    }

});
2 Likes