aboutsummaryrefslogtreecommitdiff
path: root/files/fa/web/api/canvasrenderingcontext2d/createlineargradient/index.html
blob: d7a1e1bde668638c1244a1ac8f33a6c6e10367f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</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>