网络编程 
首页 > 网络编程 > 浏览文章

原生JavaScript实现刮刮乐

(编辑:jimmy 日期: 2024/5/8 浏览:3 次 )

本文实例为大家分享了JavaScript实现刮刮乐的具体代码,供大家参考,具体内容如下

原理

鼠标按住移动的时候,实现刮刮乐的效果,那就是鼠标按下的同时鼠标移动,那就清除画布。松开鼠标,鼠标移动不再清除画布了,那就得清除事件。

canvas画布

1、获取画布元素

var canvas = document.getElementById('canvas');

2、获取绘图对象getContext

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

3、画线

ctx.lineWidth = 3;//线宽
ctx.strokeStyle = 'red';//线条颜色
//开始的位置 moveTo(x,y);
//结束的位置lineTo(x,y)
//执行stroke()

4、矩形ctx.rect(x,y,width,height);

ctx.rect(0,0,300,150);
ctx.fillStyle = '#ccc';//填充的颜色
ctx.fill();//执行
ctx.clearRect(e.clientX,e.clientY,20,20);//清除矩形

页面

1、页面结构

<canvas id="canvas" width="300" height="150" style="border: 1px solid #ccc;"></canvas>
<div class="prize">谢谢惠顾</div>

2、样式

.prize {
 width: 300px;
 height: 150px;
 text-align: center;
 line-height: 150px;
 margin-top:-150px;
 color: red;
 font-size: 20px;
}

3、动画

//获取画布元素
 var canvas = document.getElementById('canvas');
 // 获取绘图对象
 var ctx = canvas.getContext('2d');
 ctx.lineWidth = 3;//线宽
 ctx.strokeStyle = 'red';//线条颜色
 //开始的位置 moveTo(x,y);
 //结束的位置lineTo(x,y)
 //执行stroke()

 //矩形
 ctx.rect(0,0,300,150);
 ctx.fillStyle = '#ccc';//填充的颜色
 ctx.fill();//执行
 ctx.clearRect(e.clientX,e.clientY,20,20);
 // 按下
 canvas.onmousedown = function (e){
  //移动
  canvas.onmousemove = function (e) {
   // ctx.lineTo(e.clientX,e.clientY);
   // ctx.lineTo(100,100)
   // ctx.stroke();
   ctx.clearRect(e.clientX,e.clientY,20,20);//清除
  }
 }
 canvas.onmouseup = function (e) {
  canvas.onmousemove = null;
 }
 // 改变中奖信息
 var arr = ['一个亿','现金500','100元话费','腾讯视频VIP月卡','谢谢惠顾'],
  prize = document.querySelector('.prize'),
  random = Math.floor(Math.random()*arr.length);
 prize.innerText = arr[random];

完整源码

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
  /*----------js刮刮乐------------*/
  .prize {
   width: 300px;
   height: 150px;
   text-align: center;
   line-height: 150px;
   margin-top:-150px;
   color: red;
   font-size: 20px;
  }
 </style>
</head>
<body>
<!--js刮刮乐-->
<canvas id="canvas" width="300" height="150" style="border: 1px solid #ccc;"></canvas>
<div class="prize">谢谢惠顾</div>

<script>
 // ------------js刮刮乐-----------
 //获取画布元素
 var canvas = document.getElementById('canvas');
 // 获取绘图对象
 var ctx = canvas.getContext('2d');
 ctx.lineWidth = 3;//线宽
 ctx.strokeStyle = 'red';//线条颜色
 //开始的位置 moveTo(x,y);
 //结束的位置lineTo(x,y)
 //执行stroke()

 //矩形
 ctx.rect(0,0,300,150);
 ctx.fillStyle = '#ccc';//填充的颜色
 ctx.fill();//执行
 ctx.clearRect(e.clientX,e.clientY,20,20);
 // 按下
 canvas.onmousedown = function (e){
  //移动
  canvas.onmousemove = function (e) {
   // ctx.lineTo(e.clientX,e.clientY);
   // ctx.lineTo(100,100)
   // ctx.stroke();
   ctx.clearRect(e.clientX,e.clientY,20,20);//清除
  }
 }
 canvas.onmouseup = function (e) {
  canvas.onmousemove = null;
 }
 // 改变中奖信息
 var arr = ['一个亿','现金500','100元话费','腾讯视频VIP月卡','谢谢惠顾'],
  prize = document.querySelector('.prize'),
  random = Math.floor(Math.random()*arr.length);
 prize.innerText = arr[random];
 // ------------js刮刮乐-----------
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:如何利用JS将手机号中间四位变成*号
下一篇:原生JavaScript实现拖动校验功能
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网