Title / Description
Code 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(); }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code