Masking effect using HTML5 canvas - Javascript Canvas

Javascript examples for Canvas:Mask

Description

Masking effect using HTML5 canvas

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <style id="compiled-css" type="text/css">

html, body {
   width: 100%
   height: 100%
}
#canvas1 {/*  w  w  w  . j  a v a  2  s  .  co  m*/
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
var circle = {
    posX: 0,
    posY: 0,
    hide: false
}
can.onmouseout = function(e) {
    circle.hide =  true;
}
can.onmouseover = function(e) {
    circle.hide = false;
}
window.onmousemove = function (e) {
    circle.posX = e.pageX-can.offsetLeft;
    circle.posY = e.pageY-can.offsetTop;
}
function redraw() {
    setTimeout(function () {
        requestAnimationFrame(redraw);
        can.width = can.width;
        ctx.drawImage(img, 0, 0);
        ctx.beginPath();
        ctx.rect(0, 0, 500, 500);
        if (!circle.hide) ctx.arc(circle.posX, circle.posY, 70, 0, Math.PI * 2, true);
        ctx.clip();
        ctx.fillRect(0, 0, 500, 500);
    }, 1000 / 60 /*1000 / Frames Per Second */ );
}
var img = new Image();
img.onload = function () {
    redraw({
        x: -500,
        y: -500
    })
}
img.src = 'http://placekitten.com/500/500';
    });

      </script> 
   </head> 
   <body> 
      <canvas id="canvas1" width="500" height="500"></canvas>  
   </body>
</html>

Related Tutorials