javascript canvas检测小球碰撞


Posted in Javascript onApril 17, 2020

本文实例为大家分享了javascript canvas实现检测小球碰撞的具体代码,供大家参考,具体内容如下

定义一个canvas标签

<div class="cnavasInfo">
 <canvas
  id="canvas"
  width="800"
  height="500"
 ></canvas>
</div>

函数以及相关的逻辑处理

export default {
 data() {
  return {
   canvas: null,
   ctx: null,
   arcObj: {}
  };
 },
 mounted() {
  this.canvas = document.getElementById("canvas");
  this.ctx = this.canvas.getContext("2d");
  // this.move(); // 矩形的边缘碰撞函数
  // this.moveArc(); // 绘制碰撞圆形,对象形式
  this.moveRect()
 },
 methods: {
  move() {
   let x = 0;
   let y = 0;
   let width = 100;
   let height = 100;
   let speedX = 2;
   let speedY = 2;
   let ctx = this.ctx;
   ctx.fillStyle = "red";
   ctx.fillRect(x, y, width, height);
   setInterval(() => {
    ctx.clearRect(x, y, this.canvas.width, this.canvas.height);
    x += speedX;
    if (x > this.canvas.width - width) {
     speedX *= -1;
    } else if (x < 0) {
     speedX *= -1;
    }
    y += speedY;
    if (y > this.canvas.height - height) {
     speedY *= -1;
    } else if (y < 0) {
     speedY *= -1;
    }
    ctx.fillRect(x, y, width, height);
   }, 10);
   // this.requestmove(x,y,width,height,ctx,speedX,speedY); // 请求帧的动画过程
  },
  requestmove(x, y, width, height, ctx, speedX, speedY) {
   ctx.clearRect(x, y, this.canvas.width, this.canvas.height);
   x += speedX;
   if (x > this.canvas.width - width) {
    speedX *= -1;
   } else if (x < 0) {
    speedX *= -1;
   }
   y += speedY;
   if (y > this.canvas.height - height) {
    speedY *= -1;
   } else if (y < 0) {
    speedY *= -1;
   }
   ctx.fillRect(x, y, width, height);
   window.requestAnimationFrame(
    this.requestmove(x, y, width, height, ctx, speedX, speedY)
   );
  },
  moveArc(x, y, r, speedX, speedY) {
   this.x = x;
   this.y = y;
   this.r = r;
   this.speedX = speedX;
   this.speedY = speedY;
   this.moveUpdata = function() {
    this.x += this.speedX;
    if (this.x > this.canvas.width - this.r) {
     this.speedX *= -1;
    } else if (this.x < 0) {
     this.speedX *= -1;
    }
    this.y += this.speedY;
    if (this.y > this.canvas.height - this.r) {
     this.speedY *= -1;
    } else if (this.y < 0) {
     this.speedY *= -1;
    }
   };
  },
  moveRect(){
   // 面向对象编程
   function Rect(x,y,width,height,color,speedX,speedY,ctx,canvas) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color
    this.speedX = speedX
    this.speedY = speedY
    this.ctxRect = ctx
    this.canvas = canvas
   }
   Rect.prototype.draw = function() {
    this.ctxRect.beginPath();
    this.ctxRect.fillStyle = this.color
    this.ctxRect.fillRect(this.x,this.y,this.width,this.height)
    this.ctxRect.closePath();
   }
   Rect.prototype.move = function() {
    this.x += this.speedX
    if(this.x > this.canvas.width - this.width){
     this.speedX *= -1
    } else if(this.x < 0){
     this.speedX *= -1
    }
    this.y += this.speedY
    if(this.y > this.canvas.height - this.height){
     this.speedY *= -1
    } else if(this.y < 0){
     this.speedY *= -1
    }
   }
   let rect1 = new Rect(0,100,100,100,'red',2,2,this.ctx,this.canvas)
   let rect2 = new Rect(300,100,100,100,'blue',-2,-2,this.ctx,this.canvas)
   // rect1.draw();
   // rect2.draw()
   let animate = ()=>{
     this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height)
     rect1.draw()
     rect1.move()
     rect2.draw()
     rect2.move()
    let rect1Min = rect1.x;
    let rect1Max = rect1.x + rect1.width
    let rect2Min = rect2.x
    let rect2Max = rect2.x + rect2.width
    let min = Math.max(rect1Min,rect2Min)
    let max = Math.min(rect1Max,rect2Max)
    if(min < max){
     rect1.speedX *= -1;
     rect1.speedY *= 1;
     rect2.speedX *= -1
     rect2.speedY *= 1
    }
    window.requestAnimationFrame(animate)
   } 
   animate()
  }
 }
};

