canvas 实现刮刮乐

提出问题

在一个电商网站上如何实现刮刮乐的效果?

实现思路

用HTML5 的 canvas 标签画图

在解决问题前,我们先来了解一下 canvas 标签

canvas 是 html5 出现的新标签,像所有的 dom 对象一样它有自己本身的属性、方法和事件,其中就有绘图的方法,js 能够调用它来进行绘图。

context是一个封装了很多绘图功能的对象,获取这个对象的方法是 :

var canvas = document.querySelector('canvas');
var ctx = canvas.getContext("2d");

canvas元素绘制图像的时候有两种方法,分别是:

ctx.fill()//填充
ctx.stroke()//绘制边框

style:在进行图形绘制前,要设置好绘图的样式

ctx.fillStyle()//填充的样式
ctx.strokeStyle()//边框样式

ctx.fillRect(x, y, width, height)
绘制一个填充的矩形

ctx.clearRect(x, y, width, height)
清除指定矩形区域,让清除部分完全透明。

ctx.beginPath()
新建一条路径,生成之后,图形绘制命令被指向到路径上生成路径。

ctx.closePath()
闭合路径之后图形绘制命令又重新指向到上下文中。

ctx.stroke()
通过线条来绘制图形轮廓。

ctx.fill()
通过填充路径的内容区域生成实心的图形。

  • ctx.fill();与ctx. stroke()是对应的,一种是填充,一种是描线

ctx.arc()
画圆弧,有五个参数(x, y, 半径, 起始角度,结束角度,是否逆时针)

ctx.arc(75,75,50,0,Math.PI*2,true); // 绘制圆
ctx.arc(75,75,35,0,Math.PI,false);   // 口(顺时针)半圆

更多属性详情请点击这里

代码实现


  • 实现原理

首先,我们要禁用页面的鼠标选中拖动的事件,就是不运行执行选中操作。

var bodyStyle = document.body.style;
bodyStyle.mozUserSelect = 'none';
bodyStyle.webkitUserSelect = 'none';

接着我们定义图片类,获取canvas元素,并设置背景和位置属性。我们在本例中用到两张随机照片,每次刷新随机一张图片作为背景。
监听图片加载事件,定义gray颜色。
监听鼠标事件并画出图像。

js 代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>演示:使用HTML5实现刮刮卡效果</title>
<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">
   <div class="msg">刮开灰色部分看看,<a href="javascript:void(0)" onClick="window.location.reload()">再来一次</a></div>
   <div class="demo">
        <canvas></canvas>
   </div>
</div>

<script type="text/javascript">
var bodyStyle = document.body.style;
//禁用页面的鼠标选中拖动的事件,就是不运行执行选中操作
bodyStyle.mozUserSelect = 'none';
bodyStyle.webkitUserSelect = 'none';

var img = new Image();
var canvas = document.querySelector('canvas');

canvas.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;

    function eventDown(e){
      e.preventDefault();
      mousedown=true;
    }

    function eventUp(e){
      e.preventDefault();
      mousedown=false;
    }

    function eventMove(e){
      e.preventDefault();
        if(mousedown) {
        if(e.changedTouches){
          e=e.changedTouches[0];
        }
        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();
        }
      }
    }
    console.log(w)
    canvas.width=w;
    canvas.height=h;
    canvas.style.backgroundImage='url('+img.src+')';
    ctx=canvas.getContext('2d');
    ctx.fillStyle = 'gray';
    ctx.fillRect(0, 0, w, h);
    // 在前面银色图像下面画图
    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>

项目demo请点击这里

在这里再拓展一个canvas画图的实例——页面时钟
详细的解释在文中已有注释。
js代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <canvas id="canvas" width="500" height="500" style=""></canvas>
        <script>
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            function drawClock(){
                context.clearRect(0,0,500,500);
                var now = new Date();
                var sec = now.getSeconds();
                var min = now.getMinutes();
                var hours = now.getHours();
                //小时必须获取浮点类型 (小时+分钟/60)
                hours = hours+min/60;
                var hours = hours>12?hours-12:hours;

                context.lineWidth = 1;
                context.strokeStyle = "blue";
                context.beginPath();
                context.arc(250,250,200,0,360,false);
                context.closePath();
                context.stroke();

                for(var i=0;i<60;i++){
                    context.save();
                    context.beginPath();
                    context.lineWidth = 3;
                    context.strokeStyle = "#00aac5";
                    context.translate(250,250);
                    context.rotate(i*6*Math.PI/180);
                    context.moveTo(0,-180);
                    context.lineTo(0,-190);
                    context.closePath();
                    context.stroke();
                    context.restore();
                }

                                for(var i=0;i<12;i++){
                    //保存当前环境的状态
                  context.save();
                  context.lineWidth = 7;
                  context.strokeStyle = "#000";
                  // 转移原点
                  context.translate(250,250);
                  // 旋转
                  context.rotate(i*30*Math.PI/180);
                  context.beginPath();
                  context.moveTo(0,-160);
                  context.lineTo(0,-190);
                  context.closePath();
                  context.stroke();
                  // 返回之前保存过的路径状态和属性
                  context.restore();
                }
                // 绘制时针
                context.save();
                context.lineWidth = 5;
                context.strokeStyle = "#000";
                context.beginPath();
                context.translate(250,250);
                context.rotate(hours*30*Math.PI/180);
                context.moveTo(0,-130);
                context.lineTo(0,10);
                context.closePath();
                context.stroke();
                context.restore();
                // 绘制分针
                context.save();
                context.lineWidth = 3;
                context.strokeStyle = "#000";
                context.beginPath();
                context.translate(250,250);
                context.rotate(min*6*Math.PI/180);
                context.moveTo(0,-140);
                context.lineTo(0,10);
                context.closePath();
                context.stroke();
                context.restore();
                // 绘制秒针
                context.save();
                context.lineWidth = 1;
                context.strokeStyle = "#ff0000";
                context.beginPath();
                context.translate(250,250);
                context.rotate(sec*6*Math.PI/180);
                context.moveTo(0,-160);
                context.lineTo(0,15);
                context.closePath();
                context.stroke();
                context.restore();
            }
            // 1s 画一次并清除上一次图案
            setInterval(drawClock,1000);
        </script>
    </body>
</html>

项目DEMO请点击这里

猜你喜欢

转载自blog.csdn.net/weixin_38788347/article/details/78239704