开发者

Fill image with a repeatable texture

Would it be possible to fill a png with transparency with a pattern (a repeatable texture)?

Here's a quick example of loading an image onto the canvas, just not sure how to fill it with a pattern, if that isn't possible then would there be a way to extract a path from the png?

<script>
    va开发者_高级运维r c = document.getElementById("a");
    var ctx = c.getContext("2d");

   var test= new Image();
    test.src = "images/test.png";
    test.onload = function() {
        ctx.drawImage(test, 0, 0);
    };
</script>

<body>
    <canvas id="a"></canvas>
</body>

I've also created a jsfiddle with an actual loaded png

This is the effect I'm looking to achieve

Fill image with a repeatable texture

Update

working example based on Simon Sarris' answer http://jsfiddle.net/sergeh/G8egW/6/


First, draw the image to Canvas.

Then do globalCompositeOperation = 'source-in';

Then draw the pattern. It will only exist where the image was.

http://jsfiddle.net/G8egW/2/

If you had stuff already on the canvas before this time, you'll need to do the above operations on an in-memory canvas and then draw that canvas to your normal canvas. Like this:

http://jsfiddle.net/G8egW/5/

(notice the difference in the grid)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