-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
173 lines (160 loc) · 6.26 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Draggable Points</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<form id="mirror_params" method="post">
<div>
<label for="text">Mirror Text:</label>
<input name="text" id="text" value="Hello" />
</div>
<div>
<label for="font">Font</label>
<select id="font" name="font">
<option value="HersheySans1">HersheySans1</option>
<option value="HersheySansMed">HersheySansMed</option>
<option value="HersheyScript1">HersheyScript1</option>
<option value="HersheyScriptMed">HersheyScriptMed</option>
<option value="HersheySerifBold">HersheySerifBold</option>
<option value="HersheySerifBoldItalic">HersheySerifBoldItalic</option>
<option value="HersheySerifMed">HersheySerifMed</option>
<option value="HersheySerifMedItalic">HersheySerifMedItalic</option>
<option value="CutlingsGeometric">CutlingsGeometric</option>
<option value="EMSAllure">EMSAllure</option>
<option value="EMSBird">EMSBird</option>
<option value="EMSBirdSwashCaps">EMSBirdSwashCaps</option>
<option value="EMSBrush">EMSBrush</option>
<option value="EMSCapitol">EMSCapitol</option>
<option value="EMSCasualHand">EMSCasualHand</option>
<option value="EMSDecorousScript">EMSDecorousScript</option>
<option value="EMSDelight">EMSDelight</option>
<option value="EMSDelightSwashCaps">EMSDelightSwashCaps</option>
<option value="EMSElfin">EMSElfin</option>
<option value="EMSFelix">EMSFelix</option>
<option value="EMSHerculean">EMSHerculean</option>
<option value="EMSInvite">EMSInvite</option>
<option value="EMSLeague">EMSLeague</option>
<option value="EMSLittlePrincess">EMSLittlePrincess</option>
<option value="EMSMistyNight">EMSMistyNight</option>
<option value="EMSNeato">EMSNeato</option>
<option value="EMSNixish">EMSNixish</option>
<option value="EMSNixishItalic">EMSNixishItalic</option>
<option value="EMSOsmotron">EMSOsmotron</option>
<option value="EMSPancakes">EMSPancakes</option>
<option value="EMSPepita">EMSPepita</option>
<option value="EMSQwandry">EMSQwandry</option>
<option selected value="EMSReadability">EMSReadability</option>
<option value="EMSReadabilityItalic">EMSReadabilityItalic</option>
<option value="EMSSociety">EMSSociety</option>
<option value="EMSSpaceRocks">EMSSpaceRocks</option>
<option value="EMSSwiss">EMSSwiss</option>
<option value="EMSTech">EMSTech</option>
<option value="HersheyGothEnglish">HersheyGothEnglish</option>
<option value="HersheyGothGerman">HersheyGothGerman</option>
<option value="HersheyGothItalian">HersheyGothItalian</option>
</select>
</div>
<div>
<label for="hex">Number of Mirrors</label>
<select id="hex" name="hex">
<option value="37">37</option>
<option value="61">61</option>
<option value="91">91</option>
<option selected value="127">127</option>
<option value="169">169</option>
</select>
</div>
<div>
<button>Submit</button>
</div>
</form>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
// Define an array of points
let points = []
document.forms['mirror_params'].addEventListener('submit', (event) => {
event.preventDefault();
// TODO do something here to show user that form is being submitted
fetch(event.target.action, {
method: 'POST',
body: new URLSearchParams(new FormData(event.target)) // event.target is the form
}).then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // or response.text() or whatever the server sends
}).then((body) => {
points.length = 0
body.forEach(point => {
points.push({x:point[0], y:point[1]})
});
drawPoints()
}).catch((error) => {
console.log(error)
});
});
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let scaling = 2
ctx.scale(scaling, scaling)
function convertMouseCoords(event) {
const rect = canvas.getBoundingClientRect();
const x = (event.clientX - rect.left) * scaling;
const y = (event.clientY - rect.top) * scaling;
return { x, y };
}
// Draw the initial points on the canvas
function drawPoints() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'blue';
for (let i = 0; i < points.length; i++) {
ctx.beginPath();
ctx.arc(points[i].x, points[i].y, 5, 0, Math.PI * 2);
ctx.fill();
}
}
drawPoints();
// Define a function to check if the mouse is hovering over a point
function isMouseOverPoint(x, y, point) {
const distance = Math.sqrt((x - point.x) ** 2 + (y - point.y) ** 2);
return distance <= 5;
}
// Define a variable to keep track of which point is being dragged
let selectedPoint = null;
// Add an event listener to the canvas to detect when the mouse is pressed down
canvas.addEventListener('mousedown', (event) => {
// const x = event.offsetX;
// const y = event.offsetY;
const { x, y } = convertMouseCoords(event);
// Check if the mouse is hovering over any of the points
for (let i = 0; i < points.length; i++) {
if (isMouseOverPoint(x, y, points[i])) {
console.log("yes!")
selectedPoint = points[i];
break;
}
console.log("no :(")
}
});
// Add an event listener to the canvas to detect when the mouse is released
canvas.addEventListener('mouseup', (event) => {
selectedPoint = null;
});
// Add an event listener to the canvas to detect when the mouse is moved
canvas.addEventListener('mousemove', (event) => {
if (selectedPoint) {
selectedPoint.x = event.offsetX;
selectedPoint.y = event.offsetY;
drawPoints();
}
});
</script>
</body>
</html>