绘制多个对象时,processingjs 闪烁

发布于 2024-10-26 23:04:44 字数 1571 浏览 6 评论 0原文

在处理js中,我遇到了麻烦。我写的代码有效,但仅适用于一圈。如果有两个或更多圆圈,它们就会开始闪烁(我猜测这是由于背景()的刷新率较慢所致)。我的代码中是否做错了什么(下面发布),或者这只是processingjs速度的限制?

我确信一定有一种方法可以达到相同的效果而不会出现延迟。我已经看到处理工作做得更多,延迟更少。

此外,当两个圆圈重叠时,它们也会开始闪烁(大约是两倍)。有什么办法可以解决这个问题吗?

我的代码:


    int count;
    int[] circles;
    int numCircles;
    int color, color1, color2;
    void setup()
    {
    size($(document.body).width(),600);
    smooth();
    numCircles = 1;
    color = random(0,200);
    color1 = random(0, 200);
    color2 = random(0, 200);
    strokeWeight( 10 );
frameRate( 60 );

count = 0;
circles = new int[numCircles*4];
for(int x = 0; x<circles.length; x+=4)
{
    circles[x]=random(0,width);
    circles[x+1]=random(0,height);
    if(random(0,1)==0)
    {
     circles[x+2] = 1;
    }
    else
    {
     circles[x+2] = -1;
    }
    if(random(0,1)==0)
    {
     circles[x+3] = 1;
    }
    else
    {
     circles[x+3] = -1;
    }

}


}



void draw()
{


background(255);
fill(255);
stroke(color, color1, color2);

ellipse(circles[count], circles[count+1], 500, 500);
if(abs(circles[count]-width)<=10)
{circles[count+2]=-abs(circles[count+2])}
if(abs(circles[count+1]-height)<=10)
{circles[count+3]=-abs(circles[count+3])}

if(circles[count]<=10)
{
 circles[count+2] = abs(circles[count+2]);
}
if(circles[count+1]<=10)
{
 circles[count+3] = abs(circles[count+3]);
}
circles[count]+=circles[count+2];
circles[count+1]+=circles[count+3];

count+=4;
if(count>circles.length-4)
{count = 0;

}

}

In processingjs, I'm having trouble. The code I wrote works, but only for one circle. If there are two or more circles, they start to flicker (I'm guessing this is from a slow refresh rate from background()). Is there something I'm doing wrong in my code(posted below), or is that just the limit of processingjs's speed?

I'm sure there must be a way to acheive the same effect without the lag. I've seen more done in processing with less lag.

in addition, when two circles overlap they also start flickering (about twice as much). is there any way to fix that?

my code:


    int count;
    int[] circles;
    int numCircles;
    int color, color1, color2;
    void setup()
    {
    size($(document.body).width(),600);
    smooth();
    numCircles = 1;
    color = random(0,200);
    color1 = random(0, 200);
    color2 = random(0, 200);
    strokeWeight( 10 );
frameRate( 60 );

count = 0;
circles = new int[numCircles*4];
for(int x = 0; x<circles.length; x+=4)
{
    circles[x]=random(0,width);
    circles[x+1]=random(0,height);
    if(random(0,1)==0)
    {
     circles[x+2] = 1;
    }
    else
    {
     circles[x+2] = -1;
    }
    if(random(0,1)==0)
    {
     circles[x+3] = 1;
    }
    else
    {
     circles[x+3] = -1;
    }

}


}



void draw()
{


background(255);
fill(255);
stroke(color, color1, color2);

ellipse(circles[count], circles[count+1], 500, 500);
if(abs(circles[count]-width)<=10)
{circles[count+2]=-abs(circles[count+2])}
if(abs(circles[count+1]-height)<=10)
{circles[count+3]=-abs(circles[count+3])}

if(circles[count]<=10)
{
 circles[count+2] = abs(circles[count+2]);
}
if(circles[count+1]<=10)
{
 circles[count+3] = abs(circles[count+3]);
}
circles[count]+=circles[count+2];
circles[count+1]+=circles[count+3];

count+=4;
if(count>circles.length-4)
{count = 0;

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-11-02 23:04:44

我通过 for 循环循环遍历绘图函数中的每个圆,发现我做错了什么,

我发现我能够更新每个圆的位置,而不会在 draw() 函数的迭代之间出现延迟,从而导致闪烁。

I figured out what I was doing wrong

by looping through each circle in the draw function via a for loop I discovered I was able to update the location of each circle without having the delay between iterations of the draw() function which caused the flickering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文