欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手機端js和html5刮刮卡效果

 更新時間:2020年09月29日 14:42:55   作者:東成熙就  
這篇文章主要為大家詳細介紹了手機端js和html5刮刮卡效果,刮開之后是隨機生成的8位碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

H5的Canvas實現(xiàn)刮刮卡效果。 刮開之后是隨機生成的8位碼。

IE8不行...

<!DOCTYPE html> 
<html> 
<head> 
 <meta charset="utf-8"> 
 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 <meta name="viewport" content="width=device-width, initial-scale=1"> 
 <title>HTML5 刮刮卡</title> 
 <link rel="stylesheet" type="text/css" href="" /> 
 <style type="text/css"> 
 .demo{width:320px; margin:10px auto 20px auto; min-height:300px;} 
 .msg{text-align:center; height:32px; line-height:32px; font-weight:bold; margin-top:50px} 
 </style> 
</head> 
 
<body> 
 <div id="main"> 
 <h2 align="center">HTML5 刮刮卡(支持手機)</a></h2> 
 <div class="msg">刮開灰色部分看看,<a href="javascript:void(0)" onClick="window.location.reload()">再來一次</a></div> 
 <div class="demo"> 
  <canvas id="canvasBotm"></canvas> 
  <canvas id="canvasTop"></canvas> 
  
 </div> 
 </div> 
 
<script type="text/javascript"> 
 
 //生成隨機數(shù)據(jù)。n表示位數(shù) 
 var jschars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; 
 function generateMixed(n) { 
 var res = ""; 
 for(var i = 0; i < n ; i ++) { 
  var id = Math.ceil(Math.random()*61); 
  res += jschars[id]; 
 } 
 //alert(res); 
 return res; 
 }   
 
 //禁用頁面的鼠標選中拖動的事件 
 var bodyStyle = document.body.style; 
 bodyStyle.mozUserSelect = 'none'; 
 bodyStyle.webkitUserSelect = 'none'; 
 
 //定義圖片類,獲取canvas元素,并設置背景和位置屬性 
 var img = new Image(); 
 var canvas = document.getElementById('canvasTop'); 
 var canvasBotm = document.getElementById('canvasBotm'); 
 canvas.style.backgroundColor='transparent'; 
 canvas.style.position = 'absolute'; 
 canvasBotm.style.backgroundColor='#f3f3f3'; //驗證碼背景色 
 canvasBotm.style.position = 'absolute'; 
 
 var imgs = ['p_0.jpg','p_1.jpg']; 
 var num = Math.floor(Math.random()*2); 
 img.src = imgs[num]; 
 
 img.addEventListener('load', function(e) { 
 var ctx; 
 var w = img.width, 
  h = img.height; 
 var offsetX = canvas.offsetLeft, 
  offsetY = canvas.offsetTop; 
 var mousedown = false; 
 //函數(shù)layer()用來繪制一個灰色的正方形 
 function layer(ctx) { 
  ctx.fillStyle = 'grey'; 
  ctx.fillRect(0, 0, w, h); 
 } 
 function layerBotm(ctxBotm) { 
  ctxBotm.fillStyle = 'grey'; 
  ctxBotm.fillRect(0, 0, w, h); 
 } 
 //定義了按下事件 
 function eventDown(e){ 
  e.preventDefault(); 
  mousedown=true; 
 } 
 //定義了松開事件 
 function eventUp(e){ 
  e.preventDefault(); 
  mousedown=false; 
 } 
 //定義了移動事件 
 function eventMove(e){ 
  e.preventDefault(); 
  if(mousedown) {   //當按下時,獲取坐標位移,并通過arc(x, y, 10, 0, Math.PI * 2)來繪制小圓點 
   if(e.changedTouches){ 
   e=e.changedTouches[e.changedTouches.length-1]; 
   } 
   var x = (e.clientX + document.body.scrollLeft || e.pageX) - offsetX || 0, 
   y = (e.clientY + document.body.scrollTop || e.pageY) - offsetY || 0; 
   with(ctx) { 
   beginPath() 
   arc(x, y, 10, 0, Math.PI * 2); 
   fill(); 
   } 
  } 
 } 
  
 //通過canvas調(diào)用以上函數(shù),繪制圖形,并且偵聽觸控及鼠標事件,調(diào)用相應的函數(shù) 
 canvas.width=w; 
 canvas.height=h; 
 canvasBotm.width=w; 
 canvasBotm.height=h; 
 //canvas.style.backgroundImage='url('+img.src+')'; 
 //canvas.style.backgroundColor='#f3f3f3'; 
  
 ctxBotm=canvas.getContext("2d"); 
 ctx=canvasBotm.getContext("2d"); 
 ctx.font="50px Arial"; 
 
 // 創(chuàng)建漸變 
 var gradient=ctx.createLinearGradient(0,0,canvas.width,0); 
 gradient.addColorStop("0","magenta"); 
 gradient.addColorStop("0.5","blue"); 
 gradient.addColorStop("1.0","red"); 
  
 //實習字體 
 ctx.fillStyle=gradient; 
 ctx.fillText(generateMixed(8),40,90); 
 //空心字體 
 ctx.strokeStyle=gradient; 
 ctx.strokeText(generateMixed(8),40,90); 
  
 ctx=canvas.getContext('2d'); 
 ctx.fillStyle='transparent'; 
 ctx.fillRect(0, 0, w, h); 
 
 layerBotm(ctxBotm); 
 layer(ctx); 
 
 ctx.globalCompositeOperation = 'destination-out'; 
 
 canvas.addEventListener('touchstart', eventDown); 
 canvas.addEventListener('touchend', eventUp); 
 canvas.addEventListener('touchmove', eventMove); 
 canvas.addEventListener('mousedown', eventDown); 
 canvas.addEventListener('mouseup', eventUp); 
 canvas.addEventListener('mousemove', eventMove); 
 }); 
