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 相关文章推荐
javascript oop开发滑动(slide)菜单控件
Aug 25 Javascript
Json对象与Json字符串互转(4种转换方式)
Mar 27 Javascript
浅谈Javascript 执行顺序
Dec 18 Javascript
jquery实现input输入框实时输入触发事件代码
Jan 28 Javascript
Javascript字符串对象的常用方法简明版
Jun 26 Javascript
jquery操作select元素和option的实例代码
Feb 03 Javascript
关于Jquery中的bind(),on()绑定事件方式总结
Oct 26 Javascript
VUE2.0中Jsonp的使用方法
May 22 Javascript
Angular6 写一个简单的Select组件示例
Aug 20 Javascript
Vue.extend 编程式插入组件的实现
Nov 18 Javascript
如何用JS模拟实现数组的map方法
Jul 30 Javascript
Node.js 中判断一个文件是否存在
Aug 24 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
DC《神奇女侠2》因疫情推迟上映 温子仁新恐怖片《恶性》撤档
2020/04/09 欧美动漫
如何在PHP中使用Oracle数据库(4)
2006/10/09 PHP
PHP 如何向 MySQL 发送数据
2006/10/09 PHP
php imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
Yii框架登录流程分析
2014/12/03 PHP
PHP 自动加载的简单实现(推荐)
2016/08/12 PHP
yii2高级应用之自定义组件实现全局使用图片上传功能的方法
2016/10/08 PHP
详解PHP数据压缩、加解密(pack, unpack)
2016/12/17 PHP
PHP计算近1年的所有月份
2017/03/13 PHP
jQuery之浮动窗口实现代码(两种方法)
2010/09/08 Javascript
基于jQuery的倒计时插件代码
2011/05/07 Javascript
JS获取各种浏览器窗口大小的方法
2014/01/14 Javascript
JavaScript两种跨域技术全面介绍
2014/04/16 Javascript
JS从数组中随机取出几个数组元素的方法
2016/08/02 Javascript
使用微信内嵌H5网页解决JS倒计时失效问题
2017/01/13 Javascript
JS实现手写parseInt的方法示例
2017/09/24 Javascript
AngularJs ng-change事件/指令的用法小结
2017/11/01 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
Vue.js组件props数据验证实现详解
2019/10/19 Javascript
JS call()及apply()方法使用实例汇总
2020/07/11 Javascript
python使用PyFetion来发送短信的例子
2014/04/22 Python
使用Python进行新浪微博的mid和url互相转换实例(10进制和62进制互算)
2014/04/25 Python
在Python的Django框架中获取单个对象数据的简单方法
2015/07/17 Python
Python实现的归并排序算法示例
2017/11/21 Python
python中将字典形式的数据循环插入Excel
2018/01/16 Python
python使用pygame框架实现推箱子游戏
2018/11/20 Python
python调用百度地图WEB服务API获取地点对应坐标值
2019/01/16 Python
Python调用C语言的实现
2019/07/26 Python
python分别打包出32位和64位应用程序
2020/02/18 Python
python读取当前目录下的CSV文件数据
2020/03/11 Python
python应用Axes3D绘图(批量梯度下降算法)
2020/03/25 Python
50道外企软件测试面试题
2014/08/18 面试题
个人找工作自荐信格式
2013/09/21 职场文书
工作疏忽检讨书
2014/01/25 职场文书
幼儿园大班开学教师寄语
2014/04/03 职场文书
对照检查剖析材料
2014/09/30 职场文书