-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
328 lines (273 loc) · 10.7 KB
/
index.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
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
325
326
327
328
<!DOCTYPE html>
<style>
canvas {
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
touch-action: none;
border: 1px solid #808080;
}
</style>
<html>
<head>
<title>Complexify</title>
<script src="https://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="./complex/dst/complex.min.js"></script>
<script language="javascript" src="./js/dot.js" type="text/javascript"></script>
<script language="javascript" src="./js/view.js" type="text/javascript"></script>
<script language="javascript" src="./js/port.js" type="text/javascript"></script>
<script language="javascript" src="./js/camera.js" type="text/javascript"></script>
<link rel="icon" type="image/png" href="/favicon.png"/>
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<div id="nav-container"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#nav-container').load('/nav.html');
});
</script>
<div class="game">
<h2>COMPLEXIFY</h2>
<canvas width="512" height="512" id="gameCanvas">Canvas tag not supported</canvas>
<h3 id="prompt"></h3>
<p><i>Hold SHIFT to add and tap SPACE to apply to all points</i></p>
</div>
<div id="footer-container"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#footer-container').load('/footer.html');
});
</script>
<script type='text/javascript'>
var prompt = document.getElementById("prompt");
var canvas = document.getElementById("gameCanvas");
var context = canvas.getContext("2d");
var CANVAS_WIDTH = canvas.offsetWidth;
var CANVAS_HEIGHT = canvas.offsetHeight;
canvas.addEventListener("mousedown", onMouseDown, false);
canvas.addEventListener("mouseup", onMouseUp, false);
canvas.addEventListener("mousemove", onMouseMove, false);
canvas.addEventListener("touchstart", onTouchStart, false);
canvas.addEventListener("touchend", onTouchEnd, false);
canvas.addEventListener("touchcancel", onTouchCancel, false);
canvas.addEventListener("touchmove", onTouchMove, false);
canvas.addEventListener("mousewheel", handleScroll);
window.addEventListener("keydown", onKeyDown, true);
window.addEventListener("keyup", onKeyUp, true);
var view = new View();
var port = new Port();
var camera = new Camera();
var mousePointView = {x:0, y:0}
var mousePointWorld = {x:0, y:0}
var dots = [];
var newDots = [];
var transformation = new Complex.Cartesian(0, 0);
var startDot = new Dot(new Complex.Cartesian(1, 0), null);
//dots.push(startDot);
var multiply = true;
var iterate = false;
var curb = false;
function start(){
draw();
refreshPrompt();
}
function clear(){
context.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
}
function draw(){
clear();
camera.drawCircle({x:0, y:0}, 1, "#eee");
camera.drawLine({x:-0.95, y:0}, {x:0.95, y:0}, 0.1, "black");
camera.drawLine({x:0, y:-0.95}, {x:0, y:0.95}, 0.1, "black");
for (var i = 0; i < dots.length; i++) {
dots[i].draw();
}
//camera.drawCircle(mousePointWorld, 0.2, "red");
var s = (multiply?"*":"+");
if (iterate) s = s+s+s;
s = s+pointStringC(mousePointWorld);
//port.drawText(s, {x:0.03, y:0.95}, 0.05, "left", "black");
}
function transform(c){
if (multiply) return c.multiply(transformation);
return c.plus(transformation);
}
function refreshPrompt(){
var s = "";
s += (multiply?"Multiply":"Add");
s += " ";
s += (iterate?"all points":"last points");
s += " ";
s += (multiply?"by":"to");
s += " ";
s += pointStringC(mousePointWorld);
prompt.innerHTML = s;
}
function onMouseDown(event){
draw();
}
function onMouseUp(event){
var pushDots = [];
var newDot;
var l = dots.length;
if (l == 0) {
newDot = new Dot(transformation, null);
dots.push(newDot);
pushDots.push(newDot);
}
else if (iterate) {// && !curb) {
for (var i = 0; i < l; i++) {
newDot = new Dot(transform(dots[i].value), dots[i]);
dots.push(newDot);
pushDots.push(newDot);
//dots[i].setValue(transform(dots[i].value));
}
curb = true;
}
else {
for (var i = 0; i < newDots.length; i++) {
newDot = new Dot(transform(newDots[i].value), newDots[i]);
dots.push(newDot);
pushDots.push(newDot);
//dots[i].setValue(transform(dots[i].value));
}
}
newDots = pushDots;
iterate = false;
//dots.push(new Dot(transformation));
draw();
}
function onMouseMove(event){
mousePointView = getMousePos(canvas, event);
mousePointWorld = camera.transformPoint(mousePointView);
mousePointWorld.x = trunc(mousePointWorld.x);
mousePointWorld.y = trunc(mousePointWorld.y);
transformation = new Complex.Cartesian(mousePointWorld.x, mousePointWorld.y);
//console.log("Moving to "+transformation.toString());
draw();
refreshPrompt();
}
function onTouchStart(event){
var touch = event.touches[0];
var mouseEvent = new MouseEvent("mousedown", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}
function onTouchEnd(event){
var touch = event.touches[0];
var mouseEvent = new MouseEvent("mouseup");
canvas.dispatchEvent(mouseEvent);
}
function onTouchMove(event){
var touch = event.touches[0];
var mouseEvent = new MouseEvent("mousemove", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}
function onTouchCancel(event){
}
function onKeyUp(event){
console.log("UP Keycode="+event.keyCode);
if (event.keyCode == 16) {
multiply = true;
event.preventDefault();
draw();
}
/*
if (event.keyCode == 0 || event.keyCode == 32) {
iterate = false;
event.preventDefault();
draw();
}
*/
refreshPrompt();
}
function onKeyDown(event){
console.log("DOWN Keycode="+event.keyCode);
if (event.keyCode == 16) {
multiply = false;
event.preventDefault();
draw();
}
if (event.keyCode == 0 || event.keyCode == 32) {
iterate = !iterate;
event.preventDefault();
draw();
}
/*
if (event.keyCode == 0 || event.keyCode == 32 || event.keyCode == 81) {
multiply = !multiply;
event.preventDefault();
draw();
}
if (event.keyCode == 13 || event.keyCode == 69) {
iterate = !iterate;
event.preventDefault();
draw();
}
if (event.keyCode == 38 || event.keyCode == 87) {
camera.setFov(camera.fov/2);
event.preventDefault();
draw();
}
if (event.keyCode == 40 || event.keyCode == 83) {
camera.setFov(camera.fov*2);
event.preventDefault();
draw();
}
*/
if (event.keyCode == 66) {
dots = [];
newDots = [];
camera.setFov(8);
event.preventDefault();
draw();
}
refreshPrompt();
}
function handleScroll(event){
console.log("Scrolling");
event.preventDefault();
var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
camera.fov = Math.max(2, camera.fov-delta);
draw();
}
function getMousePos(canvas, event){
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
function complexToPoint(c){
return {x: c.real, y: c.imag}
}
function pointString(point){
return "("+trunc(point.x)+", "+trunc(point.y)+")";
}
function pointStringC(point){
return "("+trunc(point.x)+" + "+trunc(point.y)+"i)";
}
function xyString(x, y){
return "("+trunc(x)+", "+trunc(y)+")";
}
function trunc(v){
return Math.round(v*10)/10;
}
function distance(a, b){
return Math.sqrt((a.x-b.x)**2+(a.y-b.y)**2);
}
function length(v){
return Math.sqrt(v.x**2+v.y**2);
}
start();
</script>
</body>
</html>