如何使用HTML5画布绘制具有居中淡出渐变的圆圈?

use*_*726 11 javascript html5 gradient canvas draw

如何绘制渐弱渐变的圆圈?

我的意思是这样的:

在此输入图像描述

变暗和变暗......

小智 23

您想要使用该ctx.createRadialGradient()方法.

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();
Run Code Online (Sandbox Code Playgroud)

示例: http ://jsfiddle.net/razh/sA6Wc/