javascript 如何使用 HTML5 Canvas 绘制中心渐变渐变圆?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16494262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:55:03  来源:igfitidea点击:

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

javascripthtmlcanvasdrawgradient

提问by user2336726

How can I draw a circle with fadeing out gradients?

如何绘制渐变渐变的圆?

I mean something like this:

我的意思是这样的:

enter image description here

在此处输入图片说明

Getting darker and darker...

越来越黑了……

回答by Raymond Zhou

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

您想使用该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();

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

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