xxxxxxxxxx
45
let fadeAmount = 255; // Initial alpha value for the image
let fadeSpeed = 1; // Speed of the fade (decrease this for faster fading)
let min = 0;
let fading = false
function preload() {
img = loadImage('rock.PNG'); // Load your image
}
function setup() {
createCanvas(400, 400);
// Create a button and position it
let fadeButton = createButton('Fade Image');
fadeButton.position(10, height - 40);
// Add an event listener to the button
fadeButton.mousePressed(fadeImage);
}
function draw() {
background(220);
// Draw the image with the current alpha value
tint(255, fadeAmount); // Apply the fade effect (alpha value)
image(img, 100, 100, 200, 200); // Draw the image at position (100, 100)
}
function fadeImage() {
// Decrease the alpha value to fade the image out
if (fading) {
fadeAmount = fadeAmount - fadeSpeed;
if (fadeAmount < min) {
fading = false;
}
} else {
fading = true;
}
print(fadeAmount)
}