原生JavaScript實(shí)現(xiàn)刮刮樂
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)刮刮樂的具體代碼,供大家參考,具體內(nèi)容如下
原理
鼠標(biāo)按住移動(dòng)的時(shí)候,實(shí)現(xiàn)刮刮樂的效果,那就是鼠標(biāo)按下的同時(shí)鼠標(biāo)移動(dòng),那就清除畫布。松開鼠標(biāo),鼠標(biāo)移動(dòng)不再清除畫布了,那就得清除事件。
canvas畫布
1、獲取畫布元素
var canvas = document.getElementById('canvas');
2、獲取繪圖對(duì)象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 id="canvas" width="300" height="150" 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、動(dòng)畫
//獲取畫布元素 var canvas = document.getElementById('canvas'); // 獲取繪圖對(duì)象 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){ //移動(dòng) 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; } // 改變中獎(jiǎng)信息 var arr = ['一個(gè)億','現(xiàn)金500','100元話費(fèi)','騰訊視頻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 id="canvas" width="300" height="150" style="border: 1px solid #ccc;"></canvas> <div class="prize">謝謝惠顧</div> <script> // ------------js刮刮樂----------- //獲取畫布元素 var canvas = document.getElementById('canvas'); // 獲取繪圖對(duì)象 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){ //移動(dòng) 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; } // 改變中獎(jiǎng)信息 var arr = ['一個(gè)億','現(xiàn)金500','100元話費(fèi)','騰訊視頻VIP月卡','謝謝惠顧'], prize = document.querySelector('.prize'), random = Math.floor(Math.random()*arr.length); prize.innerText = arr[random]; // ------------js刮刮樂----------- </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JS+XML(數(shù)據(jù)島)實(shí)現(xiàn)分頁)
使用JS+XML(數(shù)據(jù)島)實(shí)現(xiàn)分頁)...2007-01-01javaScript之split與join的區(qū)別(詳解)
下面小編就為大家?guī)硪黄猨avaScript之split與join的區(qū)別(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11JavaScript使用readAsDataURL讀取圖像文件
這篇文章主要為大家詳細(xì)介紹了JavaScript使用readAsDataURL讀取圖像文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05屏蔽鼠標(biāo)右鍵、Ctrl+n、shift+F10、F5刷新、退格鍵 的javascript代碼
屏蔽鼠標(biāo)右鍵、Ctrl+n、shift+F10、F5刷新、退格鍵 的javascript代碼...2007-04-04javascript中的類,繼承,構(gòu)造函數(shù)詳解
這篇文章主要為大家詳細(xì)介紹了javascript中的類,繼承,構(gòu)造函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02