-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·105 lines (81 loc) · 2.93 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
const TWO_PI = 2 * Math.PI;
const stringOpacity = "0.7";
const stringOpacityNoWrap = "0.2";
let currentColour;
let currentOpacity;
let board;
let requestDraw;
window.addEventListener("load", init);
window.addEventListener('beforeunload', (e) => {
//warn user on leaving page if the board has been drawn on
//https://stackoverflow.com/questions/3221161/how-to-pop-up-an-alert-box-when-the-browsers-refresh-button-is-clicked
if (board.stringChains.length > 0 && inpWarnOnPageLeave.checked)
{
e.preventDefault();
e.returnValue = '';
}
});
function init()
{
currentColour = "0, 0, 0";
currentOpacity = stringOpacity;
currentMousePos = new Vec2(0, 0);
prevMousePos = new Vec2(0, 0);
initUI();
board = new StringBoard(canvasRes / 2.5, DEFAULT_NUMPEGS, DEFAULT_PEGRADIUS);
board.updatePreRender(cnvMain, ctxMain);
requestDraw = true;
requestAnimationFrame(draw);
}
function draw()
{
if (requestDraw)
{
//draw board's pegs and strings
board.draw(ctxMain);
//draw current string to mouse
if (stringActive)
{
let csc = board.getCurrentStringChain();
let startPos = board.calculateCirclePointTangent(csc.getLastPegIndex(), csc.getLastPegIsClockwise(), currentMousePos);
ctxMain.beginPath();
if (csc.getLength() > 1)
{
let pegPos = board.getPegPos(csc.getLastPegIndex());
let dStart = csc.getLastPegWrapStart().sub(pegPos);
let aStart = Math.atan2(dStart.y, dStart.x);
let dEnd = startPos.sub(pegPos);
let aEnd = Math.atan2(dEnd.y, dEnd.x);
ctxMain.arc(pegPos.x, pegPos.y, board.pegRadius, aStart, aEnd, !csc.getLastPegIsClockwise());
}
ctxMain.moveTo(startPos.x, startPos.y);
ctxMain.lineTo(currentMousePos.x, currentMousePos.y);
if (editing) setStringStyle(csc.colour, (enableWrap ? stringOpacity : stringOpacityNoWrap));
else setStringStyle(currentColour, (enableWrap ? stringOpacity : stringOpacityNoWrap));
ctxMain.stroke();
}
requestDraw = false;
}
requestAnimationFrame(draw);
}
function setStringStyle(colour, opacity)
{
ctxMain.strokeStyle = "rgba(" + colour + ", " + opacity + ")";
}
function getLocalStorage(name)
{
let ls = localStorage.getItem("circleString");
if (ls === null) return null;
else ls = JSON.parse(ls);
let value = ls[name];
if (value === undefined) return null;
else return value;
}
function setLocalStorage(name, value)
{
let ls = localStorage.getItem("circleString");
if (ls === null) ls = {};
else ls = JSON.parse(ls);
ls[name] = value;
localStorage.setItem("circleString", JSON.stringify(ls));
}