日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

HTML5-canvas实例:刮刮乐游戏

發(fā)布時間:2025/4/14 HTML 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HTML5-canvas实例:刮刮乐游戏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

實現(xiàn)方法:

  (1)利用canvas畫布,fillRect()描繪出一個矩形(不是透明),定位蓋在某個標(biāo)簽如div上面(這個標(biāo)簽寫著中獎的信息)

  (2)globalCompositeOperation = 'destination-out';利用此屬性,手指劃過畫布,arc(x,y, radius, 0, Math.PI*2, true)繪畫圓形,那么這個圓形會把之前描繪出的矩形穿透,看到div標(biāo)簽的內(nèi)容

globalCompositeOperation屬性: 

  globalCompositeOperation 屬性設(shè)置或返回如何將一個源(新的)圖像繪制到目標(biāo)(已有)的圖像上。

  源圖像 = 您打算放置到畫布上的繪圖。

  目標(biāo)圖像 = 您已經(jīng)放置在畫布上的繪圖。

值描述
source-over默認(rèn)。在目標(biāo)圖像上顯示源圖像。
source-atop在目標(biāo)圖像頂部顯示源圖像。源圖像位于目標(biāo)圖像之外的部分是不可見的。
source-in在目標(biāo)圖像中顯示源圖像。只有目標(biāo)圖像內(nèi)的源圖像部分會顯示,目標(biāo)圖像是透明的。
source-out在目標(biāo)圖像之外顯示源圖像。只會顯示目標(biāo)圖像之外源圖像部分,目標(biāo)圖像是透明的。
destination-over在源圖像上方顯示目標(biāo)圖像。
destination-atop在源圖像頂部顯示目標(biāo)圖像。源圖像之外的目標(biāo)圖像部分不會被顯示。
destination-in在源圖像中顯示目標(biāo)圖像。只有源圖像內(nèi)的目標(biāo)圖像部分會被顯示,源圖像是透明的。
destination-out在源圖像外顯示目標(biāo)圖像。只有源圖像外的目標(biāo)圖像部分會被顯示,源圖像是透明的。
lighter顯示源圖像 + 目標(biāo)圖像。
copy顯示源圖像。忽略目標(biāo)圖像。
source-over使用異或操作對源圖像與目標(biāo)圖像進行組合。

  如下圖,藍色是目標(biāo)圖像(就是矩形),紅色就是源圖像(即手指劃過的圓形)

?

  從圖中可以看出,globalCompositeOperation = 'destination-out'就是我們要的屬性

例子完整代碼:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>demo</title><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/><style type="text/css">.wraper{width: 300px;height: 100px;position: relative;margin: 150px auto;}.inner{width: 300px;height: 100px;background: #AA0707;color: #fff;font-size: 36px;line-height: 100px;text-align: center;}#j_canvas{position: absolute;left: 0;top: 0;}</style> </head> <body><div class="wraper"><div class="inner">恭喜您中獎</div><canvas id="j_canvas" width="300" height="100"></canvas></div><script type="text/javascript">var scratchGame = (function(){var target = document.getElementById('j_canvas'),ctx = target.getContext('2d'),w = target.width,h = target.height,radius = 15,target_offset_x = target.getBoundingClientRect().left,target_offset_y = target.getBoundingClientRect().top,isTouch = false; return{init:function(){ctx.fillStyle = '#999';ctx.fillRect(0, 0,w,h);//屬性設(shè)置或返回如何將一個源(新的)圖像繪制到目標(biāo)(已有)的圖像上。 ctx.globalCompositeOperation = 'destination-out';// 事件 target.addEventListener('touchstart', this.eventDown,false);target.addEventListener('touchmove', this.eventMove,false);target.addEventListener('touchend', this.eventUp,false);},eventDown:function(e){e.preventDefault();isTouch = true;},eventUp:function(e){e.preventDefault();isTouch = false;},eventMove:function(e){e.preventDefault();if(!isTouch||!e.touches.length) return;var touch = e.touches[0],x = touch.pageX - target_offset_x,y = touch.pageY - target_offset_y;ctx.beginPath(); ctx.arc(x,y, radius, 0, Math.PI*2, true); ctx.fill(); }}})();scratchGame.init();</script> </body> </html>

?

轉(zhuǎn)載于:https://www.cnblogs.com/focuslgy/p/4293534.html

總結(jié)

以上是生活随笔為你收集整理的HTML5-canvas实例:刮刮乐游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。