js實現(xiàn)PC端和移動端刮卡效果
更新時間:2020年03月27日 16:05:20 作者:最騷的就是你
這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)PC端和移動端刮卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js刮卡效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

具體代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>小月博客刮刮卡案例分享</title>
<script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
}
body {
background: #E34830;
position: relative;
}
.banner1 {
display: block;
width: 100%;
/*height: auto;*/
overflow: hidden;
}
.ggl {
position: relative;
width: 85.6%;
height: 90px;
margin: -5px auto;
background: url(img/ggl.png) no-repeat center center;
background-size: 100% 100%;
border: 1px solid blue;
}
.canvas {
position: absolute;
top: 2px;
left: 2.5%;
width: 95%;
height: 82px;
line-height: 82px;
text-align: center;
z-index: 2;
border: 1px solid black;
}
.info {
position: absolute;
top: 2px;
left: 2.5%;
width: 95%;
height: 82px;
text-align: center;
}
.info span {
display: block;
font-size: 18px;
}
#prompt {
line-height: 40px;
}
.btn {
position: relative;
width: 50%;
height: 35px;
line-height: 35px;
background: #df412b;
color: #fff;
border-radius: 5px;
margin: 0 auto;
z-index: 1;
}
.guize {
display: block;
width: 85.6%;
height: auto;
margin: 5% auto 10% auto;
border-radius: 5px;
border: 1px solid black;
}
.num {
width: 90%;
margin: 0 auto;
height: 30px;
line-height: 30px;
text-align: center;
font-size: 14px;
margin-top: 5%;
border: 1px solid black;
}
#ok,
#no {
display: none;
}
.pop {
position: fixed;
left: 0;
top: 0;
z-index: 3;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
display: none;
}
.pop img {
width: 100%;
height: auto;
overflow: hidden;
margin: 15% auto;
}
</style>
<script>
//控制刮卡次數(shù)
var t = 0;
//初始化所有數(shù)據(jù)并且隨機(jī)產(chǎn)生獎品
var initialize = function() {
//剩余刮卡次數(shù)
$('.num1').html(4 - t);
//隨機(jī)數(shù)
function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
var r = getRandomNum(1, 100);
var btn = document.getElementsByClassName("btn");
for (var i = 0; i < btn.length; i++) {
btn[i].style.zIndex = '1';
}
document.getElementById("no").style.display = "none";
document.getElementById("ok").style.display = "none";
//初始化涂抹面積
isOk = 0;
if (r < t * 33) {
document.getElementById("prompt").innerHTML = "恭喜您,中獎了!"
var ok = document.getElementById("ok");
ok.style.display = "block";
//點擊領(lǐng)取獎品
ok.onclick = function() {
window.location.href = "prize.html"
};
} else {
document.getElementById("prompt").innerHTML = "很遺憾,未中獎!"
document.getElementById("no").style.display = "block";
}
};
var c1; //畫布
var ctx; //畫筆
var ismousedown; //標(biāo)志用戶是否按下鼠標(biāo)或開始觸摸
var isOk = 0; //標(biāo)志用戶是否已經(jīng)刮開了一半以上
var fontem = parseInt(window.getComputedStyle(document.documentElement, null)["font-size"]); //這是為了不同分辨率上配合@media自動調(diào)節(jié)刮的寬度
/* 頁面加載后開始初始化畫布 */
window.onload = function() {
initialize();
c1 = document.getElementById("c1");
//這里很關(guān)鍵,canvas自帶兩個屬性width、height,我理解為畫布的分辨率,跟style中的width、height意義不同。
//最好設(shè)置成跟畫布在頁面中的實際大小一樣
//不然canvas中的坐標(biāo)跟鼠標(biāo)的坐標(biāo)無法匹配
c1.width = c1.clientWidth;
c1.height = c1.clientHeight;
ctx = c1.getContext("2d");
//PC端的處理
c1.addEventListener("mousemove", eventMove, false);
c1.addEventListener("mousedown", eventDown, false);
c1.addEventListener("mouseup", eventUp, false);
//移動端的處理
c1.addEventListener('touchstart', eventDown, false);
c1.addEventListener('touchend', eventUp, false);
c1.addEventListener('touchmove', eventMove, false);
//初始化
initCanvas();
}
//初始化畫布,畫灰色的矩形鋪滿
function initCanvas() {
//網(wǎng)上的做法是給canvas設(shè)置一張背景圖片,我這里的做法是直接在canvas下面另外放了個div。
//c1.style.backgroundImage="url(中獎圖片.jpg)";
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = '#aaaaaa';
ctx.fillRect(0, 0, c1.clientWidth, c1.clientHeight);
ctx.fill();
ctx.font = "Bold 30px Arial";
ctx.textAlign = "center";
ctx.fillStyle = "#999999";
ctx.fillText("刮一刮", c1.width / 2, 50);
//把這個屬性設(shè)為這個就可以做出圓形橡皮擦的效果
//有些老的手機(jī)自帶瀏覽器不支持destination-out,下面的代碼中有修復(fù)的方法
ctx.globalCompositeOperation = 'destination-out';
}
//鼠標(biāo)按下 和 觸摸開始
function eventDown(e) {
e.preventDefault();
ismousedown = true;
}
//鼠標(biāo)抬起 和 觸摸結(jié)束
function eventUp(e) {
e.preventDefault();
//得到canvas的全部數(shù)據(jù)
var a = ctx.getImageData(0, 0, c1.width, c1.height);
var j = 0;
for (var i = 3; i < a.data.length; i += 4) {
if (a.data[i] == 0) j++;
}
//當(dāng)被刮開的區(qū)域等于一半時,則可以開始處理結(jié)果
if (j >= a.data.length / 8) {
isOk = 1;
}
ismousedown = false;
}
//鼠標(biāo)移動 和 觸摸移動
function eventMove(e) {
e.preventDefault();
if (ismousedown) {
if (e.changedTouches) {
e = e.changedTouches[e.changedTouches.length - 1];
}
var topY = document.getElementById("top").offsetTop;
var oX = c1.offsetLeft,
oY = c1.offsetTop + topY;
var x = (e.clientX + document.body.scrollLeft || e.pageX) - oX || 0,
y = (e.clientY + document.body.scrollTop || e.pageY) - oY || 0;
//畫360度的弧線,就是一個圓,因為設(shè)置了ctx.globalCompositeOperation = 'destination-out';
//畫出來是透明的
ctx.beginPath();
ctx.arc(x, y, fontem * 1.2, 0, Math.PI * 2, true);
//下面3行代碼是為了修復(fù)部分手機(jī)瀏覽器不支持destination-out
//我也不是很清楚這樣做的原理是什么
c1.style.display = 'none';
c1.offsetHeight;
c1.style.display = 'inherit';
ctx.fill();
}
if (isOk) {
var btn = document.getElementsByClassName("btn");
for (var i = 0; i < btn.length; i++) {
btn[i].style.zIndex = '3';
}
document.getElementsByClassName("btn")[0].style.zIndex = "3";
}
}
//沒有中獎再來一次
$("#no").click(function() {
if (t > 3) {
//因該彈出遮罩層提示您的次數(shù)已經(jīng)用完了
$('.pop1').show();
$('.pop1 img').click(function() {
$('.pop1').hide();
})
} else {
t++;
//初始化按鈕
document.getElementById("no").style.display = "none";
document.getElementById("ok").style.display = "none";
window.onload();
initCanvas();
}
});
</script>
</head>
<body>
<img src="img/banner1.png" class="banner1" />
<div class="ggl" id="top">
<div class="info" id="prize">
<span id="prompt"></span>
<span class="btn" id="ok">領(lǐng)取獎品</span>
<span class="btn" id="no">再來一次</span>
</div>
<canvas id="c1" class="canvas"></canvas>
</div>
<div class="num">
您還有<span class="num1"></span>次刮卡機(jī)會
</div>
<img src="img/guize.png" class="guize" />
<!-- 遮罩層1抽獎次數(shù)已經(jīng)用完-->
<div class="pop pop1">
<img src="img/pop1.png" />
</div>
<div class="pop pop2">
<img src="img/pop2.png" id="pop2" />
</div>
</body>
</html>
源碼下載:js刮卡效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uni-app和原生小程序混合開發(fā)的具體實現(xiàn)過程
最近項目中遇到了一些功能需要與原生進(jìn)行混合開發(fā),所以下面這篇文章主要給大家介紹了關(guān)于uni-app和原生小程序混合開發(fā)的具體實現(xiàn)過程,需要的朋友可以參考下2022-07-07
微信小程序開發(fā)之好友列表字母列表跳轉(zhuǎn)對應(yīng)位置
這篇文章主要介紹了微信小程序開發(fā)之好友列表字母列表跳轉(zhuǎn)對應(yīng)位置的相關(guān)資料,希望通過本文能幫助到大家讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
javascript的console.log()用法小結(jié)
console.log 原先是 Firefox 的 專利 ,嚴(yán)格說是安裝了 Firebugs 之后的 Firefox 所獨有的調(diào)試 絕招2012-05-05
關(guān)于promise和async用法以及區(qū)別詳解
Promise是一個構(gòu)造函數(shù),我們就可以new Promise()得到一個 Promise的實例,下面這篇文章主要給大家介紹了關(guān)于promise和async用法以及區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-01-01
javascript 中的try catch應(yīng)用總結(jié)
這篇文章主要介紹了javascript 中的try catch應(yīng)用總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
JS求1到任意數(shù)之間的所有質(zhì)數(shù)的方法詳解
這篇文章主要介紹了JS求1到任意數(shù)之間的所有質(zhì)數(shù),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05

