Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Draw a Circle with Centered Fadeing Out Gradients with HTML5 Canvas?

How can I draw a circle with fadeing out gradients?

I mean something like this:

enter image description here

Getting darker and darker...

like image 472
user2336726 Avatar asked May 11 '13 05:05

user2336726


People also ask

How will you create radial gradients in html5 canvas?

The createRadialGradient() method creates a radial/circular gradient object. The gradient can be used to fill rectangles, circles, lines, text, etc. Tip: Use this object as the value to the strokeStyle or fillStyle properties.

What is radial gradient in CSS?

radial-gradient() The radial-gradient() CSS function creates an image consisting of a progressive transition between two or more colors that radiate from an origin. Its shape may be a circle or an ellipse. The function's result is an object of the <gradient> data type, which is a special kind of <image> .


1 Answers

You want to use the ctx.createRadialGradient() method.

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var x = 100,
    y = 75,
    // Radii of the white glow.
    innerRadius = 5,
    outerRadius = 70,
    // Radius of the entire circle.
    radius = 60;

var gradient = ctx.createRadialGradient(x, y, innerRadius, x, y, outerRadius);
gradient.addColorStop(0, 'white');
gradient.addColorStop(1, 'blue');

ctx.arc(x, y, radius, 0, 2 * Math.PI);

ctx.fillStyle = gradient;
ctx.fill();

Example: http://jsfiddle.net/razh/sA6Wc/

like image 122
Raymond Zhou Avatar answered Oct 02 '22 00:10

Raymond Zhou