blob: 5ba2f995c25782c70175adf65f296b02ff250420 [file] [log] [blame]
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +00001var IS_SKV8 = typeof document == "undefined";
2var HAS_PATH = typeof Path != "undefined";
3
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +00004var NumTeeth = 24;
5var NumGears = 60;
6var DeltaTheta = Math.PI/90;
7var FaceColors = ["#000099", "#006600", "#990000", "#EEEE00"];
8var SideColors = ["#0000FF", "#009900", "#FF0000", "#CCCC00"];
9
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000010function makeGear(pathLike, r) {
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000011 var dT = Math.PI*2/NumTeeth;
12 var dTq = dT/4;
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000013 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.orgd7841042014-01-07 19:00:27 +000016 for (var i=0; i<NumTeeth; i+=2) {
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000017 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.orgd7841042014-01-07 19:00:27 +000021 }
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000022}
23
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000024function 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
35function 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
46function 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
57function draw3DGear(ctx, angle, gear) {
58 ctx.strokeStyle = gear.sideColor;
59 ctx.fillStyle = gear.faceColor;
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000060 ctx.rotate(angle);
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000061 strokeGear(ctx, gear);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000062 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.orgcd110182014-01-10 21:33:01 +000066 strokeGear(ctx, gear);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000067 }
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000068 fillGear(ctx, gear);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000069 ctx.rotate(-angle);
70}
71
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000072function draw3DGearAt(ctx, angle, gear) {
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000073 ctx.save();
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000074 ctx.translate(gear.x, gear.y);
75 draw3DGear(ctx, angle, gear);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000076 ctx.restore();
77}
78
79var 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.orgcd110182014-01-10 21:33:01 +000086 r = Math.random()*100+5;
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000087 gears.push({
88 x: Math.random()*500,
89 y: Math.random()*500,
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +000090 path: gearPath(r),
91 r: r,
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +000092 faceColor: FaceColors[color],
93 sideColor: SideColors[color]
94 });
95 }
96
97 function draw(ctx) {
98 ctx.resetTransform();
99
commit-bot@chromium.org58c0fa32014-01-16 19:48:33 +0000100 ctx.fillStyle = "#FFFFFF";
101 ctx.fillRect(0, 0, 499, 499);
102
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +0000103 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.orgcd110182014-01-10 21:33:01 +0000109 gear = gears[i];
110 draw3DGearAt(ctx, rotation, gear);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +0000111 }
112
113 ticks++;
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000114 if (IS_SKV8) {
115 inval();
116 }
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +0000117 };
118
119 function fps() {
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000120 console.log(ticks);
commit-bot@chromium.orgd7841042014-01-07 19:00:27 +0000121 ticks = 0;
122 setTimeout(fps, 1000);
123 };
124
125 setTimeout(fps, 1000);
126
127 return draw;
128}();
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000129
130if (!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