-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompGrphHwk1.htm
324 lines (298 loc) · 9.64 KB
/
CompGrphHwk1.htm
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE HTML>
<html>
<head>
<title>Hwk 1</title>
<link rel="stylesheet" href="stick.css" type="text/css"/>
<!-- <script src="http://www.html5canvastutorials.com/libraries/kinetic2d-v1.0.2.js"></script> -->
<script src="kinetic2d-v1.0.2.js"></script>
<!--
Chris Russell-Walker
c.russell.walker@gmail.com
BU CS532 Comp Graphics
Project 1
-->
<script>
var kin;
var figure;
var figPos;
var swimPos = (Math.PI / 2);
var armAngle = 0;
var legAngle = 65;
var stroke = true;
var armStroke = true;
var bodyX = 0;
var bodyY = 40;
var headSize = 50;
var bodyHeight = 200;
var legLength = 120;
var armLength = 80;
var swimHeight = 50;
var waveHeight = 50;
function init() {
canvas = document.getElementById("stickScene");
context = canvas.getContext("2d");
swimHeight = canvas.height / 2;
figPos = canvas.width / 2;
document.addEventListener('mousedown', mouseClickHandler, false);
kin = new Kinetic_2d("stickScene");
kin.setDrawStage(function () {
var context = kin.getContext();
context.lineWidth = "4";
kin.clear();
run();
});
}
function mouseClickHandler(event) {
mouseX = event.clientX;
mouseY = event.clientY;
if (!kin.animating)
kin.startAnimation();
else
kin.stopAnimation();
}
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
function drawWaves() {
context.save();
context.moveTo(0, swimHeight);
// sine path from 0 to rads radians scales by sx
context.translate(-kin.frame * 2, 0);
var N = canvas.width;
var dx = 2 * (Math.PI) / N;
var x = 0;
var px = 0;
var py = waveHeight;
context.beginPath();
context.strokeStyle = "Blue";
for (var i = 0; i < N * 6; i++) {
x += dx;
y = Math.sin(x);
px += (180.0 / (Math.PI)) * dx;
py = -30 * y + (kin.frame % 2 * 3);
context.lineTo(px, py + waveHeight * 6.5);
}
context.stroke();
context.restore();
}
function cartesianToPolar(x, y) {
var length = Math.sqrt((x * x) + (y * y));
var angle = Math.atan(y / x);
var polarCrds = new Array(length, angle);
return polarCrds;
}
function polarToCartesian(length, theta) {
var x = Math.cos(theta) * length;
var y = Math.sin(theta) * length;
return (length, theta);
}
function run() {
context.fillText(kin.frame.toString(), 10, 10);
context.lineJoin = 'miterLimit';
figure = new stickFigure();
if (kin.animating) {
if (kin.frame % 2)
figPos += 5;
else
figPos -= 1;
switch (kin.frame % 20) {
case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9:
swimHeight = swimHeight + 2;
break;
case 10, 11, 12, 13, 14, 15, 16, 17, 18, 19:
swimHeight = swimHeight - 2;
break;
}
if (kin.frame >= 100 && kin.frame < 200) {
// bubbles
swimHeight = swimHeight + 1;
waveHeight = waveHeight - .25;
if (kin.frame < 150)
swimPos += .01;
else
swimPos -= .01;
}
if (kin.frame >= 200 && kin.frame < 300) {
// bubbles
swimHeight = swimHeight - 1;
waveHeight = waveHeight + .25;
if (kin.frame < 250)
swimPos -= .01;
else
swimPos += .01;
}
if (kin.frame != 0) {
if (legAngle > 120 || legAngle < 65)
stroke = !stroke;
if (legAngle <= 120 && stroke)
legAngle += 1;
if (legAngle >= 65 && !stroke)
legAngle -= 1;
if (armAngle > 100 || armAngle < 0)
armStroke = !armStroke;
if (armAngle <= 100 && armStroke)
armAngle += 2;
if (armAngle >= 0 && !armStroke)
armAngle -= 2;
}
if (kin.frame >= 400) {
kin.frame = 1;
figPos = -60;
}
}
figure.draw();
drawWaves();
}
/**** Stick Figure Objects ****/
function stickFigure() {
this.body = new body(bodyX, bodyY, bodyHeight);
this.head = new head(bodyX, bodyY, headSize);
this.leftArm = new arm(bodyX, bodyHeight / 2.6, -armLength, true);
this.rightArm = new arm(bodyX, bodyHeight / 2.6, armLength, false);
this.leftLeg = new leg(bodyX, bodyHeight, -legLength);
this.rightLeg = new leg(bodyX, bodyHeight, legLength);
this.draw = drawFigure;
}
function body(x, y, length) {
this.draw = drawBody;
this.x = x;
this.y = y;
this.length = length;
}
function head(x, y, radius) {
this.draw = drawHead;
this.x = x;
// Puts 'neck' (bottom of circle) at top of body
this.y = y - radius;
this.radius = radius;
}
function arm(x, y, armLength, left) {
this.x = x;
this.y = y;
this.length = armLength / 2;
this.left = left;
this.bicep = new bicep(this.x, this.y, this.length);
this.forearm = new forearm(this.x + Math.abs(this.length), this.y, this.length);
}
function leg(x, y, legLength) {
this.x = x;
this.y = y;
this.length = legLength / 2;
this.thigh = new thigh(this.x, this.y, this.length);
this.calf = new calf(this.x + Math.abs(length), this.y + length, this.length);
}
function bicep(x, y, length) {
this.draw = drawBicep;
this.x = x;
this.y = y;
this.length = length;
}
function forearm(x, y, length) {
this.draw = drawForearm;
this.x = x;
this.y = y;
this.length = length;
}
function thigh(x, y, length) {
this.draw = drawThigh;
this.x = x;
this.y = y;
this.length = length;
}
function calf(x, y, length) {
this.draw = drawCalf;
this.x = x;
this.y = y;
this.length = length;
}
/**** START FIGURE DRAWING ****/
function drawFigure() {
context.save(); // State 1
context.translate(figPos, swimHeight);
context.rotate(swimPos);
this.body.draw();
this.head.draw();
context.save(); // State 2
//LEFT ARM translate, etc
context.translate(0, bodyHeight / 2.6);
context.rotate(degToRad(armAngle));
this.leftArm.bicep.draw();
this.leftArm.forearm.draw(armAngle / 2);
// RIGHT ARM
context.rotate(degToRad(armAngle));
this.rightArm.bicep.draw();
this.rightArm.forearm.draw(armAngle / 2);
context.restore(); // State 2
context.save(); // State 3.1
// LEFT LEG translate, etc
context.translate(0, bodyHeight);
context.rotate(degToRad(-legAngle));
this.leftLeg.thigh.draw();
this.leftLeg.calf.draw(legAngle / 2);
context.restore(); // State 3.1
context.save(); // State 3.2
// RIGHT LEG translate, etc
context.translate(0, bodyHeight);
context.rotate(degToRad(legAngle));
this.rightLeg.thigh.draw();
this.rightLeg.calf.draw(legAngle / 2);
context.restore(); // State 3.2
context.restore(); // State 1
}
function drawBody() {
context.save();
context.moveTo(this.x, this.y);
context.strokeStyle = "Black";
context.lineTo(this.x, this.length);
context.stroke();
context.restore();
}
function drawHead() {
context.save();
context.beginPath();
context.fillStyle = "Red";
// x, y, radius, start-angle, end-angle
context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
context.fill();
context.closePath();
context.stroke();
context.restore();
}
function drawBicep() {
context.moveTo(0, 0);
context.lineTo(this.length, 0);
context.stroke();
}
function drawForearm(angle) {
context.save();
this.angle = angle;
context.translate(this.length, 0);
context.rotate(Math.PI / 180 * this.angle);
context.lineTo(this.length, 0);
context.stroke();
context.restore();
}
function drawThigh() {
context.moveTo(0, 0);
context.lineTo(this.length, 0);
context.stroke();
}
function drawCalf(angle) {
context.save();
this.angle = angle;
context.translate(this.length, 0);
context.rotate(Math.PI / 180 * this.angle);
context.lineTo(this.length, 0);
context.stroke();
context.restore();
}
/**** END FIGURE DRAWING ****/
window.onload = function () {
init();
};
</script>
</head>
<body onmousedown="return false;">
<canvas id="stickScene" width="960" height="640"></canvas>
</body>
</html>