-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalProject.html
479 lines (413 loc) · 14.4 KB
/
FinalProject.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Victor`s Project</title>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
#container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin-top: 20px;
text-align: center;
}
canvas {
border: 2px solid #333;
background-color: #ffffff;
cursor: pointer;
margin-bottom: 20px;
}
button {
margin: 10px;
padding: 20px 16px;
font-size: 18px;
cursor: pointer;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
#slider {
width: 300px;
height: 20px;
margin: 10px 0;
background-color: #ddd;
border: none;
border-radius: 5px;
outline: none;
}
#slider::-webkit-slider-thumb {
width: 20px;
height: 18px;
background-color: #164a9d;
border: none;
border-radius: 50%;
cursor: pointer;
margin-top: -5px;
}
#slider::-moz-range-thumb {
width: 20px;
height: 20px;
background-color: #164ba1;
border: none;
border-radius: 50%;
cursor: pointer;
}
#valueOfSlideBar {
font-size: 16px;
color: #000;
}
#instructions {
color: rgb(0, 0, 0);
margin-top: 20px;
}
</style>
</head>
<body>
<div id="container">
<canvas id="bezierCanvas" width="800" height="500"></canvas>
<div style="display: flex; align-items: center; margin-top: 10px;">
<label for="slider" id="minValue" style="flex-grow: 1; text-align: right; margin-right: 10px;">Min Value: 0</label>
<label for="slider" id="currentValue" style="flex-grow: 1; text-align: center;">Current Value: 0.5</label>
<label for="slider" id="maxValue" style="flex-grow: 1; text-align: left; margin-left: 10px;">Max Value: 1</label>
</div>
<input type="range" id="slider" min="0" max="1" step="0.01" value="0.5" oninput="updateSlider()">
<div style="display: flex; justify-content: space-around; margin-top: 10px;">
<button onclick="removeAll()">Remove All</button>
</div>
<div style="display: flex; align-items: center; margin-top: 10px;">
<label for="showBezier" style="color: #000; font-weight: margin-right: 5px;">Visualize Bezier Curve:</label>
<input type="checkbox" id="showBezier" onchange="toggleBezierVisibility()" style="width: 17px; height: 17px;">
</div>
<div id="instructions" style="margin-top: 20px; text-align: center;">
<p><b>User guide:</b></p>
<ul>
<li><b>You can add a control point for the bezier curve by clicking the left mouse button.</b></li>
<li><b>By moving the slider to the left and right, you can visualize how the subdivision changes based on its value.</b></li>
<li><b>You can hold the mouse with the left button on the last added point to drag and change its current position.</b></li>
<li><b>Note that you can change the position of the last added point only!</b></li>
<li><b>You can delete the last added point by clicking with the right mouse button.</b></li>
<li><b>You can delete all the points by clicking the button with the label "Remove All".</b></li>
</ul>
</div>
</div>
<script>
let arrayOfPoints = [];
let valueOfSlideBar = 0.5;
let savingID;
let flagDragingAcceptable = false;
let draggedPoint = null;
let showBezier = false; // Added variable for bezier visibility
const canvas = document.getElementById('bezierCanvas');
const context = canvas.getContext('2d');
/**
* Function to update the slider value
*/
function updateSlider() {
valueOfSlideBar = parseFloat(document.getElementById('slider').value);
cancelAnimationFrame(savingID);
saveAllChanges();
}
/**
* Function to initialize control points
*/
function initializeArrayOfPoints() {
// No initial points
arrayOfPoints = [];
}
/**
* Function to perform de Casteljau's algorithm for Bézier curve
* @param {Array} points - Array of control points
* @param {number} t - Parameter value for subdivision
* @returns {Array} - Subdivided points
*/
function calculateSubdivide(points, t) {
const subdividedPoints = [];
for (let i = 1; i < points.length; i++) {
const prevPoint = points[i - 1];
const currentPoint = points[i];
const interpolatedPoint = linearInterpolation(prevPoint, currentPoint, t);
subdividedPoints.push(interpolatedPoint);
}
return subdividedPoints;
}
/**
* Linear interpolation function
* @param {Object} p0 - First point
* @param {Object} p1 - Second point
* @param {number} t - Interpolation parameter
* @returns {Object} - Interpolated point
*/
function linearInterpolation(p0, p1, t) {
return {
x: p0.x + (p1.x - p0.x) * t,
y: p0.y + (p1.y - p0.y) * t,
};
}
/**
* Function to draw a Bézier curve on the canvas
* @param {CanvasRenderingContext2D} context - Canvas rendering context
* @param {Array} arrayOfPoints - Array of control points
*/
function drawBezier(context, arrayOfPoints) {
if (showBezier) {
const n = arrayOfPoints.length - 1;
/**
* Function to calculate the de Casteljau point
* @param {number} t - Parameter value for subdivision
* @param {Array} points - Array of control points
* @returns {Object} - De Casteljau point
*/
function deCasteljauAlgorithm(t, points) {
const tempPoints = [...points];
for (let r = 1; r <= n; r++) {
for (let i = 0; i <= n - r; i++) {
tempPoints[i] = {
x: (1 - t) * tempPoints[i].x + t * tempPoints[i + 1].x,
y: (1 - t) * tempPoints[i].y + t * tempPoints[i + 1].y
};
}
}
return tempPoints[0];
}
// Draw the control points
context.fillStyle = "green";
for (const point of arrayOfPoints) {
context.beginPath();
context.arc(point.x, point.y, 5, 0, 2 * Math.PI);
context.fill();
}
// Draw the Bezier curve
context.beginPath();
context.strokeStyle = "black";
context.moveTo(arrayOfPoints[0].x, arrayOfPoints[0].y);
const step = 0.01;
for (let t = step; t <= 1; t += step) {
const curvePoint = deCasteljauAlgorithm(t, arrayOfPoints);
context.lineTo(curvePoint.x, curvePoint.y);
}
context.stroke();
}
}
/**
* Function to draw control points
* @param {CanvasRenderingContext2D} context - Canvas rendering context
* @param {Array} points - Array of control points
* @param {string} color - Color of control points
*/
function drawArrayOfPoints(context, points, color) {
context.fillStyle = color;
points.forEach(point => {
context.beginPath();
context.arc(point.x, point.y, 5, 0, 2 * Math.PI);
context.fill();
});
}
/**
* Function to toggle the visibility of the Bezier curve
*/
function toggleBezierVisibility() {
showBezier = !showBezier; // Toggle the value
saveAllChanges(); // Redraw the canvas with updated visibility
}
/**
* Function to draw lines connecting points
* @param {CanvasRenderingContext2D} context - Canvas rendering context
* @param {Array} points - Array of points
* @param {string} color - Color of lines
*/
function drawLines(context, points, color) {
context.strokeStyle = color;
context.beginPath();
context.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
context.lineTo(points[i].x, points[i].y);
}
context.stroke();
}
/**
* Function to handle mouse down event on the canvas
* @param {MouseEvent} event - Mouse event
*/
function handleMouseDown(event) {
const mouseX = event.clientX - canvas.getBoundingClientRect().left;
const mouseY = event.clientY - canvas.getBoundingClientRect().top;
// Check if the mouse is over a control point
for (const point of arrayOfPoints) {
const distance = Math.sqrt((mouseX - point.x) ** 2 + (mouseY - point.y) ** 2);
if (distance <= 5) {
flagDragingAcceptable = true;
draggedPoint = point;
break;
}
}
}
/**
* Function to handle mouse move event on the canvas
* @param {MouseEvent} event - Mouse event
*/
function handleMouseMove(event) {
if (flagDragingAcceptable && draggedPoint) {
draggedPoint.x = event.clientX - canvas.getBoundingClientRect().left;
draggedPoint.y = event.clientY - canvas.getBoundingClientRect().top;
cancelAnimationFrame(savingID);
saveAllChanges();
}
}
/**
* Function to handle mouse up event on the canvas
*/
function handleMouseUp() {
if (flagDragingAcceptable) {
flagDragingAcceptable = false;
draggedPoint = null;
cancelAnimationFrame(savingID);
context.clearRect(0, 0, canvas.width, canvas.height);
drawBezier(context, arrayOfPoints);
removeLastPoint();
saveAllChanges();
}
}
/**
* Function to handle mouse click event on the canvas
* @param {MouseEvent} event - Mouse event
*/
function handleCanvasClick(event) {
const mouseX = event.clientX - canvas.getBoundingClientRect().left;
const mouseY = event.clientY - canvas.getBoundingClientRect().top;
// Add a new control point
arrayOfPoints.push({ x: mouseX, y: mouseY });
drawBezier(context, arrayOfPoints);
// Cancel animation and redraw
cancelAnimationFrame(savingID);
saveAllChanges();
}
/**
* Function to delete the last point
*/
function removeLastPoint() {
if (arrayOfPoints.length > 0) {
arrayOfPoints.pop(); // Remove the last point
cancelAnimationFrame(savingID);
context.clearRect(0, 0, canvas.width, canvas.height);
drawBezier(context, arrayOfPoints);
saveAllChanges();
}
}
/**
* Function to handle right-click event on the canvas
* @param {MouseEvent} event - Mouse event
*/
function handleCanvasRightClick(event) {
event.preventDefault(); // Prevent default right-click behavior
// Check if there are points to delete
if (arrayOfPoints.length > 0) {
arrayOfPoints.pop(); // Remove the last point
cancelAnimationFrame(savingID);
context.clearRect(0, 0, canvas.width, canvas.height);
drawBezier(context, arrayOfPoints);
saveAllChanges();
}
}
// Add event listener for right-click on canvas
canvas.addEventListener('contextmenu', handleCanvasRightClick);
/**
* Function to save the changes
*/
function saveAllChanges() {
// Draw control points and lines
context.clearRect(0, 0, canvas.width, canvas.height);
drawArrayOfPoints(context, arrayOfPoints, 'green');
drawLines(context, arrayOfPoints, 'green');
if (arrayOfPoints.length > 1) {
// Subdivide the curve and draw the subdivided curves
const t = valueOfSlideBar;
let currentPoints = [...arrayOfPoints];
while (currentPoints.length > 1) {
currentPoints = calculateSubdivide(currentPoints, t);
drawBezier(context, arrayOfPoints, 'green');
drawArrayOfPoints(context, currentPoints, 'orange');
drawLines(context, currentPoints, 'gray');
}
drawArrayOfPoints(context, currentPoints, 'yellow');
}
savingID = requestAnimationFrame(saveAllChanges);
}
// Add event listeners for canvas interactions
canvas.addEventListener('mousedown', handleMouseDown);
canvas.addEventListener('mousemove', handleMouseMove);
canvas.addEventListener('mouseup', handleMouseUp);
canvas.addEventListener('click', handleCanvasClick);
// Initialize control points and run the example when the page is loaded
initializeArrayOfPoints();
drawBezier(context, arrayOfPoints);
saveAllChanges();
/**
* Function to reset the canvas by removing all points
*/
function removeAll() {
arrayOfPoints = [];
cancelAnimationFrame(savingID);
context.clearRect(0, 0, canvas.width, canvas.height);
}
/**
* Function to update the instructions text
*/
function updateInstructions() {
const instructionsDiv = document.getElementById('instructions');
instructionsDiv.innerHTML = `
<p><b>User guide:</b></p>
<ul>
<li><b>You can add a control point for the bezier curve by clicking the left mouse button.</b></li>
<li><b>By moving the slider to the left and right, you can visualize how the subdivision changes based on its value.</b></li>
<li><b>You can hold the mouse with the left button on the last added point to drag and change its current position.</b></li>
<li><b>Note that you can change the position of the last added point only!</b></li>
<li><b>You can delete the last added point by clicking with the right mouse button.</b></li>
<li><b>You can delete all the points by clicking the button with the label "Remove All".</b></li>
</ul>
`;
}
// Call the function to initially set the instructions
updateInstructions();
/**
* Function to update the labels of the slider
*/
function updateSliderLabels() {
const slider = document.getElementById('slider');
const minValueLabel = document.getElementById('minValue');
const currentValueLabel = document.getElementById('currentValue');
const maxValueLabel = document.getElementById('maxValue');
minValueLabel.textContent = `Min Value: ${slider.min}`;
currentValueLabel.textContent = `Current Value: ${slider.value}`;
maxValueLabel.textContent = `Max Value: ${slider.max}`;
}
/**
* Function to update the slider value and trigger animation
*/
function updateSlider() {
const slider = document.getElementById('slider');
valueOfSlideBar = parseFloat(slider.value);
cancelAnimationFrame(savingID);
saveAllChanges();
updateSliderLabels();
}
// Call the function to initially set the slider value
updateSlider();
</script>
</body>
</html>