/* Fancy flying lines
 * Copyright © 2009 Göran Weinholt <goran@weinholt.se>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var dirs;
var lines;
var divs;
var screens;
var idx;
var colors;

function drawlines() {
    var s = screens[idx];
    var l;

    // Draw the lines
    for (i = 0; i < lines.length; i++) {
        var l = lines[i];
        s.beginPath();
        s.strokeStyle = l[0];
        s.moveTo(l[1],l[2]);
        s.lineTo(l[3],l[4]);
        s.stroke();
    }

    var l = lines[0];
    if (lines.length < 15) {
        // Add another line
        lines[lines.length] = [colors[lines.length] ,l[1],l[2],l[3],l[4]];
    } else {
        // Shift the lines
        for (i = 1; i < lines.length-1; i++) {
            lines[i] = lines[i+1];
        }
        lines[lines.length-1] = [lines[1][0], l[1],l[2],l[3],l[4]];
    }
    l[1] += dirs[0];
    l[2] += dirs[1];
    l[3] += dirs[2];
    l[4] += dirs[3];
    if (l[1] >= 300 || l[1] <= 5) dirs[0] = -dirs[0];
    if (l[2] >= 145 || l[2] <= 5) dirs[1] = -dirs[1];
    if (l[3] >= 300 || l[3] <= 5) dirs[2] = -dirs[2];
    if (l[4] >= 145 || l[4] <= 5) dirs[3] = -dirs[3];

    // Switch canvases (double-buffering)
    divs[idx].style.zIndex = 1;
    idx = 1 - idx;
    divs[idx].style.zIndex = 0;
    // Software has progressed so far that anti-aliasing is mandatory
    // and <canvas> does not make it possible to clear one of the
    // lines. This idiotic line is from the HTML5 specification and it
    // clears a canvas:
    divs[idx].width = divs[idx].width;
}

function startlines() {
    divs = [document.getElementById("cnv1"), document.getElementById("cnv2")];
    idx = 0;
    screens = [divs[0].getContext("2d"), divs[1].getContext("2d")];
    screens[0].lineWidth = 1;
    screens[1].lineWidth = 1;
    lines = [["green",21,21,50,105]];

    dirs = [4,5,6,7];
    // awk '/[012]/{print "\"rgb("$1","$2","$3")\","}' < /usr/share/gimp/2.0/palettes/Pastels.gpl
    colors = ["rgb(226,145,145)",
              "rgb(153,221,146)",
              "rgb(147,216,185)",
              "rgb(148,196,211)",
              "rgb(148,154,206)",
              "rgb(179,148,204)",
              "rgb(204,150,177)",
              "rgb(204,164,153)",
              "rgb(223,229,146)",
              "rgb(255,165,96)",
              "rgb(107,255,99)",
              "rgb(101,255,204)",
              "rgb(101,196,255)",
              "rgb(101,107,255)",
              "rgb(173,101,255)",
              "rgb(255,101,244)",
              "rgb(255,101,132)",
              "rgb(255,101,101)"];
    drawlines();
    setInterval("drawlines()",  1000/16);
}
