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 相关文章推荐
jquery的ajax从纯真网(cz88.net)获取IP地址对应地区名
Dec 02 Javascript
js 通用javascript函数库整理
Aug 14 Javascript
JS字符串累加Array不一定比字符串累加快(根据电脑配置)
May 14 Javascript
如何让浏览器支持jquery ajax load 前进、后退功能
Jun 12 Javascript
jQuery实现仿腾讯微博滑出效果报告每日天气的方法
May 11 Javascript
JavaScript 浏览器兼容性总结及常用浏览器兼容性分析
Mar 30 Javascript
浅谈jQuery before和insertBefore的区别
Dec 04 Javascript
webpack v4 从dev到prd的方法
Apr 02 Javascript
使用vue-cli打包过程中的步骤以及问题的解决
May 08 Javascript
koa2服务端使用jwt进行鉴权及路由权限分发的流程分析
Jul 22 Javascript
webpack打包优化的几个方法总结
Feb 10 Javascript
react组件基本用法示例小结
Apr 27 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
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
PHP大文件切割上传功能实例分析
2019/07/01 PHP
4种Windows系统下Laravel框架的开发环境安装及部署方法详解
2020/04/06 PHP
js可突破windows弹退效果代码
2008/08/09 Javascript
innerText和innerHTML 一些问题分析
2009/05/18 Javascript
高性能WEB开发 flush让页面分块,逐步呈现 flush让页面分块,逐步呈现
2010/06/19 Javascript
js日期时间补零的小例子
2013/03/05 Javascript
jquery控制select的text/value值为选中状态
2014/06/03 Javascript
Javascript递归打印Document层次关系实例分析
2015/05/15 Javascript
详解Vue 实例中的生命周期钩子
2017/03/21 Javascript
vue2.0实现移动端的输入框实时检索更新列表功能
2018/05/08 Javascript
Vue实现移动端页面切换效果【推荐】
2018/11/13 Javascript
Vue.js路由实现选项卡简单实例
2019/07/24 Javascript
Vue实现回到顶部和底部动画效果
2019/07/31 Javascript
解决Vue 移动端点击出现300毫秒延迟的问题
2020/07/21 Javascript
详解Node.JS模块 process
2020/08/31 Javascript
Python学习笔记之常用函数及说明
2014/05/23 Python
python实现的登录和操作开心网脚本分享
2014/07/09 Python
python机器学习之神经网络(三)
2017/12/20 Python
python占位符输入方式实例
2019/05/27 Python
python实现kNN算法识别手写体数字的示例代码
2019/08/16 Python
Python嵌套函数,作用域与偏函数用法实例分析
2019/12/26 Python
浅谈Tensorflow 动态双向RNN的输出问题
2020/01/20 Python
Python运行提示缺少模块问题解决方案
2020/04/02 Python
Python drop方法删除列之inplace参数实例
2020/06/27 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
2021/02/25 Python
如何实现一个自定义类的序列化
2012/05/22 面试题
群众路线教育实践活动方案
2014/02/02 职场文书
企业演讲稿范文大全
2014/05/20 职场文书
人力资源管理专业毕业生自荐书
2014/05/25 职场文书
国庆节标语大全
2014/10/08 职场文书
2014年药店工作总结
2014/11/20 职场文书
就业证明函
2015/06/17 职场文书
工人先锋号事迹材料(2016精选版)
2016/03/01 职场文书
导游词之南迦巴瓦峰
2019/11/19 职场文书
uniapp开发打包多端应用完整方法指南
2022/12/24 Javascript