溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用canvas怎么實(shí)現(xiàn)一個(gè)探照燈效果

發(fā)布時(shí)間:2021-04-14 15:53:37 來源:億速云 閱讀:195 作者:Leah 欄目:web開發(fā)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用canvas怎么實(shí)現(xiàn)一個(gè)探照燈效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

</iframe>
<button id="btn">變換</button>
<button id="con">暫停</button>
<canvas id="canvas" width="400" height="290" style="border:1px solid black">當(dāng)前瀏覽器不支持canvas,請(qǐng)更換瀏覽器后再試</canvas>
<script> btn.onclick = function(){history.go();}
con.onclick = function(){ if(this.innerHTML == '暫停'){ this.innerHTML = '恢復(fù)';
        clearInterval(oTimer);
    }else{ this.innerHTML = '暫停'; 
        oTimer = setInterval(fnInterval,50);
    }
} var canvas = document.getElementById('canvas'); //存儲(chǔ)畫布寬高
var H=290,W=400; //存儲(chǔ)探照燈
var ball = {}; //存儲(chǔ)照片
var IMG; //存儲(chǔ)照片地址
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);
      }//歡迎加入全棧開發(fā)交流圈一起學(xué)習(xí)交流:582735936
    ]//面向1-3年前端人員
  }   //幫助突破技術(shù)瓶頸,提升思維能力
        }   
    }    
} function update(){
    ball.x += ball.stepX;
    ball.y += ball.stepY; 
    bumpTest(ball);
} function bumpTest(ele){ //左側(cè)
    if(ele.x <= ele.r){
        ele.x = ele.r;
        ele.stepX = -ele.stepX;
    } //右側(cè)
    if(ele.x >= W - ele.r){
        ele.x = W - ele.r;
        ele.stepX = -ele.stepX;
    } //上側(cè)
    if(ele.y <= ele.r){
        ele.y = ele.r;
        ele.stepY = -ele.stepY;
    } //下側(cè)
    if(ele.y >= H - ele.r){
        ele.y = H - ele.r;
        ele.stepY = -ele.stepY;
    }
} function render(){ //重置畫布高度,達(dá)到清空畫布的效果
 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(),畫布背景圖片會(huì)出現(xiàn)在clip()區(qū)域內(nèi)
 cxt.drawImage(IMG,0,0);
        cxt.restore();
      }//歡迎加入全棧開發(fā)交流圈一起學(xué)習(xí)交流:582735936
    ]//面向1-3年前端人員
  }   //幫助突破技術(shù)瓶頸,提升思維能力
    }

}
initial();
clearInterval(oTimer); function fnInterval(){ //更新運(yùn)動(dòng)狀態(tài)
 update(); //渲染
 render();    
} var oTimer = setInterval(fnInterval,50); 
</script>

上述就是小編為大家分享的使用canvas怎么實(shí)現(xiàn)一個(gè)探照燈效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI