OLD | NEW |
(Empty) | |
| 1 var IS_SKV8 = typeof document == "undefined"; |
| 2 |
| 3 function circlePath(r) { |
| 4 if (IS_SKV8) { |
| 5 var p = new Path(); |
| 6 p.oval(0, 0, r, r); |
| 7 p.closePath(); |
| 8 return p; |
| 9 } else { |
| 10 return null; |
| 11 } |
| 12 } |
| 13 |
| 14 var onDraw = function() { |
| 15 var W = 500; |
| 16 var H = 500; |
| 17 var NumParticles = 100; |
| 18 |
| 19 var angle = 0; |
| 20 var ticks = 0; |
| 21 var particles =[]; |
| 22 |
| 23 for (var i = 0; i < NumParticles; i++) { |
| 24 particles[i] = { |
| 25 x: Math.floor(Math.random()*W), |
| 26 y: Math.floor(Math.random()*H), |
| 27 r: Math.floor(Math.random()*7+1), |
| 28 path: circlePath(Math.random()*7+1), |
| 29 } |
| 30 } |
| 31 |
| 32 function draw(ctx) { |
| 33 ctx.fillStyle = "#ADD8E6"; |
| 34 ctx.fillRect(0, 0, W-1, H-1); |
| 35 ctx.fillStyle = "#FFFFFF"; |
| 36 |
| 37 angle += 0.0039; |
| 38 for (var i = 0; i < particles.length; i++) { |
| 39 var p = particles[i]; |
| 40 p.x += Math.floor(Math.sin(angle)*5.0); |
| 41 p.y += 0.6*p.r; |
| 42 if (p.x > W) { |
| 43 p.x-=W; |
| 44 } |
| 45 if (p.x < 0) { |
| 46 p.x += W; |
| 47 } |
| 48 if(p.y>(H+1)){ |
| 49 p.y = 0; |
| 50 } |
| 51 if (IS_SKV8) { |
| 52 ctx.save(); |
| 53 ctx.translate(p.x, p.y); |
| 54 ctx.fill(p.path); |
| 55 ctx.restore(); |
| 56 } else { |
| 57 ctx.beginPath(); |
| 58 ctx.moveTo(p.x, p.y); |
| 59 ctx.arc(p.x, p.y, p.r, 0, 2*Math.PI, true); |
| 60 ctx.closePath(); |
| 61 ctx.fill(); |
| 62 } |
| 63 }; |
| 64 |
| 65 ticks++; |
| 66 if (IS_SKV8) { |
| 67 inval(); |
| 68 } |
| 69 } |
| 70 |
| 71 function fps() { |
| 72 console.log(ticks); |
| 73 ticks = 0; |
| 74 setTimeout(fps, 1000); |
| 75 } |
| 76 |
| 77 setTimeout(fps, 1000); |
| 78 |
| 79 return draw; |
| 80 }(); |
| 81 |
| 82 if (!IS_SKV8) { |
| 83 window.onload = function(){ |
| 84 var canvas = document.getElementById("snow"); |
| 85 var ctx = canvas.getContext("2d"); |
| 86 function drawCallback() { |
| 87 onDraw(ctx); |
| 88 setTimeout(drawCallback, 1); |
| 89 } |
| 90 setTimeout(drawCallback, 1); |
| 91 } |
| 92 } |
OLD | NEW |