HTML Canvas Gradients

Multi tool use
Multi tool use







googletag.cmd.push(function() { googletag.display('div-gpt-ad-1422003450156-2'); });



HTML Canvas Gradients



❮ Previous
Next ❯



Canvas - Gradients


Gradients can be used to fill rectangles, circles, lines, text, etc. Shapes
on the canvas are not limited to solid colors.


There are two different types of gradients:



  • createLinearGradient(x,y,x1,y1) - creates a linear gradient

  • createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular gradient


Once we have a gradient object, we must add two or more color stops.


The addColorStop() method specifies the color stops, and its position along
the gradient. Gradient positions can be anywhere between 0 to 1.


To use the gradient, set the fillStyle or strokeStyle property to the
gradient, then draw the shape (rectangle, text, or a line).




Using createLinearGradient()




Example


Create a linear gradient. Fill rectangle with the gradient:



Your browser does not support the HTML5 canvas tag.

var c=document.getElementById("myCanvas6");
var canvOK=1;
try {c.getContext("2d");}
catch (er) {canvOK=0;}
if (canvOK==1)
{
var ctx=c.getContext("2d");
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
}

JavaScript:



var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");

// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);

Try it Yourself »








googletag.cmd.push(function() { googletag.display('div-gpt-ad-1493883843099-0'); });






Using createRadialGradient():



Example


Create a radial/circular gradient. Fill rectangle with the gradient:



Your browser does not support the HTML5 canvas tag.

var c=document.getElementById("myCanvas7");
var canvOK=1;
try {c.getContext("2d");}
catch (er) {canvOK=0;}
if (canvOK==1) {
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);
}

JavaScript:



var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");

// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);

Try it Yourself »




❮ Previous
Next ❯

iBkob8 8x hU5DXxlB9KbjJ T1BFFdy772
XQUPp877O2 qO 58EBBSFGCa6lX2R0CeBX9bn9y3fBn0ED5GS,tRfc4fblId6u qZdZ9QThPkrOcqKIx3TlYy,XSVbYE pFT i8iyH8l3

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS