Harry
Harry

Reputation: 1150

HTML5 canvas: Can I draw a shape and give it a variable name?

I would like to draw a shape on the canvas (which I can do) but I'm not sure if it's possible to give that shape a variable name. I want to do this so I can then change the width of that shape later without having to redraw the shape.

Can somebody help? Thanks.

Upvotes: 2

Views: 5659

Answers (1)

Loktar
Loktar

Reputation: 35319

You cannot do that unfortunately without redrawing the shape. What you could do is store the information in an object like so.

var rectangle = {x:10,y:20,width:20,height:40};

Then you can change any of the values and redraw it like so,

//clear the canvas then draw
rectangle.width = 60;
ctx.fillRect(rectangle.x,rectangle.y,rectangle.width,rectangle.height);

Live Demo

Upvotes: 4

Related Questions