</script> 
</body> 
</html> 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • canvas學習之API整理筆記(二)

    canvas學習之API整理筆記(二)

    本篇文章的主要內(nèi)容包括高級動畫、像素操作、性能優(yōu)化等知識點,講解每個知識點的同時還會有一些酷炫的demo。下面跟著小編一起來看下吧
    2016-12-12
  • Boostrap入門準備之border box

    Boostrap入門準備之border box

    之前在學習Bootstrap的過程中,遇到各種奇葩的坑,如果在學習bootstrap之前,準備工作先做好,就可以或多或少的避開一些坑。下面小編開始給大家介紹border-box這個屬性的知識。感興趣的朋友一起學習吧
    2016-05-05
  • 如何利用Typescript封裝本地存儲

    如何利用Typescript封裝本地存儲

    這篇文章主要給大家介紹了關于如何利用Typescript封裝本地存儲的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • JS 使用 window對象的print方法實現(xiàn)分頁打印功能

    JS 使用 window對象的print方法實現(xiàn)分頁打印功能

    這篇文章主要介紹了JS 使用 window對象的print方法實現(xiàn)分頁打印功能,這種方法兼容性比較好,在ie和火狐瀏覽器下都可以正常使用,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-05-05
  • JavaScript代碼編寫中各種各樣的坑和填坑方法

    JavaScript代碼編寫中各種各樣的坑和填坑方法

    這篇文章主要介紹了JavaScript代碼編寫中各種各樣的坑和填坑方法,相信你肯定遇到過這些陷阱,而且陷入過,本文共計介紹了5種坑和填坑方法,需要的朋友可以參考下
    2014-06-06
  • 使用微信小程序開發(fā)彈出框應用實例詳解

    使用微信小程序開發(fā)彈出框應用實例詳解

    本文通過實例代碼給大家介紹了使用微信小程序開發(fā)彈出框功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • JS獲取CSS樣式(style/getComputedStyle/currentStyle)

    JS獲取CSS樣式(style/getComputedStyle/currentStyle)

    這篇文章主要為大家介紹了JS獲取CSS樣式的方法,介紹了CSS樣式的三種分類情況,對獲取樣式做一個簡單的封裝,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 利用JS測試目標網(wǎng)站的打開響應速度

    利用JS測試目標網(wǎng)站的打開響應速度

    本文簡單說明利用JS來測試目標網(wǎng)站的打開響應速度,方法簡單明了大家一看就明白并附上了腳本源碼
    2017-12-12
  • JavaScript 高仿真可控彈簧振子實現(xiàn)代碼

    JavaScript 高仿真可控彈簧振子實現(xiàn)代碼

    我剛學JavaScript ,看見一些牛人寫了許多特效,我也花了一天寫了一個彈簧振子,完全獨創(chuàng),沒有借鑒任何人的代碼.
    2009-10-10
  • JavaScript移除數(shù)組內(nèi)重復元素的方法

    JavaScript移除數(shù)組內(nèi)重復元素的方法

    這篇文章主要介紹了JavaScript移除數(shù)組內(nèi)重復元素的方法,實例分析了javascript遍歷數(shù)組及刪除等操作的相關技巧,需要的朋友可以參考下
    2015-03-03

最新評論