原生JavaScript實現(xiàn)刮刮樂
本文實例為大家分享了JavaScript實現(xiàn)刮刮樂的具體代碼,供大家參考,具體內(nèi)容如下
原理
鼠標(biāo)按住移動的時候,實現(xiàn)刮刮樂的效果,那就是鼠標(biāo)按下的同時鼠標(biāo)移動,那就清除畫布。松開鼠標(biāo),鼠標(biāo)移動不再清除畫布了,那就得清除事件。
canvas畫布
1、獲取畫布元素
var canvas = document.getElementById(’canvas’);
2、獲取繪圖對象getContext
var ctx = canvas.getContext(’2d’);
3、畫線
ctx.lineWidth = 3;//線寬ctx.strokeStyle = ’red’;//線條顏色//開始的位置 moveTo(x,y);//結(jié)束的位置lineTo(x,y)//執(zhí)行stroke()
4、矩形ctx.rect(x,y,width,height);
ctx.rect(0,0,300,150);ctx.fillStyle = ’#ccc’;//填充的顏色ctx.fill();//執(zhí)行ctx.clearRect(e.clientX,e.clientY,20,20);//清除矩形
頁面
1、頁面結(jié)構(gòu)
<canvas style='border: 1px solid #ccc;'></canvas><div class='prize'>謝謝惠顧</div>
2、樣式
.prize { width: 300px; height: 150px; text-align: center; line-height: 150px; margin-top:-150px; color: red; font-size: 20px;}
3、動畫
//獲取畫布元素 var canvas = document.getElementById(’canvas’); // 獲取繪圖對象 var ctx = canvas.getContext(’2d’); ctx.lineWidth = 3;//線寬 ctx.strokeStyle = ’red’;//線條顏色 //開始的位置 moveTo(x,y); //結(jié)束的位置lineTo(x,y) //執(zhí)行stroke() //矩形 ctx.rect(0,0,300,150); ctx.fillStyle = ’#ccc’;//填充的顏色 ctx.fill();//執(zhí)行 ctx.clearRect(e.clientX,e.clientY,20,20); // 按下 canvas.onmousedown = function (e){ //移動 canvas.onmousemove = function (e) { // ctx.lineTo(e.clientX,e.clientY); // ctx.lineTo(100,100) // ctx.stroke(); ctx.clearRect(e.clientX,e.clientY,20,20);//清除 } } canvas.onmouseup = function (e) { canvas.onmousemove = null; } // 改變中獎信息 var arr = [’一個億’,’現(xiàn)金500’,’100元話費’,’騰訊視頻VIP月卡’,’謝謝惠顧’], prize = document.querySelector(’.prize’), random = Math.floor(Math.random()*arr.length); prize.innerText = arr[random];
完整源碼
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style> /*----------js刮刮樂------------*/ .prize { width: 300px; height: 150px; text-align: center; line-height: 150px; margin-top:-150px; color: red; font-size: 20px; } </style></head><body><!--js刮刮樂--><canvas style='border: 1px solid #ccc;'></canvas><div class='prize'>謝謝惠顧</div><script> // ------------js刮刮樂----------- //獲取畫布元素 var canvas = document.getElementById(’canvas’); // 獲取繪圖對象 var ctx = canvas.getContext(’2d’); ctx.lineWidth = 3;//線寬 ctx.strokeStyle = ’red’;//線條顏色 //開始的位置 moveTo(x,y); //結(jié)束的位置lineTo(x,y) //執(zhí)行stroke() //矩形 ctx.rect(0,0,300,150); ctx.fillStyle = ’#ccc’;//填充的顏色 ctx.fill();//執(zhí)行 ctx.clearRect(e.clientX,e.clientY,20,20); // 按下 canvas.onmousedown = function (e){ //移動 canvas.onmousemove = function (e) { // ctx.lineTo(e.clientX,e.clientY); // ctx.lineTo(100,100) // ctx.stroke(); ctx.clearRect(e.clientX,e.clientY,20,20);//清除 } } canvas.onmouseup = function (e) { canvas.onmousemove = null; } // 改變中獎信息 var arr = [’一個億’,’現(xiàn)金500’,’100元話費’,’騰訊視頻VIP月卡’,’謝謝惠顧’], prize = document.querySelector(’.prize’), random = Math.floor(Math.random()*arr.length); prize.innerText = arr[random]; // ------------js刮刮樂-----------</script></body></html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫方法2. Jsp中request的3個基礎(chǔ)實踐3. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實現(xiàn)4. XML入門的常見問題(一)5. jsp EL表達(dá)式詳解6. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實例詳解7. Django程序的優(yōu)化技巧8. Django ORM實現(xiàn)按天獲取數(shù)據(jù)去重求和例子9. idea設(shè)置自動導(dǎo)入依賴的方法步驟10. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
