The CanvasRenderingContext2D.moveTo() method of the Canvas 2D API begins a new sub-path at the point specified by the given (x, y) coordinates.
moveTo(x, y)xThe x-axis (horizontal) coordinate of the point.
yThe y-axis (vertical) coordinate of the point.
None (undefined).
This example uses moveTo() to create two sub-paths within a single path. Both sub-paths are then rendered with a single stroke() call.
<canvas id="canvas"></canvas>The first line begins at (50, 50) and ends at (200, 50). The second line begins at (50, 90) and ends at (280, 120).
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();CanvasRenderingContext2DCanvasRenderingContext2D.lineTo()CanvasRenderingContext2D.stroke()