【发布时间】:2011-09-19 11:18:12
【问题描述】:
我想在图像上获得径向渐变效果(alpha = 1 在中间,边缘透明)。
你有什么想法吗?
【问题讨论】:
-
看看这篇文章也许对你有帮助:stackoverflow.com/questions/5525874/…
-
我可以在圆圈上制作径向渐变,但我需要在图像上使用它。
标签: javascript gradient html5-canvas
我想在图像上获得径向渐变效果(alpha = 1 在中间,边缘透明)。
你有什么想法吗?
【问题讨论】:
标签: javascript gradient html5-canvas
如果我没记错你想要做的是:
globalCompositeOperation 设置将透明渐变与图像混合。您可以轻松地将其转换为代码:http://jsfiddle.net/W8Ywp/1/。
var ctx = $('#cv').get(0).getContext('2d');
var img = new Image();
img.src = 'http://www.netstate.com/states/'
+ 'symb/flowers/images/oklahoma_rose.jpg';
img.onload = function() {
ctx.drawImage(img, 0, 0, 300, 300); // Draw image
// Create gradient, from middle to borders
var gradient = ctx.createRadialGradient(150, 150, 0,
150, 150, 150);
// Opaque white in the middle
gradient.addColorStop(0, 'rgba(255,255,255,0)');
// Transparent white at the borders
gradient.addColorStop(1, 'rgba(255,255,255,1)');
ctx.globalCompositeOperation = 'destination-out';
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 300, 300); // Fill rectangle over image with the gradient
};
【讨论】:
ctx.globalCompositeOperation 设置可能会解决这个问题。