File: cynosure.js

package info (click to toggle)
freej 0.10git20100110-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 32,080 kB
  • ctags: 22,705
  • sloc: cpp: 156,254; ansic: 25,531; sh: 13,538; perl: 4,624; makefile: 3,278; python: 2,889; objc: 1,284; asm: 1,125; ruby: 126
file content (267 lines) | stat: -rw-r--r-- 8,160 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*  FreeJ example scripts
 *  (c) Copyright 2005 Christoph Rudorff aka MrGoil <goil@dyne.org>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * This source code 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.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
Javascript Version of the xscreensaver Cynosure
*/

curColor    = 0;
curBase     = curColor;
shadowWidth = 2 ; //get_integer_resource ("shadowWidth", "Integer");
elevation   = 10; //get_integer_resource ("elevation", "Integer");
sway        = 30; //get_integer_resource ("sway", "Integer");
tweak       = 20; //get_integer_resource ("tweak", "Integer");
gridSize    = 10; //get_integer_resource ("gridSize", "Integer");
timeLeft    = 0;
ncolors     = 8; //get_integer_resource ("colors", "Colors");
iterations  = 100; //get_integer_resource ("iterations", "Iterations");
MINCELLSIZE = 64;
MINRECTSIZE = 24;
THRESHOLD = 100;

i = 0;

colors = new Array(0xa03000ff, 0x006000ff, 0x336000ff, 0x00ff00ff, 0xc20000ff, 0x0000c0ff, 0xc0c0c0ff, 0x30f0f0ff, 0x00ff90ff);

width=get_width(); height=get_height();

scr = new Screen("sdl");
scr.init(width, height);
add_screen(scr);

cyno_geo = new GeometryLayer(width, height);
//cyno_geo.activate(true);
scr.add_layer(cyno_geo);
cyno_geo.start();

cyno_trigger = new TriggerController();
register_controller(cyno_trigger);
cyno_trigger.frame = function() { Draw(); };

cyno_kbd = new KeyboardController();
register_controller(cyno_kbd);
cyno_kbd.released_q = function() { quit(); }
cyno_kbd.released_x = function() { Draw(); }

cyno_kbd.released_p = function() {
    echo("ok, I shot myself now");
    try{rem_controller(cyno_trigger);}catch(e){echo("tg nope: "+e);}
    try{rem_layer(cyno_geo);}catch(e){echo("la nope: "+e);}
    try{rem_controller(mc);}catch(e){echo("mc nope: "+e);}
    try{rem_controller(cyno_kbd);}catch(e){echo("kb nope: "+e);}
    delete cyno_geo;
    delete mc;
    delete cyno_kbd;
    delete cyno_trigger;
    gc(); // kbd-js not cleared within this call!
    echo("Bullet arrived.");
}


try { 
    mc = new MidiController(); // ('descr')
    register_controller(mc);
    ret = mc.connect_from(0, 20, 0);

    mc.event_ctrl = function (ch, func, value) {
        switch (ch) {
            case 0:
                switch (func) {
                    case 2:
                        shadowWidth = value;
                        break;
                    case 3:
                        elevation = value;
                        break;
                    case 4:
                        sway = value;
                        break;
                }
                break;
            case 1:
                switch (func) {
                    case 2:
                        tweak = value+1;
                        break;
                    case 3:
                        gridSize = value/4 +1;
                        break;
                    case 4:
                        iterations = value*2;
                }
                break;
            case 2:
                switch (func) {
                    case 2:
                        MINRECTSIZE = value;
                        break;
                    case 3:
                        MINCELLSIZE = value;
                        break;
                    case 4:
                        THRESHOLD = value;
                        break;
                }
        }
    }
} catch(e) {
    echo("No MIDI support ");
}

function integer(i) {
	if (i>0) 
		return Math.floor(i)
	else
		return Math.ceil(i)
}

function Draw() {
    for (j=0; j<10; j++) {
		if ((iterations > 0) && (++i >= iterations)) {
			i = 0; // clearScreen
			cyno_geo.rectangle_fill(0,0,width,height,0x000000FF);
		} 
        paint();
    }
}

