CanvasRenderingContext2D .moveTo() method of the Canvas 2D API begins a new sub-path at the point specified by the given (x, y) 坐标。

句法

void ctx.moveTo(x, y);
					

参数

x

The x-axis (horizontal) coordinate of the point.

y

The y-axis (vertical) coordinate of the point.

范例

Creating multiple sub-paths

此范例使用 moveTo() to create two sub-paths within a single path. Both sub-paths are then rendered with a single stroke() 调用。

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).

var canvas = document.getElementById('canvas');
var 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();
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.moveTo' in that specification.
实时标准

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
moveTo Chrome 1 Edge 12 Firefox 1.5 IE 9 Opera 11.6 Safari 2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: