Path2D.addPath() - Web APIs 编辑

The Path2D.addPath() method of the Canvas 2D API adds one Path2D object to another Path2D object.

Syntax

void path.addPath(path [, transform]);

Parameters

path
A Path2D path to add.
transform Optional
A DOMMatrix to be used as the transformation matrix for the path that is added. (Technically a DOMMatrixInit object).

Examples

Adding a path to an existing path

This example adds one path to another.

HTML

<canvas id="canvas"></canvas>

JavaScript

First, we create two separate Path2D objects, each of which contains a rectangle made using the rect() method. We then create a matrix using document.createElementNS() and createSVGMatrix(). We then add the second path to the first using addPath(), also applying the matrix to move the second path to the right. Finally, we draw the first path (which now contains both rectangles) using fill().

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// Create first path and add a rectangle
let p1 = new Path2D();
p1.rect(0, 0, 100, 150);

// Create second path and add a rectangle
let p2 = new Path2D();
p2.rect(0, 0, 100, 75);

// Create transformation matrix that moves 200 points to the right
let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix();
m.a = 1; m.b = 0;
m.c = 0; m.d = 1;
m.e = 200; m.f = 0;

// Add second path to the first path
p1.addPath(p2, m);

// Draw the first path
ctx.fill(p1);

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'Path2D.addPath()' in that specification.
Living StandardInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

  • The interface defining this method: Path2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:96 次

字数:4106

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文