开发者

Javascript - Canvas - overlaying a transparent png on a previousley filled color

If I draw a rectangle on a canvas, fill it with a solid colour. Is it possible to fill it again with an transparent png, so that I can still see the original colour in the background?

Something like, dummy code -

  ctx.beginPath();
  ctx.lineTo( //draw a rectangle )
  ctx.fillStyle = "#FF0000" 
  ctx.f开发者_JS百科ill();
  var imageObj = new Image();
  function drawPattern() {
       var pattern = ctx.createPattern(imageObj, "repeat");
       ctx.fillStyle = pattern;
       ctx.fill();
  }
  imageObj.onload = drawPattern;
  imageObj.src = "images/dot.png"; //transparent png

I've tried a similar approach and it's not working.

Is there another way of doing this? Am I missing something?


What you've got will work just fine. Make sure you have a truly transparent PNG.

Here's a working example of your code:

http://jsfiddle.net/BDXc7/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