Canvas API
Draw on canvas element
IntermediateTopic: Browser API
JavaScript Canvas API Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 100, 100);Output
// Draws red rectangle on canvas
Understanding Canvas API
Canvas API enables 2D drawing.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.