HTML Canvas clip and putImageData

后端 未结 1 463
悲哀的现实
悲哀的现实 2021-01-07 10:20

I have a canvas with a large image in the background and a smaller round image in front of it. I achieved this round image effect by using clip like so

ctx.s         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 11:12

    According to the specs,

    The current path, transformation matrix, shadow attributes, global alpha, the clipping region, and global composition operator must not affect the getImageData() and putImageData() methods.

    In your case, why are you using an additional canvas and pixel data manipulations? Why not just

    ctx.save();
    
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI*2, true);   
    ctx.closePath();
    ctx.clip();
    
    ctx.translate(x, y);
    ctx.drawImage(img, -r, -r, 2*r, 2*r);
    // not restoring context here, saving the clipping region and translation matrix
    
    // ... here goes the second part, wherever it is:
    ctx.save();
    ctx.rotate(a);
    ctx.drawImage(img, -r, -r, 2*r, 2*r);
    ctx.restore();
    

    0 讨论(0)
提交回复
热议问题