admin管理员组文章数量:1326515
I would like to obtain a radial gradient effect on an image (alpha = 1
in the middle and transparent on the edges).
Do you have any ideeas on how I could do that?
I would like to obtain a radial gradient effect on an image (alpha = 1
in the middle and transparent on the edges).
Do you have any ideeas on how I could do that?
Share Improve this question edited Sep 30, 2011 at 11:12 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Jun 21, 2011 at 11:57 gabitzishgabitzish 9,6917 gold badges46 silver badges67 bronze badges 2- take a look at this post maybe it will help you: stackoverflow./questions/5525874/… – Tim Commented Jun 21, 2011 at 11:59
- I can make radial gradients on circles, but I need it on images. – gabitzish Commented Jun 21, 2011 at 12:33
1 Answer
Reset to default 7If I'm not mistaking what you want to do is:
- Draw the image
- Draw a radial gradient over it, where the borders are transparent and the middle is opaque and using the
globalCompositeOperation
setting on the context to blend the transparency gradient with the image.
You can rather easily translate this into code: http://jsfiddle/W8Ywp/1/.
var ctx = $('#cv').get(0).getContext('2d');
var img = new Image();
img.src = 'http://wwwstate./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
};
本文标签: javascriptImage gradient on HTML5 canvasStack Overflow
版权声明:本文标题:javascript - Image gradient on HTML5 canvas - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742199234a2431647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论