CanvasRenderingContext2D: moveTo() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Please help us by answering a few questions.
The
CanvasRenderingContext2D.moveTo()
method of the Canvas 2D API begins a new sub-path at the point specified by the given
(x, y)
coordinates.
Syntax
js
moveTo(x, y)
Parameters
Return value
None (undefined
).
Examples
Creating multiple sub-paths
This example uses moveTo()
to create two sub-paths within a single path.
Both sub-paths are then rendered with a single stroke()
call.
HTML
html
<canvas id="canvas"></canvas>
JavaScript
The first line begins at (50, 50) and ends at (200, 50). The second line begins at (50, 90) and ends at (280, 120).
js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50, 50); // Begin first sub-path
ctx.lineTo(200, 50);
ctx.moveTo(50, 90); // Begin second sub-path
ctx.lineTo(280, 120);
ctx.stroke();
Result
Specifications
Specification |
---|
HTML # dom-context-2d-moveto-dev |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
moveTo |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
See also
- The interface defining this method:
CanvasRenderingContext2D
CanvasRenderingContext2D.lineTo()
CanvasRenderingContext2D.stroke()