// Create a canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set the size of the canvas
const SIDE_LENGTH = 100;
canvas.width = SIDE_LENGTH;
canvas.height = SIDE_LENGTH;
// Generate a random color
const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
// Draw the square
ctx.fillStyle = randomColor;
ctx.fillRect(canvas.width/2 - SIDE_LENGTH/2, canvas.height/2 - SIDE_LENGTH/2, SIDE_LENGTH, SIDE_LENGTH);
Random color square JavaScript
Write a program that draws a random color square centered on the graphics canvas. The sides of the square should be SIDE_LENGTH.
(Hint: Use the Randomizer to get a random color for the square.)
1 answer