File: fireworks.js

package info (click to toggle)
qlcplus 4.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,644 kB
  • sloc: cpp: 182,867; javascript: 7,764; xml: 2,453; ansic: 2,120; sh: 1,716; python: 634; ruby: 606; makefile: 23
file content (379 lines) | stat: -rw-r--r-- 12,246 bytes parent folder | download | duplicates (3)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
  Q Light Controller Plus
  fireworks.js

  Copyright (c) Hans-Jürgen Tappe

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

// Development tool access
var testAlgo;

(
  function()
  {
    var util = new Object;

    var algo = new Object;
    algo.apiVersion = 2;
    algo.name = "Fireworks";
    algo.author = "Hans-Jürgen Tappe";
    algo.acceptColors = 1;
    algo.properties = new Array();
    algo.initialized = false;

    algo.triggerPoints = new Array(
      ["Upper Half", {minFactor: 0.5, maxFactor: 1}],
      ["Upper Third", {minFactor: 0.7, maxFactor: 1}],
      ["Bottom Half", {minFactor: 0, maxFactor: 0.5}],
      ["Bottom Third", {minFactor: 0, maxFactor: 0.3}],
      ["Centered Third", {minFactor: 0.3, maxFactor: 0.7}],
      ["Full Size", {minFactor: 0, maxFactor: 1}]
    );
    algo.makeSubArray = function(_index) {
      var _array = new Array();
      for (var i = 0; i < algo.triggerPoints.length; i++) {
        _array.push(algo.triggerPoints[parseInt(i)][parseInt(_index)]);
      }
      return _array;
    };
    algo.triggerNames = algo.makeSubArray(0);
    algo.getNameIndex = function(_name) {
      var idx = algo.triggerNames.indexOf(_name);
      if (idx === -1) {
        idx = (algo.triggerPoints.length - 1);
      }
      return idx;
    };

    algo.rocketsCount = 3;
    algo.properties.push("name:rocketCount|type:range|display:Count|values:1,50|write:setCount|read:getCount");
    algo.randomColor = 1;
    algo.properties.push("name:randomColor|type:list|display:Random Color|values:No,Yes|write:setRandom|read:getRandom");
    algo.triggerPoint = "Upper Half";
    algo.properties.push("name:triggerPoint|type:list|display:Trigger Point|"
        + "values:" + algo.triggerNames.toString() + "|"
        + "write:setTrigger|read:getTrigger");
    algo.particleCount = 11;
    algo.properties.push("name:particleCount|type:range|display:Particles|values:5,30|write:setParticleCount|read:getParticleCount");
    algo.particleSteps = 15;
    algo.properties.push("name:particleSteps|type:range|display:Size|values:5,30|write:setParticleSteps|read:getParticleSteps");

    algo.setCount = function(_count)
    {
      algo.rocketsCount = _count;
      algo.initialized = false;
    };

    algo.getCount = function()
    {
      return algo.rocketsCount;
    };

    algo.setRandom = function(_random)
    {
      if (_random === "Yes") {
        algo.randomColor = 0;
      } else if (_random === "No") {
        algo.randomColor = 1;
      }
    };

    algo.getRandom = function()
    {
      if (algo.randomColor === 0) {
        return "Yes";
      } else if (algo.randomColor === 1) {
        return "No";
      }
    };

    algo.setTrigger = function(_trigger)
    {
      algo.triggerPoint = _trigger;
    };

    algo.getTrigger = function()
    {
      return algo.triggerPoint;
    };

    algo.setParticleCount = function(_count)
    {
      return algo.particleCount = _count;
    };

    algo.getParticleCount = function()
    {
      return algo.particleCount;
    };

    algo.setParticleSteps = function(_steps)
    {
      return algo.particleSteps = _steps;
    };

    algo.getParticleSteps = function()
    {
      return algo.particleSteps;
    };


    algo.rgbMap = function(width, height, rgb, progstep)
    {
      if (algo.initialized === false ||
          util.map.length != height ||
          util.map[0].length != width) {
        util.initialize(width, height);
      }

      // Dim current map data
      for (var y = 0; y < height; y++) {
        for (var x = 0; x < width; x ++) {
          util.map[y][x] = util.dimColor(util.map[y][x], 0.8);
        }
      }

      // Initialize each rocket displayed
      for (var i = 0; i < algo.rocketsCount; i++) {
        // create a new position for the rocket if the rocket has reached map borders
        if (algo.rockets[i].particleSteps <= 0) {
          algo.rockets[i].initialized = false;
        }

        // Initialize the rocket
        if (!algo.rockets[i].initialized ||
            algo.rockets[i].particle.length < algo.particleCount) {
          util.initializeRocket(i, width, height, rgb);
        }

        // Trigger the rocket
        if (! algo.rockets[i].triggered &&
            Math.floor(algo.rockets[i].y) <= Math.floor(algo.rockets[i].triggerPoint)) {
          // switch to particles mode
          algo.rockets[i].triggered = true;
          // initialize the particles
          for (var j = 0; j < algo.particleCount; j++) {
            // particle start location
            algo.rockets[i].particle[j].x = algo.rockets[i].x;
            algo.rockets[i].particle[j].y = algo.rockets[i].y;
            // particle directions
            var percent = j / algo.particleCount;
            var angle = 2 * Math.PI * percent;
            algo.rockets[i].particle[j].xDirection = Math.cos(angle);
            algo.rockets[i].particle[j].yDirection = Math.sin(angle);
          }
        }

        if (! algo.rockets[i].triggered) {
          // Draw the rocket
          util.drawObject(algo.rockets[i].x, algo.rockets[i].y,
            width, height,
            algo.rockets[i].r, algo.rockets[i].g, algo.rockets[i].b);
        } else {
          // Countdown particle steps
          algo.rockets[i].particleSteps--;
          for (var j = 0; j < algo.particleCount; j++) {
            // Progress the particle
            algo.rockets[i].particle[j].x +=
              algo.rockets[i].particle[j].xDirection;
            algo.rockets[i].particle[j].y -=
              algo.rockets[i].particle[j].yDirection - util.gravity;
            // draw the particles
            util.drawObject(algo.rockets[i].particle[j].x,
              algo.rockets[i].particle[j].y,
              width, height,
              algo.rockets[i].r, algo.rockets[i].g, algo.rockets[i].b);
          }
        }

        // Accelerate the rocket
        algo.rockets[i].yDirection *= util.acceleration;
        // set rocket's next center location
        algo.rockets[i].x += algo.rockets[i].xDirection;
        algo.rockets[i].y -= algo.rockets[i].yDirection - util.gravity;
        // Carry away the rocket centers
        algo.rockets[i].xDirection = algo.rockets[i].xDirection *
          (1 + algo.rockets[i].yDirection / 30);
      }
      return util.map;
    };

    algo.rgbMapStepCount = function(width, height)
    {
      return 1024; // This make no difference to the script
    };

    // --------------------------------
    // Helper Utilities & Functions

    util.gravity = 0.4;
    util.acceleration = 1.05;

    // random position function for new rocket
    util.getNewNumberRange = function(minVal, maxVal) {
      // Search in the range of min to max + 1
      // which will be reduced by random() excluding 1
      // and floor() reducing to lower number.
      return Math.floor(Math.random() * (maxVal + 1 - minVal)) + minVal;
    }

    // Combine RGB color from color channels
    util.mergeRgb = function(r, g, b) {
      r = Math.min(255, Math.round(r));
      g = Math.min(255, Math.round(g));
      b = Math.min(255, Math.round(b));
      return ((r << 16) + (g << 8) + b);
    }

    util.getColor = function(r, g, b, mRgb) {
      // split rgb in to components
      var pointr = (mRgb >> 16) & 0x00FF;
      var pointg = (mRgb >> 8) & 0x00FF;
      var pointb = mRgb & 0x00FF;
      // add the color to the mapped location
      pointr += r;
      pointg += g;
      pointb += b;
      // set mapped point
      return util.mergeRgb(pointr, pointg, pointb);
    }

    util.dimColor = function(mRgb, factor) {
      if (mRgb < 1) {
        return 0;
      }
      // split rgb into components
      var pointr = (mRgb >> 16) & 0x00FF;
      var pointg = (mRgb >> 8) & 0x00FF;
      var pointb = mRgb & 0x00FF;
      // add the color to the mapped location
      pointr *= factor;
      pointg *= factor;
      pointb *= factor;
      // set mapped point
      return util.mergeRgb(pointr, pointg, pointb);
    }

    util.drawObject = function(x, y, w, h, r, g, b) {
      // workout closest map location for rocket
      var mx = Math.floor(x);
      var my = Math.floor(y);
      for (var ry = my; ry < my + 2; ry++) {
        for (var rx = mx; rx < mx + 2; rx++) {
          // Draw only if edges are on the map
          if (rx < w && rx > -1 && ry < h && ry > -1) {
            // Draw the box for debugging.
            //util.map[ry][rx] = util.getColor(45, 45, 45, 0);
            var offx = rx - x;
            var offy = ry - y;
            var hyp = Math.max(0, 1 - Math.abs(Math.sqrt( (offx * offx) + (offy * offy))));
            var pointr = Math.round(r * hyp);
            var pointg = Math.round(g * hyp);
            var pointb = Math.round(b * hyp);
            util.map[ry][rx] = util.getColor(pointr, pointg, pointb, util.map[ry][rx]);
          }
        }
      }
    }

    util.initializeRocket = function(i, w, h, rgb) {
      // reset height and set a start x location
      algo.rockets[i].x = util.getNewNumberRange(Math.round(w / 5), Math.round(4 * w / 5));
      algo.rockets[i].y = h;
      // determine the x and y speed
      algo.rockets[i].yDirection = util.getNewNumberRange(3, 5) / 3;
      algo.rockets[i].xDirection =
        (algo.rockets[i].x - (w / 2)) / w *
        util.getNewNumberRange(-1, 1);
      // Set the rocket to raise mode, untriggered.
      algo.rockets[i].triggered = false;
      // set particle steps
      algo.rockets[i].particleSteps = algo.particleSteps;
      // Initialize the rocket trigger point
      algo.rockets[i].triggerPoint = util.getRocketTriggerPoint(h);
      // initialize the rocket color
      if (algo.randomColor === 0) {
        do {
          // Chose random colour for each rocket
          algo.rockets[i].r = Math.round(Math.random() * 255);
          algo.rockets[i].g = Math.round(Math.random() * 255);
          algo.rockets[i].b = Math.round(Math.random() * 255);
          // try again if it is too dim
        } while ((algo.rockets[i].r + algo.rockets[i].g + algo.rockets[i].b) < 125);
      } else {
        algo.rockets[i].r = (rgb >> 16) & 0x00FF;
        algo.rockets[i].g = (rgb >> 8) & 0x00FF;
        algo.rockets[i].b = rgb & 0x00FF;
      }
      // initialize particles
      algo.rockets[i].particle = new Array();
      for (var j = 0; j < algo.particleCount; j++) {
        algo.rockets[i].particle[j] = new Object();
      }

      // Set rocket status to initialized
      algo.rockets[i].initialized = true;
    }

    util.getRocketTriggerPoint = function(h)
    {
      var nameIndex = algo.getNameIndex(algo.triggerPoint);
      var minMaxFactor = algo.triggerPoints[nameIndex][1];
      var min = Math.floor(minMaxFactor.minFactor * h);
      var max = Math.floor(minMaxFactor.maxFactor * h);
      return h - util.getNewNumberRange(min, max);
    }

    util.initialize = function(width, height)
    {
      algo.rockets = new Array(algo.rocketsCount);
      for (var i = 0; i < algo.rocketsCount; i++) {
        algo.rockets[i] = new Object();
        algo.rockets[i].initialized = false;
      }

      // Clear map data
      util.map = new Array(height);
      for (var y = 0; y < height; y++) {
        util.map[y] = new Array();
        for (var x = 0; x < width; x++) {
          util.map[y][x] = 0;
        }
      }

      for (var i = 0; i < algo.rocketsCount; i++) {
        algo.rockets[i] = {
          x: 0,
          y: height,
          xDirection: 0,
          yDirection: height,
          triggerPoint: 0,
          triggered: false,
          r: 0,
          g: 0,
          b: 0,
        };
      }

      algo.initialized = true;
      return;
    };

    // Development tool access
    testAlgo = algo;

    return algo;
  }
)();