Using closure variables

Diff code posted by murphy
created at 27 Nov 02:49

Edit | Back
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
diff --git a/lesson09/index.html b/lesson09/index.html
index b3b15d9..3cfce0a 100644
--- a/lesson09/index.html
+++ b/lesson09/index.html
@@ -351,26 +351,27 @@
 
   function Star(startingDistance, rotationSpeed)
   {
-    this.angle = 0;
-    this.dist = startingDistance;
-    this.rotationSpeed = rotationSpeed;
+    var angle = 0;
+    var dist = startingDistance;
+    var r, g, b;
+    var twinkleR, twinkleG, twinkleB;
 
     this.draw = function(tilt, spin, twinkle)
       {
         mvPushMatrix();
 
         // Move to the star's position
-        mvRotate(this.angle, [0.0, 1.0, 0.0]);
-        mvTranslate([this.dist, 0.0, 0.0]);
+        mvRotate(angle, [0.0, 1.0, 0.0]);
+        mvTranslate([dist, 0.0, 0.0]);
 
         // Rotate back so that the star is facing the viewer
-        mvRotate(-this.angle, [0.0, 1.0, 0.0]);
+        mvRotate(-angle, [0.0, 1.0, 0.0]);
         mvRotate(-tilt, [1.0, 0.0, 0.0]);
 
         if (twinkle)
         {
           // Draw a non-rotating star in the alternate "twinkling" color
-          gl.uniform3f(gl.getUniformLocation(shaderProgram, "uColor"), this.twinkleR, this.twinkleG, this.twinkleB);
+          gl.uniform3f(gl.getUniformLocation(shaderProgram, "uColor"), twinkleR, twinkleG, twinkleB);
           drawStar();
         }
 
@@ -378,47 +379,47 @@
         mvRotate(spin, [0.0, 0.0, 1.0]);
 
         // Draw the star in its main color
-        gl.uniform3f(gl.getUniformLocation(shaderProgram, "uColor"), this.r, this.g, this.b);
+        gl.uniform3f(gl.getUniformLocation(shaderProgram, "uColor"), r, g, b);
         drawStar()
 
         mvPopMatrix();
       };
 
+    var randomiseColors = function()
+      {
+        // Give the star a random color for normal
+        // circumstances...
+        r = Math.random();
+        g = Math.random();
+        b = Math.random();
+
+        // When the star is twinkling, we draw it twice, once
+        // in the color below (not spinning) and then once in the
+        // main color defined above.
+        twinkleR = Math.random();
+        twinkleG = Math.random();
+        twinkleB = Math.random();
+      };
+
     this.animate = function(elapsedTime)
       {
         var effectiveFPMS = 60 / 1000;
-        this.angle += this.rotationSpeed * effectiveFPMS * elapsedTime;
+        angle += rotationSpeed * effectiveFPMS * elapsedTime;
 
         // Decrease the distance, resetting the star to the outside of
         // the spiral if it's at the center.
-        this.dist -= 0.01 * effectiveFPMS * elapsedTime;
-        if (this.dist < 0.0)
+        dist -= 0.01 * effectiveFPMS * elapsedTime;
+        if (dist < 0.0)
         {
-          this.dist += 5.0;
-          this.randomiseColors();
+          dist += 5.0;
+          randomiseColors();
         }
 
       };
 
 
-    this.randomiseColors = function()
-      {
-        // Give the star a random color for normal
-        // circumstances...
-        this.r = Math.random();
-        this.g = Math.random();
-        this.b = Math.random();
-
-        // When the star is twinkling, we draw it twice, once
-        // in the color below (not spinning) and then once in the
-        // main color defined above.
-        this.twinkleR = Math.random();
-        this.twinkleG = Math.random();
-        this.twinkleB = Math.random();
-      };
-
     // Set the colors to a starting value.
-    this.randomiseColors();
+    randomiseColors();
 
   }
 
3.46 KB in 12 ms with coderay