patch 8

As I wade through the tutorials, I stumble upon objects that appear quite handy, like the js object. The js object could have vastly simplified my development of patch 7. How so? Well, like this:

js

Ahh…so much cleaner than the mess I had before. Modifying the Java code to Javascript was trivial, here’s the code for sketch0.js:

outlets = 0;

// global vars
var offset = 20.0;
var radius = 50.0;
var centerX = 50.0;
var centerY = 50.0;
var x = 0.0;
var y = 0.0;
var angle = 0.0;
var frequency = 2.0;
var counter = 0;
var coordsX = new Array(180);
var coordsY = new Array(180);

function bang()
{
    draw();
}

function radians(angle)
{
    return angle * Math.PI / 180;
}

function draw()
{
    outlet(0, "clear");

    // calculate point
    x = centerX + Math.cos(radians(angle)) * radius;
    y = centerY + Math.sin(radians(angle)) * radius;

    // draw black circle
    outlet(0, "paintoval", offset, offset, radius*2+offset, radius*2+offset, 0, 0, 0);

    // draw red circle (10 pixels in diameter)
    outlet(0, "paintoval", x+offset-5, y+offset-5, x+offset+5, y+offset+5, 255, 0, 0);

    // draw green circle
    outlet(0,
              "paintoval",
              centerX+radius+counter+offset-5,
              y+offset-5,
              centerX+radius+counter+offset+5,
              y+offset+5,
              0, 255, 0);

    // draw prev point
    for(var i = 0; i < counter; ++i)
        outlet(0, "setpixel", coordsX[i], coordsY[i], 0, 0, 0);

    // cache prev point
    coordsX[counter] = centerX + radius + counter + offset;
    coordsY[counter] = y + offset;

    // adjust counter and angle
    ++counter;
    angle -= frequency;
    if(angle <= -360)
    {
        angle = 0;
        counter = 0;
    }
}



Another possiblity is to swap the lcd object out in favour of jsui, but let’s not get carried away.

Leave a Reply