久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

原生JavaScript實(shí)現(xiàn)刮刮樂(lè)

瀏覽:2日期:2023-06-16 10:29:12

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)刮刮樂(lè)的具體代碼,供大家參考,具體內(nèi)容如下

原理

鼠標(biāo)按住移動(dòng)的時(shí)候,實(shí)現(xiàn)刮刮樂(lè)的效果,那就是鼠標(biāo)按下的同時(shí)鼠標(biāo)移動(dòng),那就清除畫(huà)布。松開(kāi)鼠標(biāo),鼠標(biāo)移動(dòng)不再清除畫(huà)布了,那就得清除事件。

canvas畫(huà)布

1、獲取畫(huà)布元素

var canvas = document.getElementById(’canvas’);

2、獲取繪圖對(duì)象getContext

var ctx = canvas.getContext(’2d’);

3、畫(huà)線

ctx.lineWidth = 3;//線寬ctx.strokeStyle = ’red’;//線條顏色//開(kāi)始的位置 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);//清除矩形

頁(yè)面

1、頁(yè)面結(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、動(dòng)畫(huà)

//獲取畫(huà)布元素 var canvas = document.getElementById(’canvas’); // 獲取繪圖對(duì)象 var ctx = canvas.getContext(’2d’); ctx.lineWidth = 3;//線寬 ctx.strokeStyle = ’red’;//線條顏色 //開(kāi)始的位置 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刮刮樂(lè)------------*/ .prize { width: 300px; height: 150px; text-align: center; line-height: 150px; margin-top:-150px; color: red; font-size: 20px; } </style></head><body><!--js刮刮樂(lè)--><canvas style='border: 1px solid #ccc;'></canvas><div class='prize'>謝謝惠顧</div><script> // ------------js刮刮樂(lè)----------- //獲取畫(huà)布元素 var canvas = document.getElementById(’canvas’); // 獲取繪圖對(duì)象 var ctx = canvas.getContext(’2d’); ctx.lineWidth = 3;//線寬 ctx.strokeStyle = ’red’;//線條顏色 //開(kāi)始的位置 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刮刮樂(lè)-----------</script></body></html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 交口县| 嫩江县| 商城县| 潼南县| 榆树市| 清水河县| 公主岭市| 房产| 错那县| 南雄市| 扎兰屯市| 凤城市| 白朗县| 宜黄县| 斗六市| 黔西县| 喀喇沁旗| 攀枝花市| 大城县| 九龙坡区| 镇宁| 博客| 河北区| 金寨县| 荆州市| 从化市| 宜君县| 祥云县| 石泉县| 柞水县| 深圳市| 新民市| 靖江市| 凉城县| 高陵县| 建湖县| 焉耆| 昭通市| 扶沟县| 双城市| 恩平市|