canvas searchlight effect

Posted by cpharry on Wed, 04 Dec 2019 15:51:32 +0100

The clip() method in canvas is used to cut arbitrary shapes and sizes from the original canvas. Once an area is cut, all subsequent drawings are limited to the cut area (no access to other areas on the canvas)

You can also save the current canvas area by using the save() method before using the clip() method, and restore it at any time in the future by using the restore() method

  • Next, use the clip() method to achieve a searchlight effect
</iframe>
<button id="btn">Transformation</button>
<button id="con">suspend</button>
<canvas id="canvas" width="400" height="290" style="border:1px solid black">Current browser does not support canvas,Please change your browser and try again</canvas>
<script> btn.onclick = function(){history.go();}
con.onclick = function(){ if(this.innerHTML == 'suspend'){ this.innerHTML = 'recovery';
        clearInterval(oTimer);
    }else{ this.innerHTML = 'suspend'; 
        oTimer = setInterval(fnInterval,50);
    }
} var canvas = document.getElementById('canvas'); //Storage canvas width and height
var H=290,W=400; //Storage searchlight
var ball = {}; //Store photos
var IMG; //Store photo address
var URL = 'http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/chunfen.jpg'; function initial(){ if(canvas.getContext){ var cxt = canvas.getContext('2d'); var tempR = Math.floor(Math.random()*30+20); var tempX = Math.floor(Math.random()*(W-tempR) + tempR); var tempY = Math.floor(Math.random()*(H-tempR) + tempR)        
        ball = {
            x:tempX,
            y:tempY,
            r:tempR,
            stepX:Math.floor(Math.random() * 21 -10),
            stepY:Math.floor(Math.random() * 21 -10)
        };
        IMG = document.createElement('img');
        IMG.src=URL;
        IMG.onload = function(){
            cxt.drawImage(IMG,0,0);
      }//Welcome to the full stack development exchange circle to learn and exchange: 582735936
    ]//For front-end personnel for 1-3 years
  }   //Help to break through technical bottleneck and improve thinking ability
        }   
    }    
} function update(){
    ball.x += ball.stepX;
    ball.y += ball.stepY; 
    bumpTest(ball);
} function bumpTest(ele){ //Left
    if(ele.x <= ele.r){
        ele.x = ele.r;
        ele.stepX = -ele.stepX;
    } //Right
    if(ele.x >= W - ele.r){
        ele.x = W - ele.r;
        ele.stepX = -ele.stepX;
    } //Upper side
    if(ele.y <= ele.r){
        ele.y = ele.r;
        ele.stepY = -ele.stepY;
    } //Underside
    if(ele.y >= H - ele.r){
        ele.y = H - ele.r;
        ele.stepY = -ele.stepY;
    }
} function render(){ //Reset the height of canvas to empty the canvas
 canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d');
        cxt.save(); //Black the canvas background
 cxt.beginPath();
        cxt.fillStyle = '#000';
        cxt.fillRect(0,0,W,H); //Rendering searchlights
 cxt.beginPath();
        cxt.arc(ball.x,ball.y,ball.r,0,2*Math.PI);
        cxt.fillStyle = '#000';
        cxt.fill(); 
        cxt.clip(); //Because of the use of clip(), the canvas background image will appear in the clip() area
 cxt.drawImage(IMG,0,0);
        cxt.restore();
      }//Welcome to the full stack development exchange circle to learn and exchange: 582735936
    ]//For front-end personnel for 1-3 years
  }   //Help to break through technical bottleneck and improve thinking ability
    }

}
initial();
clearInterval(oTimer); function fnInterval(){ //Update motion state
 update(); //Rendering
 render();    
} var oTimer = setInterval(fnInterval,50); 
</script> 
 </pre>

Topics: Front-end