使用处理在屏幕上烘焙阵列 [英] Baking the arrays on screen with Processing

查看:65
本文介绍了使用处理在屏幕上烘焙阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用处理数组函数,但是当我使用此函数绘制事物时,我意识到它会将每个绘制的形状存储在内存中,这会导致CPU峰值.(特别是到几千时)

I like working with Processing array functions but when I draw things using this function, I realize it's storing every single drawn shape in the memory, which is causing spikes in CPU. (Especially when it goes up to a few thousands)

如何烘焙图形,然后在mouserelease上删除数组对象?我的意思是,Processing是否可以在每个笔触之后将这些对象表现为单个图像,然后使用 .remove(0)函数清除数组?

How to bake the drawing then remove the array objects on mouserelease? I mean, can Processing behave those objects as a single image after every stroke then I'd clear the array using .remove(0) function?

这是我的代码:

ArrayList <Drawing> drawings = new ArrayList <Drawing>();

void setup() {
  size(400, 400);
  background(255);
  colorMode(HSB);
}

void draw() {
  background(255);
  for (int i=0;i<drawings.size();i++) {
    drawings.get(i).display();


  }

  println(drawings.size());
}

void mouseDragged() {


  drawings.add(new Drawing(mouseX, mouseY));

}



class Drawing {
  float x, y, r;
  color c;

  Drawing(float ax, float ay) {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255, 88);
  }

  void display() {
    noStroke();
    fill(c, 100);
    ellipse(x,y, r, r);
  }
}

推荐答案

如果只想在按下鼠标(绘制新对象)的同时将对象存储到ArrayList中,而只保留所有旧对象为静态可以从ArrayList中移除背景,如下所示:

If you want to store the objects into the ArrayList only while the mouse is pressed (drawing new objects) and just have all the old objects to be static on the background out of the ArrayList you can do something like this:

ArrayList<Drawing> drawings = new ArrayList();
boolean flag = false;

void setup()
{
  size(400, 400);
  background(255);
  colorMode(HSB);
  loadPixels();
}

void draw()
{
  updatePixels();
  if(flag)
  {
    for(Drawing drawing : drawings)
    {
      drawing.display();
    }
  }
  println(drawings.size());
}

void mouseDragged()
{
  flag = true;
  Drawing drawing = new Drawing(mouseX, mouseY);
  drawings.add(drawing);
}

void mouseReleased()
{
  flag = false;
  loadPixels();
  drawings = new ArrayList();
}

class Drawing
{
  float x, y, r;
  color c;

  Drawing(float ax, float ay)
  {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255, 88);
  }

  public void display()
  {
    noStroke();
    fill(c, 100);
    ellipse(x,y, r, r);
  }
}

函数 loadPixels()将屏幕上的所有像素存储到 pixels [] 数组中,而 updatePixels()绘制像素在画布上的 pixels [] 中.这样,每次释放鼠标时,您就可以清空ArrayList,即使ArrayList包含数千个元素时,您仍然会得到一些CPU峰值,当鼠标不被拖动或ArrayList少于数千个时元素可以减少CPU消耗.

The function loadPixels() stores into the pixels[] array all the pixels of the screen, while updatePixels() draws the pixels in pixels[] on the canvas. This way you can just empty your ArrayList every time the mouse is released and, even though when the ArrayList gets some thousands of elements you still get some CPU spikes, when the mouse is not being dragged or the ArrayList has less than a couple thousands of elements it is less CPU consuming.

这篇关于使用处理在屏幕上烘焙阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