Using noLoop(), redraw() to lighten CPU load
in
Programming Questions
•
2 years ago
All,
I'm working on a simple interactive chart that allows a user to rollover a datapoint to see the data detail. A simplified example of my code is below, which contains all the structural elements (big thanks to phi.lo for showing me how to do a fade-in with this!).
The end goal is to put this on the web with processing.js. I've been able to do this, but noticed that whether I'm running in native Processing or in processing.js the sketch bumps my cpu usage up to about 25%. I'd like to ameliorate this if possible. It seems like I should be able to do noLoop() at the end of setup, and then have some sort of function for the mouseovers that will do redraw() for as long as the mouseover condition is true But I can't figure out how to work this in the context of my code. I've tried putting the mouseover code in its own function and calling it outside of draw, but that hasn't worked.
Any thoughts? Many thanks!
Chris
- testChart chart1, chart2;
- float[] datapoint = {
- 1.0, 3.5, -2.0, -1.5, 6
- };
- float[] datapoint2 = {
- 8.0, 3.5, 2.0, 1.5, 2
- };
- int alph = 255; //alpha value
- boolean over; //test for mouseover
- void setup() {
- size(600, 400);
- smooth();
- chart1 = new testChart(20, 200, 8, datapoint);
- chart2 = new testChart(20, 300, 8, datapoint2);
- }
- void draw() {
- background(255);
- over = false;
- chart1.drawchart();
- chart2.drawchart();
- if (over) {
- if (alph > 10) {
- alph -= 10;
- }
- }
- else {
- alph = 255;
- }
- }
- class testChart {
- int offsetX, offsetY, ellipseSize;
- float[] data;
- testChart(int offsetX_, int offsetY_, int ellipseSize_, float[] data_) {
- offsetX = offsetX_; //x-position
- offsetY = offsetY_; //y-position
- ellipseSize = ellipseSize_; //circle size
- data = data_; // data array
- }
- void drawchart() {
- int plotX1 = offsetX;
- int newY = offsetY;
- stroke(5, 55, 105);
- fill(164, 199, 242);
- //draw circles based on data
- for (int i = 0; i < data.length; i++) {
- float x = plotX1;
- float y = newY -(data[i]);
- ellipse (x, y, ellipseSize, ellipseSize);
- plotX1 += 25;
- }
- plotX1 = offsetX;
- //create pop-up windows on circle mouseover
- for (int i = 0; i < data.length; i++) {
- float x = plotX1;
- float y = newY -(data[i]);
- float disX = x - mouseX;
- float disY = y - mouseY;
- if (sqrt(sq(disX) + sq(disY)) < ellipseSize/2 ) {
- over = true;
- fill(5, 55, 105, alph); //this is the alpha that needs to fade in!
- rect(mouseX, mouseY-10, 50, 30);
- }
- plotX1 += 25;
- }
- }
- }
1