In HTML5 canvas, how to mask an image with a background of my choice?

前端 未结 1 888
甜味超标
甜味超标 2021-02-03 10:43

I tried to make this happen with canvas\' globalCompositeOperation, but had no luck so I\'m asking here. There are similar questions here, but I did not find my cas

相关标签:
1条回答
  • 2021-02-03 11:27

    Here’s how to composite your “roof pattern” on top of your “house” using “roofOverlay”

    enter image description here

    This is a multi-part process:

    1. Draw the house on canvas#1.
    2. Draw the red roofOverlay on canvas#2.
    3. Set canvas#2’s context.globalCompositeOperation = 'source-in'
    4. Draw your desired pattern on canvas#2
    5. Compositing will cause your desired pattern to replace the red overlay—only in the red overlay area.

    Here is a Fiddle that loads grass on your roof: http://jsfiddle.net/m1erickson/SWP6v/

    And here is code that uses a lattice pattern fill on your roof:

    Note: I'm assuming that you want the house and roof on separate canvases so you can flip through a variety of roof choices. If you need everything on 1 canvas, you can just draw the roof canvas onto the house canvas.

    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    
    <style>
        body{ background-color: ivory; }
        canvas{border:1px solid red;}
        #container{position:relative;}
        #house{position:absolute; top:0; left:0;}
        #canvas{position:absolute; top:0; left:0;}
    </style>
    
    <script>
    $(function(){
    
        var house=document.getElementById("house");
        var ctxHouse=house.getContext("2d");
        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");
    
        var red=new Image();
        red.onload=function(){
           canvas.width=red.width;
           canvas.height=red.height;
    
           var houseImage=new Image();
           houseImage.onload=function(){
               house.width=houseImage.width;
               house.height=houseImage.height;
               ctxHouse.drawImage(houseImage,0,0);
           }
           houseImage.src="https://dl.dropbox.com/u/1122582/stackoverflow/house.png";
    
           ctx.drawImage(red,0,0);
    
            var imageObj = new Image();
            imageObj.onload = function() {
              var pattern = ctx.createPattern(imageObj, 'repeat');
              ctx.globalCompositeOperation = 'source-in';
              ctx.rect(0, 0, canvas.width, canvas.height);
              ctx.fillStyle = pattern;
              ctx.fill();
            };
            imageObj.src = "http://dl.dropbox.com/u/139992952/stackoverflow/lattice.jpg";
        }
        red.src="https://dl.dropbox.com/u/1122582/stackoverflow/roof-overlay.png";
    
    }); // end $(function(){});
    </script>
    
    </head>
    
    <body>
    <div id="container">
            <canvas id="house" width=300 height=300></canvas>
            <canvas id="canvas" width=300 height=300></canvas>
    </div>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题