-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.html
153 lines (128 loc) · 5.06 KB
/
visualizer.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
<!DOCTYPE html>
<html>
<head>
<style>
#container {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
border: 1px solid #ccc;
}
#thestage {
border: 1px solid #aaa;
}
#controls {
display: grid;
grid-template-columns: 1fr 1fr;
}
.control {
border: 1px solid #bbb;
vertical-align: top;
}
</style>
</head>
<body>
<div id="container">
<canvas id="thestage" width="600" height="600"></canvas>
<div id="controls">
<div class="control">
Problem: <select id="problem-select"></select>
</div>
<div class="control">
Paste:
<br>
<textarea id="json-textarea" cols="40" rows="10"></textarea>
<br>
<button id="display-json">Display</button>
<br>
<span id="error-message"></span>
</div>
</div>
</div>
<script src="https://code.createjs.com/1.0.0/easeljs.min.js"></script>
<script>
/******************************************************/
/* Visualization functions */
/******************************************************/
function createHole(vertices) {
let line = new createjs.Shape();
let start_x, start_y, holes;
[[start_x, start_y], ...holes] = vertices;
line.graphics.setStrokeStyle(1).beginStroke("#000000");
line.graphics.moveTo(start_x, start_y);
holes.forEach(hole => line.graphics.lineTo(hole[0], hole[1]));
line.graphics.lineTo(start_x, start_y);
line.graphics.endStroke();
line.scaleX = 4.0;
line.scaleY = 4.0;
return line;
}
function createFigure(vertices, edges) {
let figure = new createjs.Container();
edges.forEach(edge => {
let line = new createjs.Shape();
line.graphics.setStrokeStyle(1).beginStroke("#ff0000");
line.graphics.moveTo(vertices[edge[0]][0], vertices[edge[0]][1]);
line.graphics.lineTo(vertices[edge[1]][0], vertices[edge[1]][1]);
line.graphics.endStroke();
figure.addChild(line);
});
figure.scaleX = 4.0;
figure.scaleY = 4.0;
return figure;
}
/*
params:
canvas: id of the html element to draw on
problem: the problem json
*/
function visualize(canvas, problem) {
let stage = new createjs.Stage(canvas);
let hole = createHole(problem.hole);
stage.addChild(hole);
let figure = createFigure(problem.figure.vertices, problem.figure.edges);
stage.addChild(figure);
stage.update();
}
/******************************************************/
/* Controls */
/******************************************************/
function configureSelect(canvas) {
const selector = document.getElementById('problem-select');
for (let i = 1; i <= 59; i++) {
let option = document.createElement('option');
option.value = `${i}.json`;
option.text = i;
selector.add(option);
}
selector.addEventListener('change', event => {
let fileName = event.target.value;
fetch(`./problems/${fileName}`)
.then(response => response.json())
.then(json => visualize(canvas, json));
});
}
function configureDisplayJson(canvas) {
const button = document.getElementById('display-json');
const textarea = document.getElementById('json-textarea');
const errorMessage = document.getElementById('error-message');
button.addEventListener('click', () => {
try {
errorMessage.innerHTML = "";
const json = JSON.parse(textarea.value);
visualize(canvas, json);
} catch (ex) {
errorMessage.innerHTML = ex.message;
}
});
}
function init() {
configureSelect("thestage");
configureDisplayJson("thestage");
}
init();
</script>
</body>
</html>