-
Notifications
You must be signed in to change notification settings - Fork 0
/
magicboard.hta
342 lines (320 loc) · 13 KB
/
magicboard.hta
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='x-ua-compatible' content='ie=9'/>
<HTA:APPLICATION
APPLICATIONNAME="Magic Board"
BORDER="none"
CONTEXTMENU="yes"
INNERBORDER="no"
NAVIGABLE="no"
SCROLL="no"
SELECTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="no"
VERSION="0.0"
WINDOWSTATE="maximize"/>
<title>Magic Board</title>
<style>
html, body {
padding: 0;
margin: 0;
}
#toolbox {
display: block;
position: fixed;
right: 0;
top: 1em;
z-index: 1;
background-color: white;
border: solid;
padding: 1em;
border-collapse: separate;
box-shadow: 0.5em 0.5em 1em rgba(0, 0, 0, 0.5);
text-align: center;
}
#toolbox section > * {
display: inline-block; /* For horizontal button placement. */
cursor: pointer;
vertical-align: top; /* For hypothetical weird spacing issues. */
width: 1em;
height: 1em;
}
#toolbox section > *:active {
background-color: rgba(0, 0, 0, 0.1);
}
#toolbox .stroke {
border-radius: 50%;
}
#toolbox [disabled] {
pointer-events: none;
}
#toolbox .first-enabled [disabled] {
display: none;
}
#toolbox .first-enabled :not([disabled]) ~ :not([disabled]) {
/* Not the first enabled */
display: none;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="toolbox">
<section>
<div class=navigation data-action=previous>←</div>
<div class=navigation data-action=refresh>↻</div>
<span class=first-enabled>
<div class=navigation data-action=next>→</div>
<div class=navigation data-action=new disabled>🆕</div>
</span>
</section>
<section>
<div class=tool data-tool=pencil>✏️</div>
</section>
<section>
<div class=stroke data-colour=black></div>
<div class=stroke data-colour=red></div>
<div class=stroke data-colour=green></div>
</section>
</div>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var toolbox = document.getElementById("toolbox");
canvas.setAttribute('width', window.innerWidth);
canvas.setAttribute('height', window.innerHeight);
var pages = [], page_num = 0;
new_page();
var is_mouse_down = false;
canvas.addEventListener('mousedown', function(e) {
is_mouse_down = true;
pages[page_num].events.push({
type: "down",
x: e.pageX,
y: e.pageY
});
draw_event();
});
canvas.addEventListener('mousemove', function(e) {
if (!is_mouse_down) return;
pages[page_num].events.push({
type: "move",
x: e.pageX,
y: e.pageY
});
draw_event();
});
canvas.addEventListener('mouseup', function(e) {
is_mouse_down = false;
pages[page_num].events.push({
type: "up",
x: e.pageX,
y: e.pageY
});
draw_event();
});
document.addEventListener('keydown', function(e) {
// Disable F5
if (event.keyCode == 116) {
window.event.returnValue = false;
}
});
(function(){
var strokes = document.querySelectorAll('.stroke');
for (var i = 0; i < strokes.length; i += 1) {
var stroke = strokes[i];
stroke.style.backgroundColor = stroke.getAttribute('data-colour');
stroke.addEventListener('click', set_stroke);
}
function set_stroke(e) {
pages[page_num].events.push({
type: "set-stroke",
stroke: this.getAttribute('data-colour')
});
draw_event();
}
})();
(function(){
var navs = document.querySelectorAll('.navigation');
for (var i = 0; i < navs.length; i += 1) {
var nav = navs[i];
var handler;
switch (nav.getAttribute('data-action')) {
case "previous":
handler = previous;
break;
case "next":
handler = next;
break;
case "refresh":
handler = refresh;
break;
case "new":
handler = new_;
break;
}
nav.addEventListener('click', handler);
}
update_nav_state();
function previous(e) {
if (page_num > 0) page_num -= 1;
redraw_page();
update_nav_state();
}
function next(e) {
if (page_num < pages.length - 1) page_num += 1;
redraw_page();
update_nav_state();
}
function refresh(e) {
redraw_page();
update_nav_state();
}
function new_(e) {
new_page(pages[page_num]);
update_nav_state();
}
function update_nav_state() {
var can = {
"previous": (page_num > 0),
"next": (page_num < pages.length - 1),
"refresh": true,
"new": true
}
for (var i = 0; i < navs.length; i += 1) {
var nav = navs[i];
if (can[nav.getAttribute('data-action')]) {
nav.removeAttribute("disabled");
} else {
nav.setAttribute("disabled", "");
}
}
}
})();
function new_page(template) {
var events = [
// Line
{type: "set-width", width: ctx.lineWidth},
{type: "set-cap", cap: ctx.lineCap},
{type: "set-join", join: ctx.lineJoin},
{type: "set-miter-limit", limit: ctx.miterLimit},
{type: "set-dash", pattern: ctx.getLineDash(), offset: ctx.lineDashOffset},
// Text
{type: "set-font", font: ctx.font},
{type: "set-align", align: ctx.textAlign},
{type: "set-baseline", baseline: ctx.textBaseline},
{type: "set-direction", direction: ctx.direction},
// Fill and stroke
{type: "set-fill", fill: ctx.fillStyle},
{type: "set-stroke", stroke: ctx.strokeStyle},
// Shadows
{type: "set-shadow-blur", blur: ctx.shadowBlur},
{type: "set-shadow-colour", colour: ctx.shadowColor},
{type: "set-shadow-offset", x: ctx.shadowOffsetX, y: ctx.shadowOffsetY},
// Compositing
{type: "set-alpha", alpha: ctx.globalAlpha},
{type: "set-composite", mode: ctx.globalCompositeOperation}
];
if (typeof template == "undefined") {
pages.push({
events: events,
background: null,
grid: null
});
} else {
pages.push({
events: events,
background: template.background,
grid: template.grid
});
}
}
function redraw_page(page) {
if (typeof page == "undefined") {
page = pages[page_num]
}
var events = page.events;
ctx.clearRect(0, 0, canvas.width, canvas.height); // Background!
// Draw grid too!
for (var i = 0; i < events.length; i += 1) {
draw_event(events[i]);
}
}
function draw_event(event) {
if (typeof event == "undefined") {
event = pages[page_num].events.slice(-1)[0];
}
switch (event.type) {
case "move":
case "up":
ctx.lineTo(event.x, event.y);
ctx.stroke();
case "down":
ctx.beginPath();
ctx.moveTo(event.x, event.y);
break;
// Line
case "set-width":
ctx.lineWidth = event.width;
break;
case "set-cap":
ctx.lineCap = event.cap;
break;
case "set-join":
ctx.lineJoin = event.join;
break;
case "set-miter-limit":
ctx.miterLimit = event.limit;
break;
case "set-dash":
ctx.setLineDash(event.pattern);
ctx.lineDashOffset = event.offset;
break;
// Text
case "set-font":
ctx.font = event.font;
break;
case "set-align":
ctx.textAlign = event.align;
break;
case "set-baseline":
ctx.textBaseline = event.baseline;
break;
case "set-direction":
ctx.direction = event.direction;
break;
// Fill and stroke
case "set-fill":
ctx.fillStyle = event.fill;
break;
case "set-stroke":
ctx.strokeStyle = event.stroke;
break;
// Shadows
case "set-shadow-blur":
ctx.shadowBlur = event.blur;
break;
case "set-shadow-colour":
ctx.shadowColor = event.colour;
break;
case "set-shadow-offset":
ctx.shadowOffsetX = event.x;
ctx.shadowOffsetY = event.y;
break;
// Compositing
case "set-alpha":
ctx.globalAlpha = event.alpha;
break;
case "set-composite":
ctx.globalCompositeOperation = event.mode;
break;
default:
alert("This file was made in a never version of the program;\nI don't understand what\n"
+ JSON.stringify(event) + "\nmeans.");
}
}
</script>
</body>
</html>