温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用原生JavaScript写一个刮刮乐

发布时间:2020-10-29 17:56:49 来源:亿速云 阅读:174 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关使用原生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" ></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" ></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>

关于使用原生JavaScript写一个刮刮乐就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI