|
7 | 7 | var html = document.documentElement; |
8 | 8 |
|
9 | 9 | function Snowflake(maxX) { |
| 10 | + this.reset(maxX); |
| 11 | + } |
| 12 | + Snowflake.prototype.tick = function() { |
| 13 | + var sidePhase = this.sidePhase += this.sideVel; |
| 14 | + this.y += this.vel; |
| 15 | + this.x = this.midX + Math.sin(sidePhase) * this.sideAmp; |
| 16 | + }; |
| 17 | + Snowflake.prototype.reset = function(maxX) { |
10 | 18 | var rand = Math.random(); |
11 | 19 | var sizeRand; |
12 | 20 | var chanceOfLargeSnowflake = 0.15; |
|
30 | 38 | this.sidePhase = 0; |
31 | 39 | this.sideAmp = sizeRand * 40; |
32 | 40 | this.sideVel = Math.random() * 0.05; |
33 | | - } |
34 | | - Snowflake.prototype.tick = function() { |
35 | | - var sidePhase = this.sidePhase += this.sideVel; |
36 | | - this.y += this.vel; |
37 | | - this.x = this.midX + Math.sin(sidePhase) * this.sideAmp; |
| 41 | + |
| 42 | + this.x = 0; |
| 43 | + |
| 44 | + return this; |
38 | 45 | }; |
39 | 46 |
|
40 | 47 | (function() { |
|
50 | 57 | var PIx2 = Math.PI*2; |
51 | 58 | var assumedFps = 60; |
52 | 59 | var settlePoint; |
| 60 | + var snowflakePool = []; |
53 | 61 |
|
54 | 62 | function resizeCanvas() { |
55 | 63 | settlePoint = Array(html.clientWidth); |
|
94 | 102 |
|
95 | 103 | // add new flake? |
96 | 104 | while ( flakesThisFrame-- ) { |
97 | | - activeFlakes.push( new Snowflake(canvas.width) ); |
| 105 | + if (snowflakePool.length) { |
| 106 | + activeFlakes.push( snowflakePool.pop().reset(canvas.width) ); |
| 107 | + } |
| 108 | + else { |
| 109 | + activeFlakes.push( new Snowflake(canvas.width) ); |
| 110 | + } |
98 | 111 | } |
99 | 112 |
|
100 | 113 | var i = activeFlakes.length; |
|
107 | 120 |
|
108 | 121 | // splice flake if it's now out of rendering zone |
109 | 122 | if (flake.y >= canvas.height || flake.y >= settlePoint[Math.floor(flake.x)]) { |
110 | | - activeFlakes.splice(i, 1); |
| 123 | + snowflakePool.push.apply(snowflakePool, activeFlakes.splice(i, 1)); |
111 | 124 | // this flake effects our settle points |
112 | 125 | if (flake.alpha > 0.8) { |
113 | 126 | updateSettlePoints(flake); |
|
0 commit comments