diff options
Diffstat (limited to 'files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html')
-rw-r--r-- | files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html b/files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html new file mode 100644 index 0000000000..d7a1e1bde6 --- /dev/null +++ b/files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html @@ -0,0 +1,114 @@ +--- +title: CanvasRenderingContext2D.createLinearGradient() +slug: Web/API/CanvasRenderingContext2D/createLinearGradient +translation_of: Web/API/CanvasRenderingContext2D/createLinearGradient +--- +<div>{{APIRef}}</div> + +<p>The <code><strong>CanvasRenderingContext2D</strong></code><strong><code>.createLinearGradient()</code></strong> method of the Canvas 2D API creates a gradient along the line connecting two given coordinates.</p> + +<p><img alt="" src="https://mdn.mozillademos.org/files/14681/mdn-canvas-linearGradient.png" style="height: 121px; width: 264px;"></p> + +<p>This method returns a linear {{domxref("CanvasGradient")}}. To be applied to a shape, the gradient must first be assigned to the {{domxref("CanvasRenderingContext2D.fillStyle", "fillStyle")}} or {{domxref("CanvasRenderingContext2D.strokeStyle", "strokeStyle")}} properties.</p> + +<div class="note"> +<p><strong>Note:</strong> Gradient coordinates are global, i.e., relative to the current coordinate space. When applied to a shape, the coordinates are NOT relative to the shape's coordinates.</p> +</div> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">CanvasGradient <var>ctx</var>.createLinearGradient(<var>x0</var>, <var>y0</var>, <var>x1</var>, <var>y1</var>); +</pre> + +<p>The <code>createLinearGradient()</code> method is specified by four parameters defining the start and end points of the gradient line.</p> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>x0</code></dt> + <dd>The x-axis coordinate of the start point.</dd> + <dt><code>y0</code></dt> + <dd>The y-axis coordinate of the start point.</dd> + <dt><code>x1</code></dt> + <dd>The x-axis coordinate of the end point.</dd> + <dt><code>y1</code></dt> + <dd>The y-axis coordinate of the end point.</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<dl> + <dt>{{domxref("CanvasGradient")}}</dt> + <dd>A linear <code>CanvasGradient</code> initialized with the specified line.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<h3 id="Filling_a_rectangle_with_a_linear_gradient">Filling a rectangle with a linear gradient</h3> + +<p>This example initializes a linear gradient using the <code>createLinearGradient()</code> method. Three color stops between the gradient's start and end points are then created. Finally, the gradient is assigned to the canvas context, and is rendered to a filled rectangle.</p> + +<h4 id="HTML">HTML</h4> + +<pre class="brush: html"><canvas id="canvas"></canvas> +</pre> + +<h4 id="JavaScript">JavaScript</h4> + +<pre class="brush: js; highlight:[7]">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="Result">Result</h4> + +<p>{{ EmbedLiveSample('Filling_a_rectangle_with_a_linear_gradient', 700, 180) }}</p> + +<h2 id="Specifications">Specifications</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-context-2d-createlineargradient", "CanvasRenderingContext2D.createLinearGradient")}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.CanvasRenderingContext2D.createLinearGradient")}}</p> + +<h3 id="Gecko-specific_notes">Gecko-specific notes</h3> + +<ul> + <li>Starting with Gecko 2.0 {{geckoRelease("2.0")}}, specifying non-finite values now throws <code>NOT_SUPPORTED_ERR</code> instead of <code>SYNTAX_ERR</code>.</li> +</ul> + +<h2 id="See_also">See also</h2> + +<ul> + <li>The interface defining this method: {{domxref("CanvasRenderingContext2D")}}</li> + <li>{{domxref("CanvasRenderingContext2D.createRadialGradient()")}}</li> +</ul> |