我想使用P5.js库在javascript中制作一个简单的动画。我希望让球从某个高度出现,然后让它下落并反弹直到停止。
我不需要外部库,只需要P5。
我的代码如下:
function Ball() {
this.diameter = 50;
this.v_speed = 5;
this.ypos = height/2 - 100;
this.xpos = width/2;
this.update = function(){
this.ypos = this.ypos + this.v_speed;
this.ypos = constrain(this.ypos, this.diameter/2, height-this.diameter/2);
}
this.show = function(){
ellipse(this.xpos, this.ypos, this.diameter);
fill(255);
}
}
var ball;
function setup() {
createCanvas(600, 600);
ball = new Ball();
}
function draw() {
background(0);
ball.update();
ball.show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
有人可以帮我吗?非常感谢。