<template>
<div class="guaguale">
    <!-- 背景图片 -->
    <!-- <img class="img-bg" src="./img/bg.png"/> -->
    <div class="guaguale-box">
		<img src="./img/jifen.png" style="display:none;"/>
		<img src="./img/logo.png" alt="logo" class="img-logo"/>
		<img src="./img/zhuti.png" alt="logo" class="img-zhuti"/>
		<div class="guajiang" id="guajiang">
			<img src="./img/libao.png" alt="logo" class="img-libao"/>
			<div class="guajiang-title">赢取大奖，拼人品的时候到了！</div>
			<div class="ggl">
				<div class="ggl-box" id="ggl_box">
					<div class="ggl-prize" v-show="showPrize"><span style="font-size: 2rem;">20</span>积分</div>
					<canvas id="canvas" class="ggl-canvas"></canvas>
				</div>
			</div>
		</div>
		<div class="btn-div">
			<button class="rule-btn" @click="showRule();"><span class="rule-icon"></span> 活动规则</button>
		</div>
    </div>
    <!-- 中奖 -->
    <div class="mask" v-show="prizeMask">
		<div class="w-prize">
			<div class="prize-name">活动积分奖励</div>
			<div class="prize-info"><span>20</span>积分</div>
			<a class="exchange">»前往兑换</a>
		</div>
		<div class="clear-div">
			<span class="clear" @click="clear()"></span>
		</div>
	</div>
	<!-- 规则 -->
    <div class="mask" v-show="ruleMask">
		<div class="rule-box">
			<div class="clear-gz">
				<span class="clear" @click="clear()"></span>
			</div>
			<img src="./img/gz.png" class="gz-title">
			<div class="rule-info">
				<h4>活动时间</h4>
				<p>2020/03/24</p>
				<h4>活动范围</h4>
				<p>全国（不包括港澳台地区）</p>
				<h4>活动奖品</h4>
				<p>一等奖</p>
				<p>二等奖</p>
				<p>三等奖</p>
				<h4>活动规则</h4>
				<p>用参与活动。。。</p>
			</div>
		</div>
		
	</div>
    
</div>
</template>

<script>
import { Toast } from "mint-ui";
import apiHelper from "@/assets/js/apiHelper.js";
import { Indicator } from "mint-ui";
import ut from "@/assets/js/utils.js";
import myimg from "./img/guajiangtu.png";

export default {
	name: "guaguale",
	props: ["comp", "cfg", "selectedPage"],
	data() {
		return {
			prizeMask:false,
			showPrize:false,
			ruleMask:false,
			guajiangFlag:false //防止多次刮奖
		};
	},
	created: function() {
		// var that = this;
		// this.mergeCompProps(this.comp);

		switch (this.sup8.env) {
			case this.sup8._def.env.design:
				this.env = "设计器模式";
				this.envType = "design";
				break;
			case this.sup8._def.env.preview:
				this.env = "预览模式";
				break;
			case this.sup8._def.env.run:
				this.env = "运行模式";
				break;
			default:
				break;
		}
	},
	mounted() {
		setTimeout(()=>{
			this.ggl();
		},800);
	},
	methods: {
		showRule(){
			this.ruleMask=true;
		},
		clear(){
			this.prizeMask = false;
			this.ruleMask = false;
		},
		ggl(){
			var device = /android|iphone|ipad|ipod|webos|iemobile|opear mini|linux/i.test(navigator.userAgent.toLowerCase());
			var startEvtName = device?"touchstart":"mousedown";
			var moveEvtName = device?"touchmove":"mousemove";
			var endEvtName = device?"touchend":"mouseup";

			var ggl_box = document.getElementById("ggl_box");
			var myCanvas = document.getElementById("canvas");
			var can = myCanvas.getContext("2d");
			var X = ggl_box.offsetWidth;
			var Y = ggl_box.offsetHeight;
			
			console.log("xy:",X,Y);
			var oImg = new Image();
			myCanvas.width = X;
			myCanvas.height = Y;
			oImg.src = myimg;
			oImg.onload = ()=>{
				can.beginPath();
				can.drawImage(oImg,0,0,X,Y);
				can.globalCompositeOperation = "destination-out";
				can.closePath();
				this.showPrize = true;
			}
			
			function draw(event){
				var offsetX =  ggl_box.getBoundingClientRect().left;
				var offsetY = ggl_box.getBoundingClientRect().top;
				var x = device?event.touches[0].clientX - offsetX:event.clientX- offsetX;
				var y = device?event.touches[0].clientY- offsetY:event.clientY- offsetY;
				can.beginPath();
				can.arc(x,y,15,0,Math.PI*2,false);
				can.fill();
				can.closePath();
			}
			//true  捕获 false  冒泡
			myCanvas.addEventListener(startEvtName,()=>{
				if(!this.guajiangFlag){
					myCanvas.addEventListener(moveEvtName,draw,false);
				}
			},false);
			myCanvas.addEventListener(endEvtName,()=>{
				if(!this.guajiangFlag){
					myCanvas.removeEventListener(moveEvtName,draw,false);
					this.check(can);
				}
			},false);
		},
		check(ctx){   
			//获取画布的像素列表
			let data = ctx.getImageData( 0,0,canvas.width,canvas.height).data;
			let n = 0;  //计算透明像素的个数
			for( let i = 0;i<data.length;i+=4 ){    //感觉这一步比较消耗性能
				//RGBA
				if( data[i]== 0 && data[i+1] ==0 && data[i+2] ==0&&data[i+3] ==0 ){
					n++
				}
			}
			let f = n*100/(canvas.width*canvas.height);  //算出所刮的面积的占比；
			console.log(`刮开面积:${f.toFixed(2)}%`);
			//刮开面积的比例
			if( f > 30 ){   //如果所刮的面积大于30%   则将canvas画布整体清除fillRect
				ctx.beginPath();
				ctx.fillRect( 0,0,canvas.width , canvas.height )
				this.prizeMask = true;
				this.guajiangFlag = true;
			}
		}
	}
};
</script>

