这一篇讲canvas中三角函数的应用,首先讲一下思路,设置边界处理,应用边界反弹来实现方向的改变,固定x轴或y轴的速度,然后根据三角函数来确定另一个速度,可以参考下图来整理思路。
先定义一个构造函数
class rain{
constructor(props){
this.x = 0;
this.y = 0;
this.r = 1;
this.vx = 0;
this.vy = 0;
this.rotation = 0;
this.flag = 1;
this.fillStyle = `rgba(111,122,111,0.8)`;
this.strokeStyle = `rgba(0,0,0,0)`;
Object.assign(this,props)
return this;
}
createRain(ctx){
let {x,y,r} = this;
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,true);
ctx.closePath();
return this;
}
rander(ctx){
let {fillStyle,strokeStyle,rotation,x,y} = this;
ctx.save();
ctx.fillStyle = fillStyle;
ctx.strokeStyle = strokeStyle;
ctx.translate(x,y);
ctx.rotate(rotation);
this.createRain(ctx);