1. 什么是边界检测?
在之前的开发中,物体在运动过程中一旦超出画布,就会消失,今天学习如何去检测是否碰到了边界,碰到边界后又会有哪些处理的办法。
边界检测,就是物体运动的限制范围。边界检测的范围,既可以是画布的某个区域,也可以是整个画布。
下面是一个代码示例:


<canvas id="myCanvas"></canvas>
init() {
//定义一个球
class Ball {
constructor(x, y) {
this.x = x
this.y = y
this.radius = 10 //球的半径
this.color = 'red'
}
fill(context) {
context.beginPath()
context.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
context.fillStyle = this.color
context.fill()
context.closePath()
}
}
const cnv = document.getElementById('myCanvas')
const cxt = cnv.getContext('2d')
// 初始化一个小球
const ball = new Ball(0, cnv.height / 2)
const vx = 2
;(function frame(

最低0.47元/天 解锁文章
9878

被折叠的 条评论
为什么被折叠?



