[英]Smooth Concentric Radial Gradient in CSS
看起来您是在追求高斯模糊而不是渐变。
您可以使用ctx.filter
属性创建这样的模糊并传入 CSS 过滤器值"blur(Npx)"
。
但是 Safari 仍然不支持这个属性,所以对于这个浏览器,我们需要使用阴影作为解决方法。
const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); if (ctx.filter === "none") { ctx.filter = "blur(60px)"; } else { // Safari still doesn't support ctx.filter... ctx.shadowColor = "#34aaff"; ctx.shadowBlur = 120; // x2 ctx.shadowOffsetX = 800; ctx.translate(-800, 0); // we draw the actual shape outside of the visible context } ctx.arc(400, 400, 200, 0, Math.PI*2); ctx.lineWidth = 125; ctx.strokeStyle = "#34aaff"; ctx.stroke();
<canvas width=800 height=800></canvas>
您可以尝试添加一个或多个色标来控制形状:
gradient.addColorStop(0, "White");
gradient.addColorStop(0.3, "rgba(128, 128, 256, .5)");
gradient.addColorStop(0.4, "rgba(128, 128, 256, .5)");
gradient.addColorStop(1, "White");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.