The clip() method in canvas is used to cut any shape and size from the original canvas. Once an area is clipped, all subsequent drawings will be limited to the clipped area (other areas on the canvas cannot be accessed)
You can also use save before using the clip() method () method to save the current canvas area and restore it at any time in the future through the restore() method
Next, use the clip() method to achieve a searchlight effect
<button>变换</button><button>暂停</button><canvas>当前浏览器不支持canvas,请更换浏览器后再试</canvas><script>btn.onclick = function(){history.go();} con.onclick = function(){ if(this.innerHTML == '暂停'){ this.innerHTML = '恢复'; clearInterval(oTimer); }else{ this.innerHTML = '暂停'; oTimer = setInterval(fnInterval,50); } }var canvas = document.getElementById('canvas');//存储画布宽高var H=290,W=400;//存储探照灯var ball = {};//存储照片var IMG;//存储照片地址var URL = 'http://www.php.cn/';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); } } }function update(){ ball.x += ball.stepX; ball.y += ball.stepY; bumpTest(ball); }function bumpTest(ele){ //左侧 if(ele.x <= ele.r){ ele.x = ele.r; ele.stepX = -ele.stepX; } //右侧 if(ele.x >= W - ele.r){ ele.x = W - ele.r; ele.stepX = -ele.stepX; } //上侧 if(ele.y <= ele.r){ ele.y = ele.r; ele.stepY = -ele.stepY; } //下侧 if(ele.y >= H - ele.r){ ele.y = H - ele.r; ele.stepY = -ele.stepY; } }function render(){ //重置画布高度,达到清空画布的效果 canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d'); cxt.save(); //将画布背景涂黑 cxt.beginPath(); cxt.fillStyle = '#000'; cxt.fillRect(0,0,W,H); //渲染探照灯 cxt.beginPath(); cxt.arc(ball.x,ball.y,ball.r,0,2*Math.PI); cxt.fillStyle = '#000'; cxt.fill(); cxt.clip(); //由于使用了clip(),画布背景图片会出现在clip()区域内 cxt.drawImage(IMG,0,0); cxt.restore(); } } initial(); clearInterval(oTimer);function fnInterval(){ //更新运动状态 update(); //渲染 render(); }var oTimer = setInterval(fnInterval,50);</script>
For more canvas searchlight effects and related articles, please pay attention to the PHP Chinese website!

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

There is an HTML attribute that does exactly what you think it should do:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.