-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
80 lines (68 loc) · 2.52 KB
/
sketch.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
let currentSketch;
loadSketch('neckTracking.js');
document.addEventListener('DOMContentLoaded', (event) => {
document.getElementById('fingerButton').addEventListener('click', function() {
loadSketch('handTracking.js');
});
document.getElementById('noseButton').addEventListener('click', function() {
loadSketch('noseTracking.js');
});
document.getElementById('minuteNeckButton').addEventListener('click', function() {
});
document.getElementById('moveAroundButton').addEventListener('click', function() {
loadSketch('moveAround.js');
});
document.getElementById('saveArtButton').addEventListener('click', function() {
// saveArt();
saveCanvas('neck-tracking-path', 'png');
clearCanvas();
});
});
function loadSketch(sketchName) {
// Remove existing canvas if there is one
const frame2 = document.getElementById('canvasContainer');
const existingCanvas = frame2.querySelector('canvas');
if (existingCanvas) {
existingCanvas.remove();
}
// Remove the previous sketch script if present
if (currentSketch) {
const oldScript = document.querySelector(`script[src="${currentSketch}"]`);
if (oldScript) {
oldScript.remove();
}
}
// Dynamically load the new sketch.js file
const script = document.createElement('script');
script.src = sketchName;
document.body.appendChild(script);
// Set the current sketch to the newly loaded one
currentSketch = sketchName;
// Append the canvas to Frame2 after the script has loaded
script.onload = function() {
// const canvas = document.querySelector('canvas');
// if (canvas) {
// canvas.parentElement.removeChild(canvas); // Remove old canvas if exists
document.getElementById('canvasContainer').appendChild(canvas); // Append the new canvas
// }
};
}
function saveArt() {
const frame2 = document.querySelector('.Frame2');
const canvas = frame2.querySelector('canvas');
if (canvas) {
// Use p5.js function to save the canvas as an image
saveCanvas(canvas, 'artwork', 'png');
// Remove the canvas after saving
canvas.remove();
// Optionally, reset button states if needed
resetButtons();
}
}
function resetButtons() {
// Example: Enable all buttons if they were disabled or reset their states
const buttons = document.querySelectorAll('.Button');
buttons.forEach(button => {
button.disabled = false;
});
}