Spaces:
Running
Running
File size: 5,105 Bytes
854a551 |
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 |
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap"
rel="stylesheet" />
<link
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200&display=block"
rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.js"></script>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Multimodal Live - Console</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script>
let mySketch;
let osc; // oscillator for the tone
let freq = 261; // base frequency of the tone
let selectedCircleIndex = -1;
window.get_circles = function () {
return {circles: window.circles};
};
window.change_circle = function (args) {
// Play the tone here
if (osc) {
osc.start();
osc.freq(freq); // Adjust frequency as needed
osc.amp(0.3); // Set initial amplitude
osc.fade(0, 0.2); // Fade to 0 amplitude over 0.2 seconds
}
// go through the object and copy the properties into the new one
window.circlesCurrent = window.circles.map((c) => ({ ...c }))
const color = args.color;
const findIndex = window.circles.findIndex(
(c) => c.color.toLowerCase() === color.toLowerCase(),
);
console.log(window.circles.splice);
window.circles.splice(findIndex, 1, args);
};
window.circles = [
{color: "#FF0000", x: window.innerWidth/2 - 150, y: window.innerHeight/2, radius: 100},
{color: "#00FF00", x: window.innerWidth/2, y: window.innerHeight/2, radius: 100},
{color: "#0000FF", x: window.innerWidth/2 + 150, y: window.innerHeight/2, radius: 100},
];
// make a copy of it
window.circlesCurrent = window.circles.map((c) => ({ ...c }))
window.initSketch = function (container) {
console.log("initialize sketch in public/index.html");
console.log(container);
if (mySketch) {
return;
}
mySketch = new p5((p) => {
p.setup = function () {
console.log(p);
p.createCanvas(window.innerWidth, window.innerHeight);
container.innerHTMl = "";
container.appendChild(p._renderer.canvas);
// Create the oscillator here, after p5.sound has been initialized
osc = new p5.Oscillator('sine');
osc.amp(0); // Start with zero amplitude
};
function getSelectedCircleIndex() {
for (let i = 0; i < circles.length; i++) {
const circle = circles[i];
if (
p.mouseX > circle.x - circle.radius &&
p.mouseX < circle.x + circle.radius &&
p.mouseY > circle.y - circle.radius &&
p.mouseY < circle.y + circle.radius
) {
console.log("clicked : " + i);
return i;
}
}
return -1;
}
p.mousePressed = function () {
selectedCircleIndex = getSelectedCircleIndex();
};
p.mouseDragged = function () {
if (selectedCircleIndex > -1) {
const circle = circles[selectedCircleIndex];
circle.x = p.mouseX;
circle.y = p.mouseY;
}
};
p.mouseReleased = function () {
selectedCircleIndex = -1;
};
p.draw = function draw() {
p.background(0);
p.blendMode(p.BLEND);
for(let i = 0; i < window.circles.length; i++) {
const circle = window.circles[i];
p.noStroke();
p.blendMode(p.SCREEN);
p.fill(circle.color);
// ease towards the final position
const easing = 0.15;
circlesCurrent[i].x = circlesCurrent[i].x + (circle.x - circlesCurrent[i].x) * easing;
circlesCurrent[i].y = circlesCurrent[i].y + (circle.y - circlesCurrent[i].y) * easing;
circlesCurrent[i].radius = circlesCurrent[i].radius + (circle.radius - circlesCurrent[i].radius) * easing;
// draw them at that position
p.circle(circlesCurrent[i].x, circlesCurrent[i].y, circlesCurrent[i].radius * 2);
}
};
});
};
</script>
</body>
</html>
|