This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

createLinearGradient

Summary

Returns a linear CanvasGradient initialized with the specified line as represented by the start point (x0, y0) and end point (x1, y1) of the gradient.

Method of apis/canvas/CanvasRenderingContext2Dapis/canvas/CanvasRenderingContext2D

Syntax

var object = object.createLinearGradient(x0, y0, x1, y1);

Parameters

x0

Data-type
Number

The x-coordinate, in pixels, of the start point of the gradient.

y0

Data-type
Number

The y-coordinate, in pixels, of the start point of the gradient.

x1

Data-type
Number

The x-coordinate, in pixels, of the end point of the gradient.

y1

Data-type
Number

The y-coordinate, in pixels, of the end point of the gradient.

Return Value

Returns an object of type DOM NodeDOM Node

A CanvasGradient object that represents the new linear gradient.

Examples

This example creates a diagonal (upper left to lower right) gradient, fading from red to yellow, and then places a rectangle filled with the gradient onto the canvas.

<canvas id="myCanvas" width="300" height="150" style="border:1px solid blue;"></canvas>
<p>. . .</p>
<script>
var can = document.getElementById("myCanvas");
var ctxt = can.getContext("2d");
var grdt = ctxt.createLinearGradient(0, 0, 150, 150);
grdt.addColorStop(0, "red");
grdt.addColorStop(1, "yellow");
ctxt.fillStyle = grdt;
ctxt.fillRect(20, 20, 150, 100);
</script>

Related specifications

W3C HTML Canvas 2D Context
W3C Candidate Recommendation

Attributions