Add global color list
[Hacks.git] / hacks / XSpinnerCGA / XSpinnerCGA.js
1 /* Copyright 2011 The Dominion of Awesome
2  * See COPYING for licensing information */
3 enyo.kind({
4         name: "XSpinnerCGA",
5         kind: "Hack",
6         style: "background-color: black",
7         pack: "center",
8         align: "center",
9         components: [
10                 {style: "border: 2px solid green; padding: 4px", components: [
11                         {name: "terminal", nodeTag: "pre", style: "margin: 0; font-family: PCSenior; font-size: 16px"}
12                 ]}
13         ],
14         create: function() {
15                 this.inherited(arguments);
16                 this.w = 40;
17                 this.h = 24;
18                 this.delay = 20;
19                 this.rate = 360.0 / 1000.0;
20                 this.cw = this.w / 2;
21                 this.ch = this.h / 2;
22         },
23         rendered: function() {
24                 this.terminal = this.$.terminal.hasNode();
25                 this.blinker = document.getElementById('spinner_blinker');
26                 this.blink_active = true;
27                 this.spinner();
28         },
29         start: function() {
30                 if (!this.timer) {
31                         this.timer = setInterval(this.spinner.bind(this), this.delay);
32                         this.blinkerTimer = setInterval(this.blink.bind(this), 500);
33                 }
34         },
35         stop: function() {
36                 clearInterval(this.timer);
37                 clearInterval(this.blinkerTimer);
38                 this.timer = null;
39                 this.blinkerTimer = null;
40         },
41         spinner: function() {
42                 // Get date
43                 var ms = (new Date()).getMilliseconds();
44                 var r = ms * this.rate;
45                 var a = Math.tan((r / 180.0) * Math.PI);
46                 var b = Math.tan(((r+90) / 180.0) * Math.PI);
47
48                 var t = '';
49                 for (var j=0; j < this.h; j++) {
50                         for (var i=0; i < this.w; i++) {
51                                 if ((r > 90 && r <= 180) || (r > 270 && r < 360) || r == 0) {
52                                         if ((j-this.ch) * 2 > a * (i-this.cw) && (j-this.ch) * 2 < b * (i-this.cw) ||
53                                             (j-this.ch) * 2 < a * (i-this.cw) && (j-this.ch) * 2 > b * (i-this.cw) ) {
54                                                 t += '*';
55                                         } else {
56                                                 t += ' ';
57                                         }
58                                 } else {
59                                         if ((j-this.ch) * 2 > a * (i-this.cw) && (j-this.ch) * 2 > b * (i-this.cw) ||
60                                             (j-this.ch) * 2 < a * (i-this.cw) && (j-this.ch) * 2 < b * (i-this.cw) ) {
61                                                 t += '*';
62                                         } else {
63                                                 t += ' ';
64                                         }
65                                 }
66                         }
67                         t += "\n";
68                 }
69                 this.terminal.innerHTML = t;
70         },
71         blink: function() {
72                 if (this.blink_active)
73                         this.blinker.style.color = '#00FF00';
74                 else
75                         this.blinker.style.color = 'black';
76                 this.blink_active = !this.blink_active;
77         }
78 });