Canvas API

The <canvas> element defines a bitmap area in the HTML page.

The Canvas API allows JavaScript to draw graphics on the canvas.

The Canvas API can draw shapes, lines, curves, boxes, text, and images, as well as colors, rotations, opacity, and other pixel operations.

Add Canvas to HTML

You can add a canvas element at any position in the HTML page using the <canvas> tag:

Instance

<canvas id="myCanvas" width="300" height="150"></canvas>

Try it yourself

How to access the Canvas element

You can access the <canvas> element using the HTML DOM method getElementById():

const myCanvas = document.getElementById("myCanvas");

To draw on the canvas, you need to create a 2D context object:

const ctx = myCanvas.getContext("2d");

Note

The HTML <canvas> element itself does not have drawing capabilities.

You must use JavaScript to draw any graphics.

The getContextMenu() method returns an object with drawing tools (methods).

Drawing on the canvas

After creating the 2D context object, you can draw on the canvas.

The following fillRect() method draws a black rectangle with its top-left corner at position 20,20. The rectangle is 150 pixels wide and 100 pixels high:

Instance

const myCanvas = document.getElementById("myCanvas");
const ctx = myCanvas.getContext("2d");
ctx.fillRect(20, 20, 150, 100);

Try it yourself

Using color

The fillStyle property sets the fill color of the drawing object:

Instance

const myCanvas = document.getElementById("myCanvas");
const ctx = myCanvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 150, 100);

Try it yourself

You can also create a new <canvas> element using the document.createElement() method and add it to an existing HTML page:

Instance

const myCanvas = document.createElement("canvas");
document.body.appendChild(myCanvas);
const ctx = myCanvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 150, 100);

Try it yourself

Path

Common methods for drawing on the canvas are:

  1. Start a path - beginPath()
  2. Move to a point - moveTo()
  3. Draw in the path - lineTo()
  4. Draw a path - stroke()

Instance

const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.stroke();

Try it yourself

Color, style, and shadow

Properties Description
fillStyle Set or return the color, gradient, or pattern used for filling the drawing.
strokeStyle Set or return the color, gradient, or pattern used for strokes.
shadowColor Set or return the color used for the shadow.
shadowBlur Set or return the blur level of the shadow.
shadowOffsetX Set or return the horizontal distance of the shadow to the shape.
shadowOffsetY Set or return the vertical distance of the shadow to the shape.
Method Description
createLinearGradient() Create a linear gradient (for canvas content).
createPattern() Repeat the specified element in the specified direction.
createRadialGradient() Create a radial/circular gradient (for canvas content).
addColorStop() Specify the color and stop position in the gradient object.

Line style

Properties Description
lineCap Set or return the line cap style.
lineJoin Set or return the type of angle created when two lines intersect.
lineWidth Set or return the current line width.
miterLimit Set or return the maximum miter length.

Rectangle

Method Description
rect() Create a rectangle.
fillRect() Draw a 'filled' rectangle.
strokeRect() Draw a rectangle (without fill).
clearRect() Clear the specified pixels within the given rectangle.

Path

Method Description
fill() Fill the current graphic (path).
stroke() Actually draw the path you define.
beginPath() Start a path, or reset the current path.
moveTo() Move the path to a specified point on the canvas without creating a line.
closePath() Create a path from the current point back to the starting point.
lineTo() Add a new point and create a line from that point to the last specified point on the canvas.
clip() Clip any shape and size of the area from the original canvas.
quadraticCurveTo() Create a quadratic Bezier curve.
bezierCurveTo() Create a cubic Bezier curve.
arc() Create an arc/curve (used to create a circle or part of a circle).
arcTo() Create an arc/curve between two tangents.
isPointInPath() Returns true if the specified point is in the current path, otherwise returns false.

Transformation

Method Description
scale() Zoom in or out of the current graphic.
rotate() Rotate the current graphic.
translate() Remap the (0,0) position on the canvas.
transform() Replace the current drawing transformation matrix.
setTransform() Reset the current transformation to the unit matrix. Then run transform().

Text

Properties Description
font Set or return the current font properties of the text content.
textAlign Set or return the current alignment style of the text content.
textBaseline Sets or returns the current text baseline used when drawing text.
Method Description
fillText() Draws 'filled' text on the canvas.
strokeText() Draws text on the canvas (without fill).
measureText() Returns an object that contains the specified text width.

Image Drawing

Method Description
drawImage() Draw images, canvas, or video on the canvas.

Pixel Operations

Properties Description
width Returns the width of the ImageData object.
height Returns the height of the ImageData object.
data Returns an object that contains the image data of the specified ImageData object.
Method Description
createImageData() Creates a new blank ImageData object.
getImageData() Returns an ImageData object that copies the pixel data of the specified rectangle on the canvas.
putImageData() Places the image data (from the specified ImageData object) back onto the canvas.

Composition

Properties Description
globalAlpha Sets or returns the current alpha or opacity value for drawing.
globalCompositeOperation Sets or returns how the new image is drawn onto the existing image.

Other

Method Description
save() Saves the current context state.
restore() Returns the previously saved path state and properties.
createEvent()
getContext()
toDataURL()

Standard Properties and Events

The canvas object supports both standardPropertiesAndEvents.

Related Pages

HTML Tutorial:HTML5 Canvas

HTML Image Tutorial:HTML Canvas Tutorial

HTML Reference Manual:HTML <canvas> Tag