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/canvaspattern | |
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/canvaspattern')
-rw-r--r-- | files/zh-cn/web/api/canvaspattern/index.html | 108 | ||||
-rw-r--r-- | files/zh-cn/web/api/canvaspattern/settransform/index.html | 180 |
2 files changed, 288 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/canvaspattern/index.html b/files/zh-cn/web/api/canvaspattern/index.html new file mode 100644 index 0000000000..0fe68e2fb4 --- /dev/null +++ b/files/zh-cn/web/api/canvaspattern/index.html @@ -0,0 +1,108 @@ +--- +title: CanvasPattern +slug: Web/API/CanvasPattern +translation_of: Web/API/CanvasPattern +--- +<div> + {{APIRef("Canvas")}}</div> +<p><code><strong>CanvasPattern</strong></code> 接口表示描述一个模板(基于image, canvas或video)的不透明对象,通过 {{domxref("CanvasRenderingContext2D.createPattern()")}} 方法创建.</p> +<h2 id="属性">属性</h2> +<p><em>非透明对象,没有暴露出属性</em></p> +<h2 id="方法">方法</h2> +<p><em>没有继承来的方法</em></p> +<dl> + <dt> + {{domxref("CanvasPattern.setTransform()")}} {{experimental_inline}}</dt> + <dd> + 应用 {{domxref("SVGMatrix")}} 对模板做线性变换</dd> +</dl> +<h2 id="Specifications" name="Specifications">标准</h2> +<table class="standard-table"> + <thead> + <tr> + <th scope="col">标准</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', "the-canvas-element.html#canvaspattern", "CanvasPattern")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>新增 <code>setTransform()</code> 方法</td> + </tr> + <tr> + <td>{{SpecName('HTML Canvas 2D Context W3C', '#canvaspattern', 'CanvasPattern')}}</td> + <td>{{Spec2('HTML Canvas 2D Context W3C')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<div> + {{CompatibilityTable}}</div> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>功能</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>基本的支持</td> + <td>4.0</td> + <td>{{CompatGeckoDesktop("1.9.2")}}</td> + <td>9.0</td> + <td>9.0</td> + <td>3.1</td> + </tr> + <tr> + <td><code>setTransform()</code>{{experimental_inline}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("33")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>功能</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>基本的支持</td> + <td>2.1</td> + <td>{{CompatGeckoMobile("1.9.2")}}</td> + <td>{{CompatUnknown}}</td> + <td>10.0</td> + <td>3.2</td> + </tr> + <tr> + <td><code>setTransform()</code>{{experimental_inline}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("33")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="查看相关">查看相关</h2> +<ul> + <li>{{domxref("CanvasRenderingContext2D.createPattern()")}}</li> + <li>{{HTMLElement("canvas")}} 元素及和canvas相关接口 {{domxref("HTMLCanvasElement")}}</li> +</ul> diff --git a/files/zh-cn/web/api/canvaspattern/settransform/index.html b/files/zh-cn/web/api/canvaspattern/settransform/index.html new file mode 100644 index 0000000000..d53a0bf44b --- /dev/null +++ b/files/zh-cn/web/api/canvaspattern/settransform/index.html @@ -0,0 +1,180 @@ +--- +title: CanvasPattern.setTransform() +slug: Web/API/CanvasPattern/setTransform +translation_of: Web/API/CanvasPattern/setTransform +--- +<div>{{APIRef("Canvas API")}} {{SeeCompatTable}}</div> + +<p><code><strong>CanvasPattern</strong></code><strong><code>.setTransform()</code></strong> 方法使用 {{domxref("SVGMatrix")}} 对象作为图案的变换矩阵,并在此图案上调用它。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">void <var><em>pattern</em>.setTransform(matrix);</var> +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>matrix</code></dt> + <dd>{{domxref("SVGMatrix")}} ,被用作图案的变换矩阵。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用_setTransform_方法">使用 <code>setTransform</code> 方法</h3> + +<p>这是一段简单的代码片段,使用 <code>setTransform</code> 方法创建一个来自 {{domxref("SVGMatrix")}} 具有指定图案变化的{{domxref("CanvasPattern")}} 。如例子所示,如果你把图案赋值给当前的 {{domxref("CanvasRenderingContext2D.fillStyle", "fillStyle")}} ,当你使用 {{domxref("CanvasRenderingContext2D.fillRect", "fillRect()")}} 方法时,图案会被应用到 canvas 上绘制出效果。</p> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html"><canvas id="canvas"></canvas> +<svg id="svg1"></svg> +</pre> + +<h4 id="JavaScript">JavaScript</h4> + +<pre class="brush: js; highlight:[12]">var canvas = document.getElementById("canvas"); +var ctx = canvas.getContext("2d"); + +var svg1 = document.getElementById("svg1"); +var matrix = svg1.createSVGMatrix(); + +var img = new Image(); +img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png'; + +img.onload = function() { + var pattern = ctx.createPattern(img, 'repeat'); + pattern.setTransform(matrix.rotate(-45).scale(1.5)); + ctx.fillStyle = pattern; + ctx.fillRect(0,0,400,400); +}; +</pre> + +<p>修改下面的代码并在线查看 canvas 的变化:</p> + +<div class="hidden"> +<h6 id="Playable_code">Playable code</h6> + +<pre class="brush: html"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> +<svg id="svg1" style="display:none"></svg> +<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:120px"> +var img = new Image(); +img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png'; +img.onload = function() { + var pattern = ctx.createPattern(img, 'repeat'); + pattern.setTransform(matrix.rotate(-45).scale(1.5)); + ctx.fillStyle = pattern; + ctx.fillRect(0,0,400,400); +};</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; + +var svg1 = document.getElementById("svg1"); +var matrix = svg1.createSVGMatrix(); + +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, 380) }}</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-canvaspattern-settransform", "CanvasPattern.setTransform")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td> </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("33") }}</td> + <td>{{CompatUnknown}}</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>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>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{ CompatGeckoMobile("33") }}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>接口定义,{{domxref("CanvasPattern")}}</li> + <li>{{domxref("SVGMatrix")}}</li> +</ul> |