0

I wrote some code to create a dynamic paintbrush, that changes colour, in Processing environment. The same code produces very different results when running in Java, Javascript and embedded javascript.

As far as i can tell there is no difference in handling of global variables in the different modes. also i am defining the color mode so even if there is differences, my definition should replace these. Any ideas?

I have a global float variable that i increment to cycle through the colours. (colours in HSB mode) I also draw a black rectanlge on the whole screen with very low alpha value in order to slowly fade out the drawings.

float cH = 0;
float cS = 0;
float cB = 0;
int counter = 0;
// other variables.... 

setup()
{
    size(800,600);
    colorMode(HSB, 1.0, 1.0, 1.0);
    cH = 0.0;
    cS = 0.74;
    cB = 0.6;
    counter = 0;
    background(0);
    // .. other setup code
}

// ... other code

mouseDragged()
{   
   counter ++;
   if(counter %5 == 0)  {    
        noStroke();
        fill(0,0.01);
        rect(0,0, width, height);
    }

   cH += 1/500;

    if(cH >= 1.0)
       cH = 0.0;
    stroke(color( cH, cS, cB));
    fill(color( cH, cS, cB));

    //... then the code for drawing the shapes
}

This works beautifully when running it in processing, in javascript mode. See picture 1 below, working with colour change and fade out java When running this same sketch, embedded in my browser using javascript, the colour change works, but rectangles drawn on top turn grey instead of black. With the same colour values! embedded javascript you can try it out here

# When running it in java mode, the fade out to black works, but the color change doesnt. the brush stays its initial color, red. enter image description here

The entire code, processing.pde file can be found here.

On my appengine I load the sketch from a html file as follows: With previous problems I made sure i was using the newest version of processing.js

<html>
<head>
    <title>{{name}} Sketch</title>
    <script src="/statics/processing.js" type="text/javascript"></script>   
    <script src="/statics/javascript.js" type="text/javascript"></script>   

</head>

  <body>
      <label>Title</label>
      <br>
      <div class="proc">
      <canvas id="SKETCH" data-processing-sources="/sketches/{{name}}/{{name}}.pde" >   
  </canvas>
    <br>
    this is below the canvas
  </body>   
</html>

1 Answer 1

0

It may be because of integer division at line cH += 1/500; JS has no such a thing...

from: http://processingjs.org/articles/p5QuickStart.html

Division which is expected to produce an integer might need explicit casting There are a class of bugs that arise when converting Processing code to Processing.js that involve integer vs. floating point division. What was straight-up integer division in Processing code, when converted to Processing.js, can sometimes become problematic, as numbers become doubles, and introduce a fractional part. The fix is to explicitly cast any division to an integer that exhibits this behaviour:

 // before
 int g = mouseX / i;

 // after
 int g = (int)(mouseX / i);
2
  • Interesting thanks, and i'll keep it in mind for future programs, but in this case cH is a float, and changing it to ""cH = (float)( cH + 1.0/500 );"" made no difference! Commented Sep 30, 2014 at 10:18
  • ops, sorry I missed ch was a float. Commented Sep 30, 2014 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.