-
Notifications
You must be signed in to change notification settings - Fork 0
/
StickStarter.html
164 lines (137 loc) · 5.26 KB
/
StickStarter.html
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
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="stick.css" type="text/css">
<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"> </script>
<script src="kinetic2d-v1.0.2.js"></script>
<script>
var kin;
var vertA = new Array(0, 82.5);
var vertB = new Array(-60, -37.5);
var vertC = new Array(60, -37.5);
var vertX = new Array(0, 110);
var vertY = new Array(-80, -50);
var vertZ = new Array(80, -50);
var squareSize = 250;
var rect = new Array([-squareSize, -squareSize],
[-squareSize, squareSize],
[squareSize, squareSize],
[squareSize, -squareSize]);
var innerTrigSpin = false;
var outerTrigSpin = false;
var squareSpin = false;
var innerTrigAngle;
var outerTrigAngle;
var squareAngle;
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
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;
var cartCrds = new Array(length, theta);
return cartCrds;
}
function init() {
innerTrigAngle = 0;
outerTrigAngle = 0;
squareAngle = 0;
// Set up mouse/keyboard events
$(window).keydown(keyHandler);
canvas = document.getElementById("stickScene");
context = canvas.getContext("2d");
context.strokeStyle = "Red";
context.lineJoin = 'round';
context.lineWidth = 5;
kin = new Kinetic_2d("stickScene");
kin.setDrawStage(function () {
var context = kin.getContext();
kin.clear();
run();
});
}
// Key Handlers
function keyHandler(e) {
// "A" - Inner triangle clockwise
if (e.keyCode == 65)
innerTrigSpin = !innerTrigSpin;
// "S" - Outer triangle ctr-clockwise
if (e.keyCode == 83)
outerTrigSpin = !outerTrigSpin;
// "D" - Square clockwise
if (e.keyCode == 68)
squareSpin = !squareSpin;
}
function run() {
// save state 1
context.save();
context.translate(canvas.width / 4, canvas.height / 4);
// save state 2
context.save();
context.translate(150, 150);
context.scale(0.5, 0.5);
context.translate((canvas.width / 8) - 20, (canvas.height / 8) + 20);
if (innerTrigSpin) {
innerTrigAngle += 1;
context.rotate(degToRad(innerTrigAngle));
}
drawTriangle(vertA, vertB, vertC);
context.restore();
// save state 3
context.save();
context.scale(2, 2);
context.translate((canvas.width / 8) - 20, (canvas.height / 8) + 20);
if (outerTrigSpin) {
outerTrigAngle -= 1.5;
context.rotate(degToRad(outerTrigAngle));
}
drawTriangle(vertX, vertY, vertZ);
context.restore();
context.translate(200, 200);
drawRectangle(rect[0], rect[1], rect[2], rect[3]);
context.restore();
}
function drawRectangle(coordA, coordB, coordC, coordD) {
// save state
context.save();
if (squareSpin) {
squareAngle += 0.5;
context.rotate(degToRad(squareAngle));
}
// draw sides using state
drawSide(coordA, coordB);
drawSide(coordB, coordC);
drawSide(coordC, coordD);
drawSide(coordD, coordA);
context.restore();
}
function drawTriangle(coordA, coordB, coordC) {
// save state
context.save();
// draw sides using state
drawSide(coordA, coordB);
drawSide(coordB, coordC);
drawSide(coordC, coordA);
context.restore();
}
function drawSide(start, end) {
context.beginPath();
context.moveTo(start[0], start[1]);
context.lineTo(end[0], end[1]);
context.stroke();
}
window.onload = function () {
init();
};
</script>
</head>
<body onmousedown="return false;">
<canvas id="stickScene" width="960" height="640"></canvas>
</body>
</html>