diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/path2d | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/path2d')
-rw-r--r-- | files/zh-cn/web/api/path2d/addpath/index.html | 181 | ||||
-rw-r--r-- | files/zh-cn/web/api/path2d/index.html | 131 | ||||
-rw-r--r-- | files/zh-cn/web/api/path2d/path2d/index.html | 224 |
3 files changed, 536 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/path2d/addpath/index.html b/files/zh-cn/web/api/path2d/addpath/index.html new file mode 100644 index 0000000000..a19a3ce8cf --- /dev/null +++ b/files/zh-cn/web/api/path2d/addpath/index.html @@ -0,0 +1,181 @@ +--- +title: Path2D.addPath() +slug: Web/API/Path2D/addPath +translation_of: Web/API/Path2D/addPath +--- +<div>{{APIRef("Canvas API")}}</div> + +<p><code style="font-style: normal;"><strong>Path2D</strong></code><strong><code>.addPath()</code></strong> 是 Canvas 2D API 根据指定路径变量添加路径的方法。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">void <var><em>path</em>.addPath(path [, transform]);</var> +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>path</code></dt> + <dd>需要添加的 {{domxref("Path2D")}} 路径。</dd> + <dt><code>transform</code> {{optional_inline}}</dt> + <dd>{{domxref("SVGMatrix")}} 作为新增路径的变换矩阵。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用_addPath_方法">使用 <code>addPath</code> 方法</h3> + +<p>这是一段使用 <code>addPath</code> 方法的简单的代码片段。</p> + +<pre class="brush: js; highlight:[19]">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); + +// Create a new path with a rect +var p1 = new Path2D(); +p1.rect(0,0,100,100); + +// Create another path with a rect +var p2 = new Path2D(); +p2.rect(0,0,100,100); + +// Create transformation matrix that moves vertically 300 points to the right +var 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 = 300; m.f = 0; + +// add the second path to the first path +p1.addPath(p2, m); + +// Finally, fill the first path onto the canvas +ctx.fill(p1); +</pre> + +<p>修改下面的代码并在线查看 canvas 的变化 (查看浏览器兼容性列表,确定你目前的浏览器是否支持这个方法):</p> + +<div style="display: none;"> +<h6 id="Playable_code">Playable code</h6> + +<pre class="brush: html"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> +<div class="playable-buttons"> + <input id="edit" type="button" value="Edit" /> + <input id="reset" type="button" value="Reset" /> +</div> +<textarea id="code" class="playable-code" style="height:220px;"> +var p1 = new Path2D(); +p1.rect(0,0,100,100); + +var p2 = new Path2D(); +p2.rect(0,0,100,100); + +var 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 = 300; m.f = 0; + +p1.addPath(p2, m); +ctx.fill(p1);</textarea> +</pre> + +<pre class="brush: js">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); +var textarea = document.getElementById("code"); +var reset = document.getElementById("reset"); +var edit = document.getElementById("edit"); +var code = textarea.value; + +function drawCanvas() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + eval(textarea.value); +} + +reset.addEventListener("click", function() { + textarea.value = code; + drawCanvas(); +}); + +edit.addEventListener("click", function() { + textarea.focus(); +}) + +textarea.addEventListener("input", drawCanvas); +window.addEventListener("load", drawCanvas); +</pre> +</div> + +<p>{{ EmbedLiveSample('Playable_code', 700, 500) }}</p> + +<h2 id="规范描述">规范描述</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', "scripting.html#dom-path2d-addpath", "Path2D.addPath()")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial defintion.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop(34)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile(34)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>接口定义, {{domxref("Path2D")}}.</li> +</ul> diff --git a/files/zh-cn/web/api/path2d/index.html b/files/zh-cn/web/api/path2d/index.html new file mode 100644 index 0000000000..b27962c96c --- /dev/null +++ b/files/zh-cn/web/api/path2d/index.html @@ -0,0 +1,131 @@ +--- +title: Path2D +slug: Web/API/Path2D +translation_of: Web/API/Path2D +--- +<div>{{APIRef("Canvas API")}} {{SeeCompatTable}}</div> + +<p>Canvas 2D API 的接口 <strong><code>Path2D</code></strong> 用来声明路径,此路径稍后会被{{domxref("CanvasRenderingContext2D")}} 对象使用。<code style="font-style: normal;">CanvasRenderingContext2D</code> 接口的 <a href="/en-US/docs/Web/API/CanvasRenderingContext2D#Paths">路径方法</a> 也存在于 Path2D 这个接口中,允许你在 canvas 中根据需要创建可以保留并重用的路径。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("Path2D.Path2D", "Path2D()")}}</dt> + <dd><code>Path2D</code> 构造函数。 创建一个新的 <code>Path2D</code> 对象。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("Path2D.addPath()")}}</dt> + <dd>添加一条新路径到对当前路径。</dd> + <dt>{{domxref("CanvasRenderingContext2D.closePath", "Path2D.closePath()")}}</dt> + <dd>使笔点返回到当前子路径的起始点。它尝试从当前点到起始点绘制一条直线。 如果图形已经是封闭的或者只有一个点,那么此函数不会做任何操作。</dd> + <dt>{{domxref("CanvasRenderingContext2D.moveTo()", "Path2D.moveTo()")}}</dt> + <dd>将一个新的子路径的起始点移动到(x,y)坐标。</dd> + <dt>{{domxref("CanvasRenderingContext2D.lineTo()", "Path2D.lineTo()")}}</dt> + <dd>使用直线连接子路径的终点到 <code style="font-style: normal;">x, y</code> 坐标。</dd> + <dt>{{domxref("CanvasRenderingContext2D.bezierCurveTo()", "Path2D.bezierCurveTo()")}}</dt> + <dd>添加一条三次贝赛尔曲线到当前路径。 该方法需要三个点。 第一、第二个点是控制点,第三个点是结束点。起始点是当前路径的最后一个点,绘制贝赛尔曲线前,可以通过调用 <code style="font-style: normal;">moveTo()</code> 进行修改。</dd> + <dt>{{domxref("CanvasRenderingContext2D.quadraticCurveTo()", "Path2D.quadraticCurveTo()")}}</dt> + <dd>添加一条二次贝赛尔曲线到当前路径。 </dd> + <dt>{{domxref("CanvasRenderingContext2D.arc()", "Path2D.arc()")}}</dt> + <dd>添加一条圆弧路径。 圆弧路径的圆心在 <em>(x, y)</em> 位置,半径为<em> r</em> ,根据<em>anticlockwise</em> (默认为顺时针)指定的方向从 <em>startAngle</em> 开始绘制,到 <em>endAngle</em> 结束。</dd> + <dt>{{domxref("CanvasRenderingContext2D.arcTo()", "Path2D.arcTo()")}}</dt> + <dd>根据控制点和半径添加一条圆弧路径,使用直线连接前一个点。</dd> + <dt>{{domxref("CanvasRenderingContext2D.ellipse()", "Path2D.ellipse()")}}</dt> + <dd>添加一条椭圆路径。椭圆的圆心在(x,y)位置,半径分别是<em>radiusX</em> 和 <em>radiusY</em> ,按照<em>anticlockwise</em> (默认顺时针)指定的方向,从 <em>startAngle </em> 开始绘制,到 <em>endAngle</em> 结束。</dd> + <dt>{{domxref("CanvasRenderingContext2D.rect()", "Path2D.rect()")}}</dt> + <dd>创建一条矩形路径,矩形的起点位置是 <em>(x, y) </em>,尺寸为 <em>width</em> 和 <em>height</em>。</dd> +</dl> + +<h2 id="Specifications" name="Specifications">规范描述</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', "scripting.html#dom-path2d", "Path2D")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("31")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>{{domxref("Path2D.addPath()")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("34")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("31")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>{{domxref("Path2D.addPath()")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("34")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("CanvasRenderingContext2D")}}</li> +</ul> diff --git a/files/zh-cn/web/api/path2d/path2d/index.html b/files/zh-cn/web/api/path2d/path2d/index.html new file mode 100644 index 0000000000..f606aa678d --- /dev/null +++ b/files/zh-cn/web/api/path2d/path2d/index.html @@ -0,0 +1,224 @@ +--- +title: Path2D() +slug: Web/API/Path2D/Path2D +translation_of: Web/API/Path2D/Path2D +--- +<div>{{APIRef("Canvas API")}}{{seeCompatTable}}</div> + +<p><code><strong>Path2D()</strong></code> 构造函数返回一个新的 <code>Path2D</code> 对象的实例,可以选择另一条路径作为参数(创建一个拷贝),或者选择 <a href="/en-US/docs/Web/SVG/Tutorial/Paths" style="line-height: 1.5;">SVG path</a><span style="line-height: 1.5;"> 数据构成的字符串。</span></p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">new Path2D(); +new Path2D(<em>path</em>); +new Path2D(<em>d</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>path</code> {{optional_inline}}</dt> + <dd>当调用另一个 <code style="font-style: normal; line-height: 1.5;">Path2D</code> 对象时,会创建一个 <code style="font-style: normal; line-height: 1.5;">path</code><span style="line-height: 1.5;"> 变量的拷贝。</span></dd> + <dt><code>d</code> {{optional_inline}}</dt> + <dd><span style="line-height: 1.5;">当调用 </span><a href="/en-US/docs/Web/SVG/Tutorial/Paths" style="line-height: 1.5;">SVG path</a><span style="line-height: 1.5;"> 数据构成的字符串</span><span style="line-height: 1.5;">时,会根据描述创建一个新的路径。</span></dd> +</dl> + +<dl> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="创建和拷贝路径">创建和拷贝路径</h3> + +<p>这是一段简单的代码片段,创建和拷贝 <code>Path2D</code> 路径。</p> + +<pre class="brush: js; highlight:[4,7]">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); + +var path1 = new Path2D(); +path1.rect(10, 10, 100,100); + +var path2 = new Path2D(path1); +path2.moveTo(220, 60); +path2.arc(170, 60, 50, 0, 2 * Math.PI); + +ctx.stroke(path2); +</pre> + +<p>修改下面的代码并在线查看 canvas 的变化:</p> + +<div style="display: none;"> +<h6 id="Playable_code">Playable code</h6> + +<pre class="brush: html"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> +<div class="playable-buttons"> + <input id="edit" type="button" value="Edit" /> + <input id="reset" type="button" value="Reset" /> +</div> +<textarea id="code" class="playable-code" style="height: 150px"> +var path1 = new Path2D(); +path1.rect(10, 10, 100,100); + +var path2 = new Path2D(path1); +path2.moveTo(220, 60); +path2.arc(170, 60, 50, 0, 2 * Math.PI); + +ctx.stroke(path2);</textarea> +</pre> + +<pre class="brush: js">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); +var textarea = document.getElementById("code"); +var reset = document.getElementById("reset"); +var edit = document.getElementById("edit"); +var code = textarea.value; + +function drawCanvas() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + eval(textarea.value); +} + +reset.addEventListener("click", function() { + textarea.value = code; + drawCanvas(); +}); + +edit.addEventListener("click", function() { + textarea.focus(); +}) + +textarea.addEventListener("input", drawCanvas); +window.addEventListener("load", drawCanvas); +</pre> +</div> + +<p>{{ EmbedLiveSample('Playable_code', 700, 420) }}</p> + +<h3 id="使用_SVG_路径">使用 SVG 路径</h3> + +<p>这是一段简单的代码片段,使用<span style="line-height: 1.5;"> </span><a href="/en-US/docs/Web/SVG/Tutorial/Paths" style="line-height: 1.5;">SVG path data</a><span style="line-height: 1.5;"> 创建一个 </span><code style="font-style: normal; line-height: 1.5;">Path2D</code><span style="line-height: 1.5;"> 路径。路径将会移动到点</span><span style="line-height: 1.5;"> (</span><code style="font-style: normal; line-height: 1.5;">M10 10</code><span style="line-height: 1.5;">) ,然后向右侧水平移动80个点 (</span><code style="font-style: normal; line-height: 1.5;">h 80</code><span style="line-height: 1.5;">),然后向下80个点 (</span><code style="font-style: normal; line-height: 1.5;">v 80</code><span style="line-height: 1.5;">),然后向左80个点(</span><code style="font-style: normal; line-height: 1.5;">h -80</code><span style="line-height: 1.5;">),最后回到起始点(</span><code style="font-style: normal; line-height: 1.5;">z</code><span style="line-height: 1.5;">)。</span></p> + +<pre class="brush: js; highlight:[4]">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); + +var p = new Path2D("M10 10 h 80 v 80 h -80 Z"); +ctx.fill(p); +</pre> + +<p>修改下面的代码并在线查看 canvas 的变化:</p> + +<div style="display: none;"> +<h6 id="Playable_code2">Playable code2</h6> + +<pre class="brush: html"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> +<div class="playable-buttons"> + <input id="edit" type="button" value="Edit" /> + <input id="reset" type="button" value="Reset" /> +</div> +<textarea id="code" class="playable-code"> +var p = new Path2D("M10 10 h 80 v 80 h -80 Z"); +ctx.fill(p);</textarea> +</pre> + +<pre class="brush: js">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); +var textarea = document.getElementById("code"); +var reset = document.getElementById("reset"); +var edit = document.getElementById("edit"); +var code = textarea.value; + +function drawCanvas() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + eval(textarea.value); +} + +reset.addEventListener("click", function() { + textarea.value = code; + drawCanvas(); +}); + +edit.addEventListener("click", function() { + textarea.focus(); +}) + +textarea.addEventListener("input", drawCanvas); +window.addEventListener("load", drawCanvas); +</pre> +</div> + +<p>{{ EmbedLiveSample('Playable_code2', 700, 360) }}</p> + +<h2 id="Specification" name="Specification">规范描述</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', 'scripting.html#dom-path2d', 'Path2D()')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop("31.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("31.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("Path2D")}}, 这个构造函数属于此接口。</li> +</ul> + +<div id="cke_pastebin" style="position: absolute; top: 459.5px; width: 1px; height: 1px; overflow: hidden; left: -1000px;"> </div> |