aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SkV8Example/js/gears.js
blob: a901f9d9211e927df000e4385ff33e5838155805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var IS_SKV8 = typeof document == "undefined";
var HAS_PATH = typeof Path2D != "undefined";
var HAS_DISPLAY_LIST = typeof DisplayList != "undefined";

var NumTeeth = 24;
var NumGears = 60;
var DeltaTheta = Math.PI/90;
var FaceColors = ["#000099", "#006600", "#990000", "#EEEE00"];
var SideColors = ["#0000FF", "#009900", "#FF0000", "#CCCC00"];

function makeGear(pathLike, r) {
  var dT = Math.PI*2/NumTeeth;
  var dTq = dT/4;
  var outer = r;
  var inner = 0.7 * r;
  pathLike.moveTo(Math.sin(-2*dTq)*outer, Math.cos(-2*dTq)*outer);
  for (var i=0; i<NumTeeth; i+=2) {
    pathLike.lineTo(Math.sin(dT*i-dTq)*outer, Math.cos(dT*i-dTq)*outer);
    pathLike.lineTo(Math.sin(dT*i+dTq)*inner, Math.cos(dT*i+dTq)*inner);
    pathLike.lineTo(Math.sin(dT*(i+1)-dTq)*inner, Math.cos(dT*(i+1)-dTq)*inner);
    pathLike.lineTo(Math.sin(dT*(i+1)+dTq)*outer, Math.cos(dT*(i+1)+dTq)*outer);
  }
}

function gearPath(r) {
  if (HAS_PATH) {
    p = new Path2D();
    makeGear(p, r)
    p.closePath();
    return p;
  } else {
    return null;
  }
}

function gearDisplayListStroke(r, color) {
  if (HAS_DISPLAY_LIST) {
    p = new Path2D();
    makeGear(p, r)
    p.closePath();
    var dl = new DisplayList();
    dl.strokeStyle = color;
    dl.stroke(p);
    dl.finalize()
    return dl;
  } else {
    return null;
  }
}

function gearDisplayListFill(r, color) {
  if (HAS_DISPLAY_LIST) {
    p = new Path2D();
    makeGear(p, r)
    p.closePath();
    var dl = new DisplayList();
    dl.fillStyle = color;
    dl.fill(p);
    dl.finalize()
    return dl;
  } else {
    return null;
  }
}

function strokeGear(ctx, gear) {
  if (HAS_PATH) {
    ctx.stroke(gear.path);
  } else {
    ctx.beginPath();
    makeGear(ctx, gear.r);
    ctx.closePath();
    ctx.stroke();
  }
}

function fillGear(ctx) {
  if (HAS_PATH) {
    ctx.fill(gear.path);
  } else {
    ctx.beginPath();
    makeGear(ctx, gear.r);
    ctx.closePath();
    ctx.fill();
  }
}

function draw3DGear(ctx, angle, gear) {
  ctx.strokeStyle = gear.sideColor;
  ctx.fillStyle = gear.faceColor;
  ctx.rotate(angle);
  strokeGear(ctx, gear);
  for (var i=0; i < 20; i++) {
    ctx.rotate(-angle);
    ctx.translate(0.707, 0.707);
    ctx.rotate(angle);
    if (HAS_DISPLAY_LIST) {
        ctx.draw(gear.gearStroke);
    } else {
        strokeGear(ctx, gear);
    }
  }
  if (HAS_DISPLAY_LIST) {
      ctx.draw(gear.gearFill);
  } else {
      fillGear(ctx, gear);
  }
  ctx.rotate(-angle);
}

function draw3DGearAt(ctx, angle, gear) {
  ctx.save();
  ctx.translate(gear.x, gear.y);
  draw3DGear(ctx, angle, gear);
  ctx.restore();
}

var onDraw = function() {
  var ticks=0;
  var rotation = 0;
  var gears = [];

  for (var i=0; i<NumGears; i++) {
    color = Math.floor(Math.random()*FaceColors.length);
    r = Math.random()*100+5;
    gears.push({
        x: Math.random()*500,
        y: Math.random()*500,
        path: gearPath(r),
        gearFill: gearDisplayListFill(r, FaceColors[color]),
        gearStroke: gearDisplayListStroke(r, SideColors[color]),
        r: r,
        faceColor: FaceColors[color],
        sideColor: SideColors[color]
    });
  }

  function draw(ctx) {
    ctx.resetTransform();

    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, 499, 499);

    rotation += DeltaTheta;
    if (rotation >= Math.PI*2) {
      rotation = 0;
    }

    for (var i=0; i < gears.length; i++) {
      gear = gears[i];
      draw3DGearAt(ctx, rotation, gear);
    }

    ticks++;
    if (IS_SKV8) {
      inval();
    }
  };

  function fps() {
    console.log(ticks);
    ticks = 0;
    setTimeout(fps, 1000);
  };

  setTimeout(fps, 1000);

  return draw;
}();

if (!IS_SKV8) {
  window.onload = function(){
    var canvas = document.getElementById("gears");
    var ctx = canvas.getContext("2d");
    function drawCallback() {
      onDraw(ctx);
      setTimeout(drawCallback, 1);
    }
    setTimeout(drawCallback, 1);
  }
}

console.log("HAS_PATH: " + HAS_PATH);