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/canvasrenderingcontext2d/createlineargradient | |
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/canvasrenderingcontext2d/createlineargradient')
-rw-r--r-- | files/zh-cn/web/api/canvasrenderingcontext2d/createlineargradient/index.html | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/canvasrenderingcontext2d/createlineargradient/index.html b/files/zh-cn/web/api/canvasrenderingcontext2d/createlineargradient/index.html new file mode 100644 index 0000000000..d7e9536d37 --- /dev/null +++ b/files/zh-cn/web/api/canvasrenderingcontext2d/createlineargradient/index.html @@ -0,0 +1,156 @@ +--- +title: CanvasRenderingContext2D.createLinearGradient() +slug: Web/API/CanvasRenderingContext2D/createLinearGradient +translation_of: Web/API/CanvasRenderingContext2D/createLinearGradient +--- +<div>{{APIRef}}</div> + +<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.createLinearGradient()</code></strong>方法创建一个沿参数坐标指定的直线的渐变。</p> + +<p><img alt="" src="https://mdn.mozillademos.org/files/14681/mdn-canvas-linearGradient.png" style="height: 121px; width: 264px;"></p> + +<p>该方法返回一个线性 {{domxref("CanvasGradient")}}对象。想要应用这个渐变,需要把这个返回值赋值给 {{domxref("CanvasRenderingContext2D.fillStyle", "fillStyle")}} 或者 {{domxref("CanvasRenderingContext2D.strokeStyle", "strokeStyle")}}。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">CanvasGradient <var><em>ctx</em>.createLinearGradient(x0, y0, x1, y1);</var> +</pre> + +<p><code>createLinearGradient()</code> 方法需要指定四个参数,分别表示渐变线段的开始和结束点。</p> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>x0</code></dt> + <dd>起点的 x 轴坐标。</dd> + <dt><code>y0</code></dt> + <dd>起点的 y 轴坐标。</dd> + <dt><code>x1</code></dt> + <dd>终点的 x 轴坐标。</dd> + <dt><code>y1</code></dt> + <dd>终点的 y 轴坐标。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>{{domxref("CanvasGradient")}}</dt> + <dd>一个根据指定线路初始化的线性<code> </code><span style="font-family: consolas,monaco,andale mono,monospace;"><code>CanvasGradient</code> 对象。</span></dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用线性渐变填充一个矩形">使用线性渐变填充一个矩形</h3> + +<p>这个例子使用<code>createLinearGradient()</code> 方法初始化了一个线性渐变。在这个线性渐变中添加了三种色彩。最后,这个渐变被赋值给上下文对应的属性,实现了对矩形的填充。</p> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html notranslate"><canvas id="canvas"></canvas> +</pre> + +<h4 id="JavaScript">JavaScript</h4> + +<pre class="notranslate">var canvas = document.getElementById('canvas'); +var ctx = canvas.getContext('2d'); + +// Create a linear gradient +// The start gradient point is at x=20, y=0 +// The end gradient point is at x=220, y=0 +var gradient = ctx.createLinearGradient(20,0, 220,0); + +// Add three color stops +gradient.addColorStop(0, 'green'); +gradient.addColorStop(.5, 'cyan'); +gradient.addColorStop(1, 'green'); + +// Set the fill style and draw a rectangle +ctx.fillStyle = gradient; +ctx.fillRect(20, 20, 200, 100);</pre> + +<h4 id="结果">结果</h4> + +<div style="display: none;"> +<h6 id="Playable_code">Playable code</h6> + +<pre class="brush: html notranslate"><canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> +<div class="playable-buttons"> + <input id="edit" type="button" value="编辑" /> + <input id="reset" type="button" value="重置" /> +</div> +<textarea id="code" class="playable-code"> +var gradient = ctx.createLinearGradient(0,0,200,0); +gradient.addColorStop(0,"green"); +gradient.addColorStop(1,"white"); +ctx.fillStyle = gradient; +ctx.fillRect(10,10,200,100);</textarea> +</pre> + +<pre class="brush: js notranslate">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('Filling_a_rectangle_with_a_linear_gradient', 700, 180) }}</p> + +<h2 id="规范描述">规范描述</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-createlineargradient", "CanvasRenderingContext2D.createLinearGradient")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("api.CanvasRenderingContext2D.createLinearGradient")}}</p> + + + +<h3 id="Gecko特性说明">Gecko特性说明</h3> + +<ul> + <li>自 Gecko 2.0 {{geckoRelease("2.0")}}开始,参数指定了极值会抛出<code>NOT_SUPPORTED_ERR</code> 而不再是之前的 <code>SYNTAX_ERR </code>异常。</li> +</ul> + +<h2 id="参见"><br> + <span style="">参见</span></h2> + +<div id="compat-mobile"></div> + +<ul> + <li>接口定义: {{domxref("CanvasRenderingContext2D")}}</li> + <li>{{domxref("CanvasRenderingContext2D.createRadialGradient()")}}</li> +</ul> |