样式控制

#canvas {
 border: 1px solid black;
}

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

Javascript 相关文章推荐
Add Formatted Data to a Spreadsheet
Jun 12 Javascript
让新消息在网页标题闪烁提示的jQuery代码
Nov 04 Javascript
二叉树的非递归后序遍历算法实例详解
Feb 07 Javascript
node.js中的fs.ftruncate方法使用说明
Dec 15 Javascript
jquery实现页面常用的返回顶部效果
Mar 04 Javascript
js改变html的原有内容实现方法
Oct 05 Javascript
使用Ajax生成的Excel文件并下载的实例
Nov 21 Javascript
jQuery实现的背景颜色渐变动画效果示例
Mar 24 jQuery
Vue Spa切换页面时更改标题的实例代码
Jul 15 Javascript
vue2.0$nextTick监听数据渲染完成之后的回调函数方法
Sep 11 Javascript
JavaScript实现简单的弹窗效果
May 19 Javascript
Vue的props父传子的示例代码
May 20 Javascript
Vue实现浏览器打印功能的代码
Apr 17 #Javascript
基于JavaScript获取url参数2种方法
Apr 17 #Javascript
VSCode写vue项目一键生成.vue模版,修改定义其他模板的方法
Apr 17 #Javascript
vue fetch中的.then()的正确使用方法
Apr 17 #Javascript
如何基于filter实现网站整体变灰功能
Apr 17 #Javascript
javascript设计模式 ? 解释器模式原理与用法实例分析
Apr 17 #Javascript
javascript设计模式 ? 迭代器模式原理与用法实例分析
Apr 17 #Javascript
You might like
中东人咖啡哲学
2021/03/03 咖啡文化
php的正则处理函数总结分析
2008/06/20 PHP
PHP源码之 ext/mysql扩展部分
2009/07/17 PHP
PHP缓存集成库phpFastCache用法
2014/12/15 PHP
PHP实现的购物车类实例
2015/06/17 PHP
提高php编程效率技巧
2015/08/13 PHP
php实现水印文字和缩略图的方法示例
2016/12/29 PHP
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
2021/03/09 Servers
用dom+xhtml+css制作的一个相册效果代码打包下载
2008/01/24 Javascript
js或css文件后面跟参数的原因说明
2010/01/09 Javascript
jquery1.4.2 for Visual studio 2010 模板文件
2010/07/14 Javascript
Javascript检查图片大小不要让大图片撑破页面
2014/11/04 Javascript
详解JavaScript中的异常处理方法
2015/06/16 Javascript
JS实现超简洁网页title标题跑动闪烁提示效果代码
2015/10/23 Javascript
很实用的js选项卡切换效果
2016/08/12 Javascript
完美实现js选项卡切换效果(一)
2017/03/08 Javascript
react-native 完整实现登录功能的示例代码
2017/09/11 Javascript
vue-router配合ElementUI实现导航的实例
2018/02/11 Javascript
vue3.0 CLI - 2.1 -  component 组件入门教程
2018/09/14 Javascript
js面向对象封装级联下拉菜单列表的实现步骤
2021/02/08 Javascript
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
python实现telnet客户端的方法
2015/04/15 Python
python回调函数中使用多线程的方法
2017/12/25 Python
Python 字符串与二进制串的相互转换示例
2018/07/23 Python
pandas 转换成行列表进行读取与Nan处理的方法
2018/10/30 Python
python递归下载文件夹下所有文件
2019/08/31 Python
解决python 上传图片限制格式问题
2019/10/30 Python
简单了解Python读取大文件代码实例
2019/12/18 Python
python实现俄罗斯方块游戏(改进版)
2020/03/13 Python
社团活动总结报告
2014/06/27 职场文书
2014乡镇班子个人对照检查材料思想汇报
2014/09/26 职场文书
导游带团欢迎词
2015/09/30 职场文书
MySQL官方导出工具mysqlpump的使用
2021/05/21 MySQL
教你利用python实现企业微信发送消息
2021/05/23 Python
MySQL优化常用的19种有效方法(推荐!)
2022/03/17 MySQL
SONY AN-LP1 短波有源天线放大器图
2022/04/05 无线电