Q:

draw rectangle on canvas javascript

//Assuming you have canvas and context (ctx) set up:
ctx.fillStyle = "(enter color)";
ctx.fillRect(x, y, width, height);
//to set up canvas and context:
var canvas = document.getElementById("(enter canvas's id)");
var ctx = canvas.getContext("2d");
2

var c = document.getElementById("myCanvas");

var ctx = c.getContext("2d");
ctx.beginPath();

ctx.rect(20, 20, 150, 100);

ctx.stroke();
 
3
// canvas context.fillRect(x, y, width, height);
ctx.fillRect(20, 20, 150, 100);
0

New to Communities?

Join the community