处理绘制方法慢?
我正在研究处理语言,并有这样的代码:
int fatness=30;
int step=20;
void drawCircle() {
fill(48, 139, 206);
ellipse(width - fatness, height - fatness, fatness, fatness);
}
void increaseCircle(){
fatness = fatness + step;
}
在另一个类中,我想调用 increaseCircle()
方法。但我希望它慢慢变大。我的意思是,在我的代码中,step 使其变大 20 倍,但我希望它变大,即如果可能的话,在 2 秒内使用动画。我怎样才能做到这一点?
编辑:当我从该类定义一个对象并调用increaseCircle方法时,它会变得越来越大,不会停止吗?
I am working on Processing language and have a code like that:
int fatness=30;
int step=20;
void drawCircle() {
fill(48, 139, 206);
ellipse(width - fatness, height - fatness, fatness, fatness);
}
void increaseCircle(){
fatness = fatness + step;
}
at another class I want to call the increaseCircle()
method. However I want it getting bigger slowly. I mean step makes it 20 more bigger at my code but I want it make bigger i.e. at 2 seconds if possible with an animation. How can I do that?
EDIT: When I define an object from that class and call increaseCircle method it gets bigger and bigger, doesn't stop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要根据时间更新值。
如果您存储开始时间,则可以随时使用 millis() 函数检查经过了多少时间。
一旦您知道过去了多少时间,您可以将该差异除以您想要更新变量的持续时间(在您的情况下为肥胖)。
这将为您提供 0.0 到 1.0 之间的值,您可以使用该值缩放/乘以变量的最终所需值(例如,肥胖度从 20 到 200)。
我的意思是:
在这个简单的演示中,您可以单击以重置动画并使用
-
和=
键来增加或减少动画持续时间。如果您习惯在Processing中使用外部库,您应该看看这个库 。
您可以运行以下演示:
Sounds like you need to update the values based on time.
If you store the start time, you can always check how much time passed using the millis() function.
Once you know how much time passed, you can divide that difference to the duration you want for updating your variable(fatness in you case).
This will give you value between 0.0 and 1.0, which you can use to scale/multiply to the final desired value for your variable (e.g. fatness goes from 20 to 200).
Here's what I mean:
In this simple demo you can click to reset the animation and use the
-
and=
keys to increase or decrease the animation duration.If you're comfortable with using external libraries in Processing, you should have a look at this library.
You can run a demo bellow: