commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 1 | var IS_SKV8 = typeof document == "undefined"; |
| 2 | var HAS_PATH = typeof Path != "undefined"; |
| 3 | |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 4 | var NumTeeth = 24; |
| 5 | var NumGears = 60; |
| 6 | var DeltaTheta = Math.PI/90; |
| 7 | var FaceColors = ["#000099", "#006600", "#990000", "#EEEE00"]; |
| 8 | var SideColors = ["#0000FF", "#009900", "#FF0000", "#CCCC00"]; |
| 9 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 10 | function makeGear(pathLike, r) { |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 11 | var dT = Math.PI*2/NumTeeth; |
| 12 | var dTq = dT/4; |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 13 | var outer = r; |
| 14 | var inner = 0.7 * r; |
| 15 | pathLike.moveTo(Math.sin(-2*dTq)*outer, Math.cos(-2*dTq)*outer); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 16 | for (var i=0; i<NumTeeth; i+=2) { |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 17 | pathLike.lineTo(Math.sin(dT*i-dTq)*outer, Math.cos(dT*i-dTq)*outer); |
| 18 | pathLike.lineTo(Math.sin(dT*i+dTq)*inner, Math.cos(dT*i+dTq)*inner); |
| 19 | pathLike.lineTo(Math.sin(dT*(i+1)-dTq)*inner, Math.cos(dT*(i+1)-dTq)*inner); |
| 20 | pathLike.lineTo(Math.sin(dT*(i+1)+dTq)*outer, Math.cos(dT*(i+1)+dTq)*outer); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 21 | } |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 22 | } |
| 23 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 24 | function gearPath(r) { |
| 25 | if (HAS_PATH) { |
| 26 | p = new Path(); |
| 27 | makeGear(p, r) |
| 28 | p.closePath(); |
| 29 | return p; |
| 30 | } else { |
| 31 | return null; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | function strokeGear(ctx, gear) { |
| 36 | if (HAS_PATH) { |
| 37 | ctx.stroke(gear.path); |
| 38 | } else { |
| 39 | ctx.beginPath(); |
| 40 | makeGear(ctx, gear.r); |
| 41 | ctx.closePath(); |
| 42 | ctx.stroke(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function fillGear(ctx) { |
| 47 | if (HAS_PATH) { |
| 48 | ctx.fill(gear.path); |
| 49 | } else { |
| 50 | ctx.beginPath(); |
| 51 | makeGear(ctx, gear.r); |
| 52 | ctx.closePath(); |
| 53 | ctx.fill(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function draw3DGear(ctx, angle, gear) { |
| 58 | ctx.strokeStyle = gear.sideColor; |
| 59 | ctx.fillStyle = gear.faceColor; |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 60 | ctx.rotate(angle); |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 61 | strokeGear(ctx, gear); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 62 | for (var i=0; i < 20; i++) { |
| 63 | ctx.rotate(-angle); |
| 64 | ctx.translate(0.707, 0.707); |
| 65 | ctx.rotate(angle); |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 66 | strokeGear(ctx, gear); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 67 | } |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 68 | fillGear(ctx, gear); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 69 | ctx.rotate(-angle); |
| 70 | } |
| 71 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 72 | function draw3DGearAt(ctx, angle, gear) { |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 73 | ctx.save(); |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 74 | ctx.translate(gear.x, gear.y); |
| 75 | draw3DGear(ctx, angle, gear); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 76 | ctx.restore(); |
| 77 | } |
| 78 | |
| 79 | var onDraw = function() { |
| 80 | var ticks=0; |
| 81 | var rotation = 0; |
| 82 | var gears = []; |
| 83 | |
| 84 | for (var i=0; i<NumGears; i++) { |
| 85 | color = Math.floor(Math.random()*FaceColors.length); |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 86 | r = Math.random()*100+5; |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 87 | gears.push({ |
| 88 | x: Math.random()*500, |
| 89 | y: Math.random()*500, |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 90 | path: gearPath(r), |
| 91 | r: r, |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 92 | faceColor: FaceColors[color], |
| 93 | sideColor: SideColors[color] |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | function draw(ctx) { |
| 98 | ctx.resetTransform(); |
| 99 | |
commit-bot@chromium.org | 58c0fa3 | 2014-01-16 19:48:33 +0000 | [diff] [blame^] | 100 | ctx.fillStyle = "#FFFFFF"; |
| 101 | ctx.fillRect(0, 0, 499, 499); |
| 102 | |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 103 | rotation += DeltaTheta; |
| 104 | if (rotation >= Math.PI*2) { |
| 105 | rotation = 0; |
| 106 | } |
| 107 | |
| 108 | for (var i=0; i < gears.length; i++) { |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 109 | gear = gears[i]; |
| 110 | draw3DGearAt(ctx, rotation, gear); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ticks++; |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 114 | if (IS_SKV8) { |
| 115 | inval(); |
| 116 | } |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | function fps() { |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 120 | console.log(ticks); |
commit-bot@chromium.org | d784104 | 2014-01-07 19:00:27 +0000 | [diff] [blame] | 121 | ticks = 0; |
| 122 | setTimeout(fps, 1000); |
| 123 | }; |
| 124 | |
| 125 | setTimeout(fps, 1000); |
| 126 | |
| 127 | return draw; |
| 128 | }(); |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 129 | |
| 130 | if (!IS_SKV8) { |
| 131 | window.onload = function(){ |
| 132 | var canvas = document.getElementById("gears"); |
| 133 | var ctx = canvas.getContext("2d"); |
| 134 | function drawCallback() { |
| 135 | onDraw(ctx); |
| 136 | setTimeout(drawCallback, 1); |
| 137 | } |
| 138 | setTimeout(drawCallback, 1); |
| 139 | } |
| 140 | } |
| 141 | |