码迷,mamicode.com
首页 > 其他好文 > 详细

canvas擦拭效果-刮刮卡

时间:2016-05-13 08:58:27      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>刮刮卡</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<style>
body,div{padding: 0; margin: 0;}
.wrap {
    width: 320px;
    height: 400px;
    background: url(‘http://s1.ifengimg.com/2015/05/27/14a99b65f24474a81530c6f642c1ef4f.jpg‘) 0 0 no-repeat;
    background-size: 100% 100%;
}
</style>
</head>
<body>
<div id="con" class="wrap">
    <canvas id="myCanvas" width="320" height="400"></canvas>
</div>
<script>
var myCanvas = document.getElementById(‘myCanvas‘);
var gray = myCanvas.getContext(‘2d‘);
gray.beginPath();
gray.fillStyle = "#999";
gray.fillRect(0,0,320,400);
gray.closePath();
gray.globalCompositeOperation="destination-out";

myCanvas.addEventListener(‘touchmove‘, function(e){
    e.preventDefault();
    gray.beginPath();
    gray.fillStyle = "#f00";
    gray.arc(e.targetTouches[0].clientX, e.targetTouches[0].clientY, 20, 0, Math.PI*2);
    gray.fill();
    gray.closePath();
}, false);

myCanvas.addEventListener(‘touchend‘, function(e){
    e.preventDefault();
    var num = 0;
    var datas = gray.getImageData(0,0,320,400);//画布中像素点属性
    for (var i = 0; i < datas.data.length; i++) {
        if (datas.data[i] == 0) {
            num++;
        };
    };
    if (num >= datas.data.length * 0.7) {
        gray.fillRect(0,0,320,400);
    };
}, false);
</script>
</body>
</html>

TJ

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name=‘viewport‘ content=‘width=device-width, initial-scale=1.0, maximum-scale=1.0‘/>
<link rel="stylesheet" href="http://s0.ifengimg.com/2016/03/30/style_e14bff55.css">
<script src="http://s0.ifengimg.com/static/js/jquery-1.7.2.min_c4de8813.js"></script>
<script src="http://m1.ifengimg.com/20150227/fa.min.js"></script>
</head>
<body>
<section>
    <h1 class="scrape-title imgBox">
        <img src="http://s0.ifengimg.com/2016/03/29/sec2_tt_5b5367e8.png">
    </h1>
    <div class="award">
    	<div class="canvas_bg_1" style="display: none;"><img src="http://s0.ifengimg.com/2016/03/29/cbg1_0b525d45.png" /></div>
    	<div class="canvas_bg_2"><img src="http://s0.ifengimg.com/2016/03/29/cbg2_79dd3c21.png" /></div>
    	<div class="award-wrap">
    		<div class="award-txt">
	    		<h4 style="display: none;"><&$gift&></h4>
	    		<p style="display: none;">凭此页面截图详询工作人员领取</p>
	    		<canvas id="awardCanvas"></canvas>
	    	</div>
    	</div>
    </div>
    <!--<dl class="actInfo">
    	<dt>活动说明</dt>
    	<dd>
    		<p>1、奖品以实物为准,详询凤凰房产展台。</p>
    		<p>2、奖品数量有限,先到先得,送完即止。</p>
    		<p>3、您可关注凤凰会员中心微信公众账号更多精彩活动邀您参加!</p>
    	</dd>
    </dl>-->
</section>
<script>
$("html").css("fontSize",$(window).width()/64 + "px");//1rem = 640 / 64
$(document).ready(function(){
	var awardCanvas = document.getElementById(‘awardCanvas‘);
	var img = new Image();
	img.src = $(".canvas_bg_2 img").attr("src");
	var timer = setInterval(function(){
        if(img.complete){
            clearInterval(timer);
            img = null;
            var offsetTop = $(".award-txt").offset().top;
			var offsetLeft = $(".award-txt").offset().left;
			var w = $(".award-txt").width();
			var h = $(".award-txt").height();
			if(awardCanvas){
				awardCanvas.width = w;
				awardCanvas.height = h;
				$(".award-txt h4").show();
				$(".award-txt p").show();
				var gray = awardCanvas.getContext(‘2d‘);
				gray.beginPath();
				gray.fillStyle = "#d3d3d3";
				gray.fillRect(0,0,w,h);
				gray.closePath();
				gray.globalCompositeOperation="destination-out";
				document.addEventListener(‘touchstart‘, function(e){
					e.preventDefault();
					awardCanvas.addEventListener(‘touchmove‘, function(e){
					    e.preventDefault();
					    gray.beginPath();
					    //gray.fillStyle = "#f00";
					    gray.arc(e.targetTouches[0].clientX - offsetLeft, e.targetTouches[0].clientY - offsetTop, 20, 0, Math.PI*2);
					    gray.fill();
					    gray.closePath();
					}, false);
				
					awardCanvas.addEventListener(‘touchend‘, function(e){
					    e.preventDefault();
					    var num = 0;
					    var datas = gray.getImageData(0,0,w,h);//画布中像素点属性
					    for (var i = 0; i < datas.data.length; i++) {
					        if (datas.data[i] == 0) {
					            num++;
					        };
					    };
					    if (num >= datas.data.length * 0.4) {
					        gray.fillRect(0,0,w,h);
					        $(‘#awardCanvas‘).hide();
					        $(‘.canvas_bg_2‘).hide();
					        $(‘.canvas_bg_1‘).show();
					        
					    };
					}, false);
				});
			}
        }
    }, 50);		
});

</script>
<script>
var _hmt = _hmt || [];
(function(){
	var hm = document.createElement("script");
	hm.src = "//hm.baidu.com/hm.js?9d8e6da658c2614d29e51e46cedce92e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>
<script>var sta_collection_time = new Date().getTime();</script>
<script id="sta_collection_new">
window.analytics(encodeURIComponent(window.location.href));
window.aptracker.collection();
window.remain();
</script>
</body>
</html>

  

canvas擦拭效果-刮刮卡

标签:

原文地址:http://www.cnblogs.com/mbyund/p/5485394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!