This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
/
main.js
202 lines (163 loc) · 6.41 KB
/
main.js
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
(function() {
"use strict";
var Point = require("./modules/objects/point.js"),
grapher = require("./modules/logic/grapher.js"),
renderer = require("./modules/logic/renderer.js"),
profiling = require("./modules/utils/profiling.js"),
random = require("./modules/utils/random.js");
var canvasId = "hexagonif",
canvasContainerId = "hexagonif-container";
function getCanvas() {
var canvas = document.getElementById(canvasId);
return canvas;
}
function createCanvas() {
var container = document.getElementById(canvasContainerId),
canvas,
i;
for (i = 0; i < container.childElementCount; i++) {
container.removeChild(container.children[i]);
};
canvas = document.createElement("canvas");
canvas.id = canvasId;
container.insertBefore(canvas, container.children[0]);
return canvas;
}
function oneAtATimePlease(fn) {
var running = false,
wrapper = function() {
if (running) {
return;
}
running = true;
fn.call(null);
running = false;
}
return wrapper;
}
function calculateHexagonSideLength() {
var canvas = getCanvas(),
absoluteMin = 50,
absoluteMax = 150,
shortestCanvasSide = Math.min(canvas.scrollWidth, canvas.scrollHeight),
min = Math.max(absoluteMin, shortestCanvasSide / 20),
max = Math.min(absoluteMax, shortestCanvasSide / 10);
return random.integer(min, max);
}
function generateAndRender() {
var canvas = createCanvas(),
canvasArea = new Point(canvas.scrollWidth, canvas.scrollHeight),
hexagonSideLength = calculateHexagonSideLength(),
profiledGrapher = profiling.wrap("grapher", function() {
return grapher(canvasArea, hexagonSideLength);
}),
graphObjects = profiledGrapher(),
profiledRenderer = profiling.wrap("renderer", function() {
return renderer(canvasId, canvasArea, graphObjects.lines);
}),
getRandomObject = function(objects) {
var keys = Object.keys(objects),
count = keys.length,
rnd = random.integer(0, count),
key = keys[rnd],
object = objects[key];
return object;
},
getRandomHexagon = function() {
var hexagon = getRandomObject(graphObjects.hexagons);
return hexagon;
},
getRandomLine = function() {
var line = getRandomObject(graphObjects.lines);
return line;
},
getRandomNode = function() {
var node = getRandomObject(graphObjects.nodes);
return node;
},
highlightOnInterval = function() {
var highlightHasHappened = false,
isAutomatedHighlight = false,
interval;
document.addEventListener("hexagonif.line.highlight", function() {
highlightHasHappened = highlightHasHappened || !isAutomatedHighlight;
});
document.addEventListener("hexagonif.line.unhighlight", function() {
// Something
});
function highlightSomething() {
var line = getRandomLine();
isAutomatedHighlight = true;
scene.highlightLine(line);
isAutomatedHighlight = false;
setTimeout(function() {
scene.unhighlightLine(line);
}, 500);
}
function highlightSomethingThatIfNothingHasHappened() {
if (!highlightHasHappened) {
highlightSomething();
}
}
interval = setInterval(highlightSomethingThatIfNothingHasHappened, 1000);
},
scene = profiledRenderer();
highlightOnInterval();
}
function once(fn) {
var hasRun = false,
runOnceCheck = function() {
if (!hasRun) {
hasRun = true;
fn.call(null);
}
};
return runOnceCheck;
}
function onMouseDetector(fn) {
// Experimental code to detect if a mouse pointing device is used.
// If a mouse is detected, call the supplied function once.
var onTouchMoveEventArgs = {
target: null,
},
onTouchMove = function(e) {
onTouchMoveEventArgs.target = e.target;
},
onMouseMove = function(e) {
// If the target isn't the same, the assumption is that the touchmove event wasn't fired first - hence it's not a touch event.
// TODO: would be better to use the mouse event .x and .y, if matching ones exist in touchmove etcetera.
if (onTouchMoveEventArgs.target !== e.target) {
onDetect();
}
// Release pointer
onTouchMoveEventArgs.target = null;
},
onDetect = once(function() {
document.removeEventListener("touchmove", onTouchMove);
document.removeEventListener("mousemove", onMouseMove);
fn.call(null);
});
document.addEventListener("touchmove", onTouchMove);
document.addEventListener("mousemove", onMouseMove);
}
function onResizeDetector(fn) {
// Currently not checking for height changes because that would
// reset the canvas every time the developer console was toggled
var previousCanvasWidth = 0;
window.addEventListener("resize", function() {
var canvas = getCanvas();
if (!canvas) {
fn.call(null);
return;
}
if (previousCanvasWidth !== canvas.scrollWidth) {
previousCanvasWidth = canvas.scrollWidth;
fn.call(null);
return;
}
});
}
var run = oneAtATimePlease(generateAndRender);
onMouseDetector(once(run));
onResizeDetector(run);
}());