国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

原生JavaScript實現刮刮樂

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

本文實例為大家分享了JavaScript實現刮刮樂的具體代碼,供大家參考,具體內容如下

原理

鼠標按住移動的時候,實現刮刮樂的效果,那就是鼠標按下的同時鼠標移動,那就清除畫布。松開鼠標,鼠標移動不再清除畫布了,那就得清除事件。

canvas畫布

1、獲取畫布元素

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

2、獲取繪圖對象getContext

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

3、畫線

ctx.lineWidth = 3;//線寬ctx.strokeStyle = ’red’;//線條顏色//開始的位置 moveTo(x,y);//結束的位置lineTo(x,y)//執行stroke()

4、矩形ctx.rect(x,y,width,height);

ctx.rect(0,0,300,150);ctx.fillStyle = ’#ccc’;//填充的顏色ctx.fill();//執行ctx.clearRect(e.clientX,e.clientY,20,20);//清除矩形

頁面

1、頁面結構

<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); //結束的位置lineTo(x,y) //執行stroke() //矩形 ctx.rect(0,0,300,150); ctx.fillStyle = ’#ccc’;//填充的顏色 ctx.fill();//執行 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 = [’一個億’,’現金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); //結束的位置lineTo(x,y) //執行stroke() //矩形 ctx.rect(0,0,300,150); ctx.fillStyle = ’#ccc’;//填充的顏色 ctx.fill();//執行 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 = [’一個億’,’現金500’,’100元話費’,’騰訊視頻VIP月卡’,’謝謝惠顧’], prize = document.querySelector(’.prize’), random = Math.floor(Math.random()*arr.length); prize.innerText = arr[random]; // ------------js刮刮樂-----------</script></body></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 桦川县| 芮城县| 嘉义市| 镇原县| 临漳县| 博湖县| 石棉县| 杭州市| 临安市| 樟树市| 凤山县| 都江堰市| 昔阳县| 新邵县| 雷山县| 德江县| 阿鲁科尔沁旗| 巴彦淖尔市| 翼城县| 思茅市| 交口县| 黄浦区| 桦甸市| 东辽县| 太湖县| 灵宝市| 浏阳市| 桃园市| 陇南市| 永寿县| 都安| 酉阳| 滕州市| 鱼台县| 高淳县| 科技| 日土县| 海淀区| 灵山县| 广西| 林甸县|