4

我想在图像上获得径向渐变效果(alpha = 1在中间,边缘透明)。

你对我怎么做有任何想法吗?

4

1 回答 1

6

如果我没有弄错你想要做的是:

  1. 绘制图像
  2. 在它上面绘制一个径向渐变,其中边界是透明的,中间是不透明的,并使用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
};
于 2011-06-21T12:56:12.593 回答