js draw a box code example

Example 1: 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");

Example 2: box draw html

<div style="width:500px;height:100px;border:1px solid #000;">This is a rectangle!</div>

Example 3: javascript rect

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

Tags:

Html Example