createRadialGradient Property

n this example, We’ve defined four different radial gradients. Because we have control over the start and closing points of the gradient, we can achieve more complex effects than we would normally have in the ‘classic’ radial gradients we see in, for instance, Photoshop. (i.e. a gradient with a single center point where the gradient expands outward in a circular shape.)
In this case, We’ve offset the starting point slightly from the end point to achieve a spherical 3D effect. It’s best to try to avoid letting the inside and outside circles overlap because this results in strange effects which are hard to predict.
The last color stop in each of the four gradients uses a fully transparent color. If you want to have a nice transition from this to the previous color stop, both colors should be equal. This isn’t very obvious from the code because We’ve used two different CSS color methods, but in the first gradient #019F62 = rgba(1,159,98,1)


        <title>Canvas tutorial</title>
            function draw(){
                var ctx = document.getElementById('canvas').getContext('2d');
                // Create gradients
                var radgrad = ctx.createRadialGradient(45, 45, 10, 52, 50, 30);
                radgrad.addColorStop(0, '#A7D30C');
                radgrad.addColorStop(0.9, '#019F62');
                radgrad.addColorStop(1, 'rgba(1,159,98,0)');
                var radgrad2 = ctx.createRadialGradient(105, 105, 20, 112, 120, 50);
                radgrad2.addColorStop(0, '#FF5F98');
                radgrad2.addColorStop(0.75, '#FF0188');
                radgrad2.addColorStop(1, 'rgba(255,1,136,0)');
                var radgrad3 = ctx.createRadialGradient(95, 15, 15, 102, 20, 40);
                radgrad3.addColorStop(0, '#00C9FF');
                radgrad3.addColorStop(0.8, '#00B5E2');
                radgrad3.addColorStop(1, 'rgba(0,201,255,0)');
                var radgrad4 = ctx.createRadialGradient(0, 150, 50, 0, 140, 90);
                radgrad4.addColorStop(0, '#F4F201');
                radgrad4.addColorStop(0.8, '#E4C700');
                radgrad4.addColorStop(1, 'rgba(228,199,0,0)');
                // draw shapes
                ctx.fillStyle = radgrad4;
                ctx.fillRect(0, 0, 150, 150);
                ctx.fillStyle = radgrad3;
                ctx.fillRect(0, 0, 150, 150);
                ctx.fillStyle = radgrad2;
                ctx.fillRect(0, 0, 150, 150);
                ctx.fillStyle = radgrad;
                ctx.fillRect(0, 0, 150, 150);
    <body onload="draw();">
        <canvas id="canvas" width="200" height="200">

You may also like...