-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
634 lines (593 loc) · 23.1 KB
/
script.js
File metadata and controls
634 lines (593 loc) · 23.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* Image sources
* {folderName (in assets folder): [fileNames or fileIndexes],...}
*/
let sources = {
players: [...Array(84).keys()],
pitches: [...Array(16).keys()],
tools: ['draw', 'text', 'line', 'dashedLine', 'arrow', 'dashedArrow', 'squigglyArrow', 'curvedArrow', 'dashedCurvedArrow', 'circle', 'move', 'delete', 'download', 'downloadJSON', 'uploadJSON'],
equipments: [...Array(41).keys()],
};
/**
* Loads images
*
* Edited version of https://www.html5canvastutorials.com/tutorials/html5-canvas-image-loader/
* Loads images in a manner that it stores loaded images in a similar structure as the sources
* Don't touch unless you know what you're doing!
*/
function loadImages(sources, callback) {
let images = {};
let loadedImages = 0;
let numImages = 0;
for (let type in sources) {
numImages += sources[type].length;
}
for (let type in sources) {
images[type] = [];
for (let src of sources[type]) {
images[type][src] = new Image();
images[type][src].crossOrigin = 'anonymous';
images[type][src].onload = function () {
if (++loadedImages >= numImages) {
callback(images);
}
};
images[type][src].src = `assets/${type}/${src}.png`;
}
}
};
/**
* Main script
*/
loadImages(sources, function (images) {
/**
* Downloads data URI as a file
*
* @param {*} uri URI
* @param {String} name File name
*/
function downloadURI(uri, name) {
let link = document.createElement('a');
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
/**
* Adds notes to final image
*/
function addNotes() {
if (document.getElementById('notesOption').checked) {
stage.height(700);
pitchLayer.add(
new Konva.Rect({
x: 0,
y: height,
width: width,
height: 200,
fill: 'white',
}),
new Konva.Text({
x: 5,
y: height + 5,
width: width - 10,
height: 190,
text: document.getElementById('notesInput').value,
fontSize: 15,
fontFamily: 'Open Sans',
})
);
}
}
/**
* Downloads the stage as an image
*/
async function downloadImage() {
// (!) Experimental use of the Files System Access API
// If the browser supports the API
if (window.showSaveFilePicker) {
const handle = await showSaveFilePicker({
types: [
{
description: 'PNG File',
accept: {
'image/png': ['.png'],
},
},
],
});
await addNotes();
let image = await new Promise((res) => stage.toCanvas().toBlob(res));
const writable = await handle.createWritable();
await writable.write(image);
writable.close();
}
// If the browser does not support the API
else {
await addNotes();
const fileName = prompt('File Name:');
// Do nothing if user cancels prompt
if (fileName == null)
return;
await downloadURI(stage.toDataURL({ pixelRatio: 3 }), `${fileName}.png`);
}
// Reset height of stage
await stage.height(520);
}
/**
* Downloads the stage as JSON
*/
async function downloadJSON() {
// (!) Experimental use of the Files System Access API
// If the browser supports the API
if (window.showSaveFilePicker) {
const handle = await showSaveFilePicker({
types: [
{
description: 'HNKL File',
accept: {
'text/json': ['.hnkl'],
},
},
],
});
const JSON = stage.toJSON();
const writable = await handle.createWritable();
await writable.write(JSON);
writable.close();
}
// If the browser does not support the API
else {
const fileName = prompt('File Name:');
// Do nothing if user cancels prompt
if (fileName == null)
return;
await downloadURI('data:text/json;charset=utf-8,' + encodeURIComponent(stage.toJSON()), `${fileName}.hnkl`);
}
}
/**
* Generates points for the squiggly arrow
*
* @param {Number} x1 Start x position of the squiggly arrow
* @param {Number} y1 Start y position of the squiggly arrow
* @param {Number} x2 End x position of the squiggly arrow
* @param {Number} y2 End y position of the squiggly arrow
* @returns {Array} Array of points of the squiggly arrow
*/
function generateSquigglyArrowPoints(x1, y1, x2, y2) {
const points = [];
const amplitude = 2;
const wavelength = 13;
const dx = x2 - x1;
const dy = y2 - y1;
const dist = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
const numPoints = Math.floor(dist / wavelength * 2 * Math.PI);
for (let i = 0; i <= numPoints - 8; i++) {
const t = i / numPoints;
const x = x1 + t * dx + amplitude * Math.sin(t * 2 * Math.PI * dist / wavelength) * Math.cos(angle + Math.PI / 2);
const y = y1 + t * dy + amplitude * Math.sin(t * 2 * Math.PI * dist / wavelength) * Math.sin(angle + Math.PI / 2);
points.push(x, y);
}
// For a stable arrow head
points.push(x2 - dx / dist, y2 - dy / dist);
points.push(x2, y2);
return points;
}
/**
* Adds interactions with the stage
*/
function addInteractions() {
// Reset variables
currentMode = 'none';
currentCursor = 'default';
stage.container().style.cursor = currentCursor;
// Graphics
stage.on('click tap', function (e) {
if (currentMode != 'none')
changed = true;
let pos = stage.getRelativePointerPosition();
if (currentMode == 'players') {
addImage('players', selectedImage, pos.x, pos.y);
} else if (currentMode == 'text') {
addText(prompt('Add text:'), pos.x, pos.y);
} else if (currentMode == 'equipments') {
addImage('equipments', selectedImage, pos.x, pos.y);
} else if (currentMode == 'delete') {
// Prevent deletion of the pitch
if (e.target.attrs.width == width)
return;
e.target.destroy();
stage.container().style.cursor = currentCursor;
}
});
// Markup
stage.on('mousedown touchstart', function () {
if (currentMode != 'none')
changed = true;
const pos = stage.getPointerPosition();
originalPosition = {
x: pos.x,
y: pos.y,
};
markup = true;
switch (currentMode) {
case 'draw':
case 'line':
case 'dashedLine':
line = new Konva.Line({
stroke: document.getElementById('colorPicker').value,
strokeWidth: 2,
globalCompositeOperation: 'source-over',
lineCap: 'round',
lineJoin: 'round',
points: [pos.x, pos.y, pos.x, pos.y],
dash: currentMode == 'dashedLine' ? [15, 10] : [],
});
break;
case 'arrow':
case 'dashedArrow':
case 'squigglyArrow':
line = new Konva.Arrow({
stroke: document.getElementById('colorPicker').value,
strokeWidth: 2,
fill: document.getElementById('colorPicker').value,
globalCompositeOperation: 'source-over',
lineCap: 'round',
lineJoin: 'round',
dash: currentMode == 'dashedArrow' ? [15, 10] : [],
});
break;
case 'curvedArrow':
case 'dashedCurvedArrow':
line = new Konva.Arrow({
stroke: document.getElementById('colorPicker').value,
strokeWidth: 2,
fill: document.getElementById('colorPicker').value,
globalCompositeOperation: 'source-over',
lineCap: 'round',
lineJoin: 'round',
bezier: true,
dash: currentMode == 'dashedCurvedArrow' ? [15, 10] : [],
});
break;
case 'circle':
line = new Konva.Ellipse({
x: pos.x,
y: pos.y,
stroke: document.getElementById('colorPicker').value,
strokeWidth: 2,
globalCompositeOperation: 'source-over',
});
break;
case 'delete':
deleting = true;
return;
default:
return;
}
markupLayer.add(line);
});
stage.on('mouseup touchend', function () {
markup = false;
deleting = false;
});
stage.on('mousemove touchmove', function (e) {
const pos = stage.getPointerPosition();
e.evt.preventDefault();
if (!markup) {
return;
}
switch (currentMode) {
case 'draw':
let newPoints = line.points().concat([pos.x, pos.y]);
line.points(newPoints);
break;
case 'line':
case 'dashedLine':
case 'arrow':
case 'dashedArrow':
line.points([originalPosition.x, originalPosition.y, pos.x, pos.y]);
break;
case 'curvedArrow':
case 'dashedCurvedArrow':
if (Math.abs(originalPosition.x - pos.x) > Math.abs(originalPosition.y - pos.y)) {
line.points([originalPosition.x, originalPosition.y, originalPosition.x, originalPosition.y, pos.x, originalPosition.y, pos.x, pos.y]);
if (e.evt.shiftKey) {
line.points([originalPosition.x, originalPosition.y, originalPosition.x, originalPosition.y, originalPosition.x, pos.y, pos.x, pos.y]);
}
} else {
line.points([originalPosition.x, originalPosition.y, originalPosition.x, originalPosition.y, originalPosition.x, pos.y, pos.x, pos.y]);
if (e.evt.shiftKey) {
line.points([originalPosition.x, originalPosition.y, originalPosition.x, originalPosition.y, pos.x, originalPosition.y, pos.x, pos.y]);
}
}
break;
case 'squigglyArrow':
let points = generateSquigglyArrowPoints(originalPosition.x, originalPosition.y, pos.x, pos.y);
line.points(points);
break;
case 'circle':
line.radius({ x: Math.abs(originalPosition.x - pos.x), y: Math.abs(originalPosition.y - pos.y) });
break;
case 'delete':
if (deleting) {
if (e.target.attrs.width == width)
return;
e.target.destroy();
stage.container().style.cursor = currentCursor;
}
break;
default:
return;
}
});
// Change cursor when interacting with objects
graphicsLayer.on('mouseenter', function () {
if (currentMode == 'players' || currentMode == 'equipments' || currentMode == 'text' || currentMode == 'move') {
stage.container().style.cursor = 'move';
}
else if (currentMode == 'delete') {
stage.container().style.cursor = 'crosshair';
}
});
graphicsLayer.on('mouseleave', function () {
stage.container().style.cursor = currentCursor;
});
markupLayer.on('mouseenter', function () {
if (currentMode == 'delete') {
stage.container().style.cursor = 'crosshair';
}
});
markupLayer.on('mouseleave', function () {
stage.container().style.cursor = currentCursor;
});
}
/**
* Changes `currentMode` and the cursor to the affiliated icon
*
* @param {String} mode The mode (players, tools, or equipments) that `currentMode` and the cursor is changing to
* @param {String} tool (optional) If `mode` is 'tools', the specific tool (draw, text, line...) that `currentMode` and the cursor is changing to
*/
function changeMode(mode, tool = '') {
// Make graphics draggable depending on the mode/tool
for (i of graphicsLayer.children) {
i.draggable(mode == 'players' || mode == 'equipments' || tool == 'text' || tool == 'move');
}
// Download image, download JSON, or upload JSON depending on mode
if (tool == 'download') {
downloadImage();
return;
} else if (tool == 'downloadJSON') {
stage.setAttr('notes', document.getElementById('notesInput').value);
downloadJSON();
return;
} else if (tool == 'uploadJSON') {
// Reset variables
currentMode = 'none';
currentCursor = 'default';
stage.container().style.cursor = currentCursor;
const uploadFile = document.getElementById('uploadFile');
uploadFile.click();
return;
}
// Change the cursor and the current mode
currentCursor = tool == '' ? `url('assets_low/${mode}/${selectedImage}.png') ${images[mode][selectedImage].width * 0.3 / 2} ${images[mode][selectedImage].height * 0.3 / 2}, auto` : 'default';
stage.container().style.cursor = currentCursor;
currentMode = tool == '' ? mode : tool;
}
/**
* Loads the menu by adding icons and event listeners
*/
function loadMenu() {
// Create player elements
for (let i of sources['players']) {
if (i <= 31) {
document.getElementById('team1').innerHTML += `<img class="player" id="player${i}" src="assets/players/${i}.png" draggable="false"></img>`;
} else if (i > 31 && i <= 63) {
document.getElementById('team2').innerHTML += `<img class="player" id="player${i}" src="assets/players/${i}.png" draggable="false"></img>`;
} else {
document.getElementById('gk').innerHTML += `<img class="player" id="player${i}" src="assets/players/${i}.png" draggable="false"></img>`;
}
}
// Add event listeners
for (let i of sources['players']) {
document.getElementById(`player${i}`).addEventListener('click', function () {
for (let j of sources['tools']) {
document.getElementById(j).dataset.selected = false;
}
selectedImage = i;
changeMode('players');
});
}
// Create tool elements
for (let i of sources['tools']) {
document.getElementById('tools').innerHTML += `<img class="tool" id="${i}" src="assets/tools/${i}.png" data-selected="false" draggable="false"></img>`;
}
for (let i of sources['tools']) {
let element = document.getElementById(i);
element.addEventListener('click', function () {
for (let j of sources['tools']) {
document.getElementById(j).dataset.selected = false;
}
element.dataset.selected = true;
selectedImage = i;
changeMode('tools', i);
});
}
// Create equipment elements
for (let i of sources['equipments']) {
document.getElementById('equipments').innerHTML += `<img class="equipment${images['equipments'][i].width < 15 / 0.3 || images['equipments'][i].height < 14 / 0.3 ? 'Small' : 'Large'}" id="equipment${i}" src="assets_low/equipments/${i}.png" draggable="false"></img>`;
if (i == 6 || i == 15 || i == 22 || i == 28 || i == 32 || i == 36) {
document.getElementById('equipments').innerHTML += '<br>';
}
}
// Add event listeners
for (let i of sources['equipments']) {
document.getElementById(`equipment${i}`).addEventListener('click', function () {
for (let j of sources['tools']) {
document.getElementById(j).dataset.selected = false;
}
selectedImage = i;
changeMode('equipments');
});
}
}
/**
* Loads the pitches menu by adding pitches and event listeners
*/
function loadPitches() {
// Create pitches elements
for (let i of sources['pitches']) {
document.getElementById('pitches').innerHTML += `<img class="pitch" id="pitch${i}" src="assets/pitches/${i}.png" draggable="false"></img>`;
}
// Add event listeners
for (let i of sources['pitches']) {
document.getElementById(`pitch${i}`).addEventListener('click', function () {
changed = true;
pitchLayer.destroyChildren();
let pitchImage = new Konva.Image({
x: 0,
y: 0,
image: images['pitches'][i],
width: width,
height: height,
});
pitchImage.setAttr('imageType', 'pitches');
pitchImage.setAttr('index', i);
pitchLayer.add(pitchImage);
});
}
}
/**
* Adds image to `graphicsLayer`
*
* @param {String} type The image type (players, pitches, or equipments)
* @param {Number} index The index of the image in the assets folder
* @param {Number} x The x position of the image being added
* @param {Number} y The y position of the image being added
*/
function addImage(type, index, x, y) {
image = images[type][index];
imageToAdd = new Konva.Image({
width: image.width * 0.3,
height: image.height * 0.3,
x: x - image.width * 0.3 / 2,
y: y - image.height * 0.3 / 2,
image: image,
draggable: true,
});
imageToAdd.setAttr('imageType', type);
imageToAdd.setAttr('index', index);
graphicsLayer.add(imageToAdd);
}
/**
* Adds text to `graphicsLayer`
* @param {String} text The text to add
* @param {*} x The x position of the text being added
* @param {*} y The y position of the text being added
*/
function addText(text, x, y) {
// Do nothing if no text is provided
if (text == null || text == '')
return;
graphicsLayer.add(
new Konva.Text({
x: x,
y: y,
text: text,
fontSize: 20,
fontFamily: 'Open Sans',
fill: document.getElementById('colorPicker').value,
draggable: true,
})
);
}
/**
* Main code
*/
// Set up variables
let selectedImage;
let currentMode = 'none';
let currentCursor = 'default';
let markup = false;
let markupLayer = new Konva.Layer();
let graphicsLayer = new Konva.Layer();
let pitchLayer = new Konva.Layer();
let deleting = false;
let changed = false;
const width = 675;
const height = 520;
// Load the menus
loadMenu();
loadPitches();
// Create the main stage
let stage = new Konva.Stage({
container: 'planner',
width: width,
height: height,
});
// Add pitch image to pitch layer
let pitchImage = new Konva.Image({
x: 0,
y: 0,
image: images['pitches'][0],
width: width,
height: height,
});
pitchImage.setAttr('imageType', 'pitches');
pitchImage.setAttr('index', 0);
pitchLayer.add(pitchImage);
// Add pitch, graphics, and markup layer to the stage
stage.add(pitchLayer);
stage.add(graphicsLayer);
stage.add(markupLayer);
// Enable stage interactions
addInteractions();
/**
* Enable notes in image once typed in
*/
document.getElementById('notesInput').addEventListener('input', function () {
changed = true;
document.getElementById('notesOption').checked = true;
});
/**
* Load stage from uploaded JSON file
*/
document.getElementById('uploadFile').addEventListener('change', function () {
if (uploadFile.files.length > 0) {
// Read the uploaded file as JSON
let reader = new FileReader();
reader.readAsText(uploadFile.files[0]);
reader.onload = function () {
let json = JSON.parse(reader.result);
// Load stage from the JSON
stage = Konva.Node.create(json, 'planner');
// Set image attributes
stage.find('Image').forEach((imageNode) => {
imageNode.image(images[imageNode.getAttr('imageType')][imageNode.getAttr('index')]);
});
// Set notes attributes from the JSON
document.getElementById('notesInput').value = stage.getAttr('notes');
document.getElementById('notesOption').checked = stage.getAttr('notes') != '';
// Set layers from the JSON
pitchLayer = stage.children[0];
if (stage.children[1] != undefined)
graphicsLayer = stage.children[1];
if (stage.children[2] != undefined)
markupLayer = stage.children[2];
// Reenable stage interations
addInteractions();
// Reset uploaded files
uploadFile.value = '';
};
}
});
// Asks for confirmation before closing / reloading to prevent loss of unsaved work
window.addEventListener('beforeunload', (event) => {
if (changed)
event.preventDefault();
});
});