let selectedItem = "";
let animationFrame = 0;
let impactFrame = 0;
let shakeAmount = 0;
let angle = 0;
let img;
let sound;
let currentMode = 'manual';
let soundPlayed = false; // Flag to check if sound has been played
function preload() {
// This can be used for loading assets, but we're doing it in the randomize() function instead
}
function setup() {
let canvas = createCanvas(400, 200);
canvas.parent('canvasContainer');
angleMode(DEGREES);
textAlign(CENTER, CENTER);
textSize(24);
setMode(currentMode); // Initialize input section when the page loads
}
// Play sound only once
if (impactFrame === 1 && !soundPlayed && sound) {
sound.play();
soundPlayed = true; // Set flag to prevent repeated sound
}
impactFrame++;
} else if (impactFrame >= 30 && img) {
// After spinning and shaking, display the "Eat this" message
document.getElementById('result').innerText = Eat this: ${selectedItem}!;
code
// Shake the text a bit before resetting the animation
let shakeTextAmount = 5;
let shakeX = random(-shakeTextAmount, shakeTextAmount);
let shakeY = random(-shakeTextAmount, shakeTextAmount);
// Position the text with shake effect
document.getElementById('result').style.transform = `translate(${shakeX}px, ${shakeY}px)`;
// Auto-reset after 2 seconds
if (frameCount % 120 === 0) {
resetAnimation();
}
}
}
function setMode(mode) {
currentMode = mode;
let inputSection = document.getElementById('inputSection');
if (mode === 'manual') {
inputSection.innerHTML = <p>Enter menu items (one per line):</p> <textarea id="menuInput" rows="5" placeholder="Pizza\nBurger\nSalad"></textarea> ;
} else {
inputSection.innerHTML = <p>Upload a menu image:</p> <input type="file" id="imageInput" accept="image/*" onchange="scanImage()"> <p>Extracted items:</p> <textarea id="menuInput" rows="5" placeholder="Items will appear here"></textarea> ;
}
}
async function scanImage() {
if (currentMode !== 'image') return;
const fileInput = document.getElementById('imageInput');
const resultDiv = document.getElementById('result');
soundPlayed = false; // Reset soundPlayed flag for next round
}
function resetAnimation() {
animationFrame = 0;
impactFrame = 0;
soundPlayed = false;
document.getElementById('result').style.transform = 'translate(0, 0)'; // Reset text shake position
}
the code, after presing randomise, it just hsakes ,does not spin (eaT this) and th esound plays on loop like its eben pressed play each 0.02 s
Assistant
Step-by-step analysis of the problem:
The spinning animation is not visible: The issue lies in the fact that the animationFrame is being reset too quickly, and the impactFrame is taking over immediately. We need to ensure that the spinning animation completes before the shaking animation starts.
The sound is playing on loop: The sound is being played repeatedly because the condition if (impactFrame === 1 && !soundPlayed && sound) is being met multiple times due to the impactFrame being incremented every frame. We need to ensure that the sound is played only once.
The "Eat this" message is not being displayed: The "Eat this" message is being displayed, but it's being reset quickly due to the resetAnimation() function being called after a certain number of frames.
Fixed solution:
javascript
let selectedItem = "";
let animationFrame = 0;
let impactFrame = 0;
let shakeAmount = 0;
let angle = 0;
let img;
let sound;
let currentMode = 'manual';
let soundPlayed = false; // Flag to check if sound has been played
function preload() {
// This can be used for loading assets, but we're doing it in the randomize() function instead
}
function setup() {
let canvas = createCanvas(400, 200);
canvas.parent('canvasContainer');
angleMode(DEGREES);
textAlign(CENTER, CENTER);
textSize(24);
setMode(currentMode); // Initialize input section when the page loads
}
function draw() {
background(255);
if (animationFrame > 0 && img) {
// Spin animation
push();
translate(width / 2, height / 2);
rotate(angle);
image(img, -25, -25, 50, 50); // Image is centered at (width / 2, height / 2)
pop();
angle += 10; // Increased spin speed
animationFrame--;
} else if (animationFrame === 0 && impactFrame < 30 && img) {
// Start shaking animation after spinning animation is complete
shakeAmount = 5;
let shakeX = random(-shakeAmount, shakeAmount);
let shakeY = random(-shakeAmount, shakeAmount);
image(img, width / 2 - 25 + shakeX, height / 2 - 25 + shakeY, 50, 50);
// Play sound only once
if (impactFrame === 0 && !soundPlayed && sound) {
sound.play();
soundPlayed = true;
}
impactFrame++;
} else if (impactFrame >= 30 && img) {
// After spinning and shaking, display the "Eat this" message
document.getElementById('result').innerText = `Eat this: ${selectedItem}!`;
// Shake the text a bit before resetting the animation
let shakeTextAmount = 5;
let shakeX = random(-shakeTextAmount, shakeTextAmount);
let shakeY = random(-shakeTextAmount, shakeTextAmount);
document.getElementById('result').style.transform = `translate(${shakeX}px, ${shakeY}px)`;
// Auto-reset after 2 seconds
setTimeout(resetAnimation, 2000);
}
}
function setMode(mode) {
currentMode = mode;
let inputSection = document.getElementById('inputSection');
if (mode === 'manual') {
inputSection.innerHTML = `
<p>Enter menu items (one per line):</p>
<textarea id="menuInput" rows="5" placeholder="Pizza\nBurger\nSalad"></textarea>
`;
} else {
inputSection.innerHTML = `
<p>Upload a menu image:</p>
<input type="file" id="imageInput" accept="image/*" onchange="scanImage()">
<p>Extracted items:</p>
<textarea id="menuInput" rows="5" placeholder="Items will appear here"></textarea>
`;
}
}
async function scanImage() {
if (currentMode !== 'image') return;
const fileInput = document.getElementById('imageInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files[0]) return;
resultDiv.innerText = "Scanning image...";
try {
const { data: { text } } = await Tesseract.recognize(fileInput.files[0], 'eng');
const items = text.split('\n').filter(item => item.trim() && !/^\d+\.?\s*$/.test(item));
document.getElementById('menuInput').value = items.join('\n');
resultDiv.innerText = items.length > 0 ? "Items extracted! Click Randomize." : "No items found.";
} catch (err) {
resultDiv.innerText = "Error scanning image: " + err.message;
console.error(err);
}
}
function randomize() {
if (animationFrame > 0 || impactFrame > 0) return;
const menuInput = document.getElementById('menuInput');
const items = menuInput.value.trim().split('\n').filter(item => item.trim());
if (items.length === 0) {
document.getElementById('result').innerText = "Please enter or scan items.";
return;
}
selectedItem = random(items);
animationFrame = 180;
impactFrame = 0;
angle = 0;
document.getElementById('result').innerText = "Randomizing...";
img = loadImage('Images/food_spin.jpg', () => {
console.log('Image loaded successfully');
}, (e) => console.error('Image load error:', e));
sound = loadSound('Sounds/superidol.mp3', () => {
console.log('Sound loaded successfully');
}, (e) => console.error('Sound load error:', e));
soundPlayed = false;
}
function resetAnimation() {
animationFrame = 0;
impactFrame = 0;
soundPlayed = false;
document.getElementById('result').style.transform = 'translate(0, 0)';
}
Explanation of changes:
Increased spin speed: The angle is now incremented by 10 degrees in each frame, making the spinning animation more visible.
Started shaking animation after spinning animation is complete: The shaking animation now starts after the animationFrame reaches 0.
Played sound only once: The sound is now played only once when impactFrame is 0.
Used setTimeout for auto-reset: The resetAnimation function is now called after a 2-second delay using setTimeout, ensuring that the "Eat this" message is displayed for a sufficient amount of time.
Tests and example uses:
Enter some menu items in the textarea and click "Randomize" to see the spinning animation, shaking animation, and "Eat this" message.
Upload an image and click "Randomize" to see the extracted items and the animation.
Verify that the sound is played only once and the animation resets after 2 seconds.