<style scoped>
.guaguale{
  /* overflow: hidden; */
  /* position: relative; */
  min-height: 100vh;
  background: url("./img/bg.png");
  background-size: 100% 100%;
}
.img-bg {
	box-sizing: border-box;
	width: 100%;
	min-height: 100vh;
	overflow: hidden;
	z-index: 1;
}
.guaguale-box {
  /* position: relative; */
  padding-top: 2rem;
  /* left: 0;
  top: 0; */
  width: 100%;
}
.img-logo{
	display: block;
	width: 6rem;
	margin: 0rem auto 2rem auto;
}
.img-zhuti{
	display: block;
	width: 70%;
	margin: 0 auto 1.5rem;
}
.guajiang{
	width:90%;
	/* position: relative; */
	margin: 0 auto;
}
.img-libao{
	width:100%;
	display: block;
}
.guajiang-title{
	position: relative;
	width:100%;
	text-align: center;
	color: #da8a24;
	font-size: 1rem;
	font-weight: bold;
	background: linear-gradient(#fde27c, #fec665,#feaf52);
	height:4rem;
	line-height: 4rem;
	box-shadow: 0px 10px 10px #e65321;
	z-index: 2;
}
.ggl{
	box-sizing: border-box;
	padding: 1.5rem 1rem 1rem 1rem ;
    width: 90%;
    height: 13rem;
	margin: 0 auto;
    background: #fdc475;
}
.ggl-box{
	position: relative;
	width: 100%;
	height: 100%;
	background: #fff;
}
.ggl-prize{
	position: absolute;
	top:50%;
	left:50%;
	width:100%;
	transform:translate(-50%,-50%);
	text-align: center;
	color: #e84b49;
	font-weight: bold;
	z-index: 3;
}
.ggl-canvas{
	position: absolute;
	top:0;
	left:0;
	z-index: 5000;
}
.btn-div{
	padding: 2rem;
	text-align: center;
}
.rule-btn{
	width: 7rem;
	height: 2rem;
	border:1px solid #7a0409;
	background: transparent;
	border-radius: 2rem;
}
.rule-icon{
	display: inline-block;
	width: 0.9rem;
  	height: 1rem;
	background: url("./img/gzicon.png");
	background-size: 100% 100%;
	color: #7a0409;
	vertical-align: top;
}

.rule {
  position: absolute;
  width: 100%;
  top: 78%;
  text-align: center;
  font-size: 0.9rem;
  color: #404040;
}
.mask {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 10000;
}
.w-prize {
  position: fixed;
  padding: 0 4rem;
  top: 50%;
  left: 50%;
  width: 20rem;
  height: 22rem;
  margin-left: -10rem;
  margin-top: -11rem;
  background: url("./img/jifen.png") no-repeat;
  background-size: 100%;
  z-index: 10001;
  box-sizing: border-box;
  text-align: center;
  color: #fff;
}
.prize-name{
	margin-top: 6rem;
	margin-bottom: 2.5rem;
}
.prize-info{
	margin-bottom: 3.5rem;
	font-size: .9rem;
}
.prize-info span{
	font-size: 2.5rem;
}
.exchange{
	text-decoration:underline;
}
.exchange span{
	font-size:1.8rem;
	vertical-align: top;
}
.clear-div{
	padding: 1rem;
	text-align: right;
}
.clear{
	width: 1.5rem;
	height: 1.5rem;
	display: inline-block;
	background: url("./img/x.png");
	background-size: 100% 100%;
}
.rule-box{
	position: fixed;
	top: 50%;
	left: 50%;
	width: 80%;
	height: 26rem;
	margin-left: -40%;
	margin-top: -13rem;
	background-size: 100%;
	z-index: 10001;
	box-sizing: border-box;
	border: 3px solid #fed148;
	border-radius: 5px;
	background: #fff;
}
.clear-gz{
	position:absolute;
	padding: .8rem;
	top:0;
	left:0;
	width: 100%;
	text-align: right;
	box-sizing: border-box;
}
.gz-title{
	width: 100%;
}
.rule-info{
	padding: 0 .8rem;
	height: 20.5rem;
	overflow-y: scroll;
}
</style>