function paint() {
    var cellsWide, cellsHigh, cellWidth, cellHeight;
//debug ("paint");
    /* How many cells wide the grid is (equal to gridSize +/- (gridSize / 2))
     */
    cellsWide  = c_tweak(gridSize, gridSize / 2);
    /* How many cells high the grid is (equal to gridSize +/- (gridSize / 2))
     */
    cellsHigh  = c_tweak(gridSize, gridSize / 2);
    /* How wide each cell in the grid is */
    cellWidth  = width  / cellsWide;
    /* How tall each cell in the grid is */
    cellHeight = height / cellsHigh;

    /* Ensure that each cell is above a certain minimum size */

    if (cellWidth < MINCELLSIZE) {
      cellWidth  = MINCELLSIZE;
      cellsWide  = width / cellWidth;
    }

    if (cellHeight < MINCELLSIZE) {
      cellHeight = MINCELLSIZE;
      cellsHigh  = width / cellWidth;
    }
    /* fill the grid with randomly-generated cells */
    for(var i = 0; i < cellsHigh; i++) {
      /* Each row is a different color, randomly generated (but constrained) */
	var c = genNewColor();
	var fg_gc = colors[c];
//debug("pick color #" + c + " = " + fg_gc);
	  //XSetForeground(dpy, fg_gc, colors[c].pixel);

	for(var j = 0; j < cellsWide; j++) {
		var curWidth, curHeight, curX, curY;

		/* Generate a random height for a rectangle and make sure that */
		/* it's above a certain minimum size */
		curHeight = random() % (cellHeight - shadowWidth);
		if (curHeight < MINRECTSIZE)
		  curHeight = MINRECTSIZE;
		/* Generate a random width for a rectangle and make sure that
		   it's above a certain minimum size */
		curWidth  = random() % (cellWidth  - shadowWidth);
		if (curWidth < MINRECTSIZE)
		  curWidth = MINRECTSIZE;
		/* Figure out a random place to locate the rectangle within the
		   cell */
		curY = (i * cellHeight) + (random() % ((cellHeight - curHeight) -
							    shadowWidth));
		curX = (j * cellWidth) +  (random() % ((cellWidth  - curWidth) -
							    shadowWidth));

		/* Draw the shadow */
		if (elevation > 0)
		  cyno_geo.rectangle_fill(curX + elevation, curY + elevation,
			  curX + elevation + curWidth, 
			  curY + elevation + curHeight,
			  0x00000060);

        /* Draw the edge */
		if (shadowWidth > 0)
		  cyno_geo.rectangle_fill(curX + shadowWidth, curY + shadowWidth,
		  curX + shadowWidth + curWidth, 
		  curY + shadowWidth + curHeight,
		  0x000000ff);

		cyno_geo.rectangle_fill(curX, curY, curX+curWidth, curY+curHeight, fg_gc)

		/* Draw a 1-pixel black border around the rectangle */
		cyno_geo.rectangle(curX, curY, curX+curWidth, curY+curHeight, 0x00000000);
    }
  }
}

function random() {
	return integer(Math.random() * 0xffffffff);
}


function genConstrainedColor(base, tweak) {
    var i = integer(1 + (random() % tweak));
    if (random() & 1)
      i = -i;
    i = (base + i) % ncolors;
    while (i < 0)
      i += ncolors;
    return integer(i);
}

function c_tweak(base, tweak) {
    var ranTweak = integer(random() % (2 * tweak));
    var n = (base + (ranTweak - tweak));
    if (n < 0) n = -n;
    return integer(n < 255 ? n : 255);
}

function genNewColor() {
    /* These lines handle "sway", or the gradual random changing of */
    /* colors. After genNewColor() has been called a given number of */
    /* times (specified by a random permutation of the tweak variable), */
    /* take whatever color has been most recently randomly generated and */
    /* make it the new base color. */
    if (timeLeft == 0) {
      timeLeft = c_tweak(sway, sway / 3);
      curColor = curBase;
    } else {
      timeLeft--;
    }
     
    /* If a randomly generated number is less than the threshold value,
       produce a "sport" color value that is completely unrelated to the 
       current palette. */
    if (0 == (random() % THRESHOLD)) {
      return integer(random() % ncolors);
    } else {
      curBase = genConstrainedColor(curColor, tweak);
      return integer(curBase);
    }

}


echo("cyno script end");