<b>Canvas Linear Gradient</b>
<p>In this practice problem, you will use createLinearGradient() method to draw a linear gradient of 7 colors of your choice . See the Javascript comments for guidance.</p>
<div>Canvas
<br/>
<canvas id="myCanvas" width="300" height="70"></canvas>
</div>
// your solutions here
// get the canvas and context objects
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext('2d');
//use createLinearGradient() method and specify the start and end points
var gradient = ctx.createLinearGradient(0, 0, 300, 0);
// create a gradient with 7 colors of your choice, each color should be of an equal distance of the gradient
//use CanvasGradient.addColorStop()method to define new stops on the gradient with specified offsets and colors
//check the MDN docs for more:
//https://developer.mozilla.org/en- US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient
gradient.addColorStop(0, 'Salmon');
gradient.addColorStop(1 / 6, 'LightPink');
gradient.addColorStop(2 / 6, 'pink');
gradient.addColorStop(3 / 6, 'pink');
gradient.addColorStop(4 / 6, 'PeachPuff');
gradient.addColorStop(5 / 6, 'PapayaWhip');
gradient.addColorStop(1, 'OldLace');
//apply the gradient by setting it as the fillStyle() and draw it to the canvas using fillRect()
ctx.fillStyle = gradient;
ctx.fillRect(0,0,300,70);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.