AttributeDescription
heightSpecifies the canvas height
widthSpecifies the canvas width

Basic Example

The canvas element was introduced in HTML5 for drawing graphics.

<canvas id="myCanvas">
 Cannot display graphic. Canvas is not supported by your browser (IE<9)
</canvas>

The above will create a transparent HTML <canvas> element of 300×150 px in size.

You can use the canvas element to draw amazing stuff like shapes, graphs, manipulate images, create engaging
games etc. with JavaScript.

Advertisement

The canvas’s 2D drawable layer surface Object is referred to as CanvasRenderingContext2D; or from a
HTMLCanvasElement using the .getContext (“2d”) method:

var ctx = document.getElementById("myCanvas").getContext("2d");
// now we can refer to the canvas's 2D layer context using `ctx`
ctx.fillStyle = "#f00";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); // x, y, width, height
ctx.fillStyle = "#000";
ctx.fillText("My red canvas with some black text", 24, 32); // text, x, y

jsFiddle example

Drawing two rectangles on a <canvas>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8" />
 <title>Draw two rectangles on the canvas</title>
 <style>
 canvas{
 border:1px solid gray;
 }
 </style>
 <script async>
 window.onload = init; // call init() once the window is completely loaded
 function init(){
 // #1 - get reference to <canvas> element
 var canvas = document.querySelector('canvas');
 
 // #2 - get reference to the drawing context and drawing API
 var ctx = canvas.getContext('2d');
 // #3 - all fill operations are now in red
 ctx.fillStyle = 'red';GoalKicker.com – HTML5 Notes for Professionals 84
 // #4 - fill a 100x100 rectangle at x=0,y=0
 ctx.fillRect(0,0,100,100);
 
 // #5 - all fill operations are now in green
 ctx.fillStyle = 'green';
 
 // #6 - fill a 50x50 rectangle at x=25,y=25
 ctx.fillRect(25,25,50,50);
 
 }
 </script>
</head>
<body>
 <canvas width=300 height=200>Your browser does not support canvas.</canvas>
</body>
</html>
Advertisement

Recent Posts

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago