xxxxxxxxxx
let currentScene = 0; // To switch between scenes
let popcornImg, puccaImg, wineGlassImg;
let x, y; // Agent position
let stepSz = 15; // Step size for drawing
let colorPalettes = [
['#de6b48', '#e5b181', '#f4b9b2', '#daedbd', '#7dbbc3'], // Palette 1
['#ff595e', '#ffca3a', '#8ac926', '#1982c4', '#6a4c93'], // Palette 2
['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'] // Palette 3
]; // Different color palettes for each scene
let dice;
let saveButton; // Save button variable
// Preload images
function preload() {
popcornImg = loadImage("Popcorn.jpeg");
puccaImg = loadImage("Pucca_ Imprimibles Gratis para Fiestas_.jpeg");
wineGlassImg = loadImage("şarap bardağı.jpeg");
}
function setup() {
createCanvas(800, 800);
background(10);
noStroke();
// Initialize agent position
x = width / 2;
y = height / 2;
// Create Save Button
saveButton = createButton('Save Canvas');
saveButton.position(10, height + 10); // Place below the canvas
saveButton.mousePressed(saveCurrentCanvas); // Add event listener
}
function draw() {
if (currentScene === 0) {
drawRandomWalk(popcornImg, "circle", colorPalettes[0]);
} else if (currentScene === 1) {
drawRandomWalk(puccaImg, "triangle", colorPalettes[1]);
} else if (currentScene === 2) {
drawRandomWalk(wineGlassImg, "square", colorPalettes[2]);
}
}
// Function to perform random walk and draw shapes
function drawRandomWalk(img, shape, palette) {
img.resize(width, height); // Resize image to fit canvas
dice = random(0, 1); // Random direction
// Move agent based on random dice
if (dice < 0.25) {
x = x - stepSz; // Left
} else if (dice < 0.5) {
x = x + stepSz; // Right
} else if (dice < 0.75) {
y = y - stepSz; // Up
} else {
y = y + stepSz; // Down
}
// Keep agent within canvas bounds
x = constrain(x, 0, width - 1);
y = constrain(y, 0, height - 1);
// Get pixel color from image
let c = img.get(x, y);
// Randomly pick a color from the palette
let randomColor = random(palette);
// Draw the corresponding shape
fill(lerpColor(color(c), color(randomColor), 0.5)); // Blend pixel color with palette
noStroke();
if (shape === "circle") {
ellipse(x, y, stepSz, stepSz); // Draw circle
} else if (shape === "triangle") {
let x1 = x, y1 = y;
let x2 = x + stepSz, y2 = y;
let x3 = x + stepSz / 2, y3 = y - stepSz;
triangle(x1, y1, x2, y2, x3, y3); // Draw triangle
} else if (shape === "square") {
rect(x, y, stepSz, stepSz); // Draw square
}
}
// Save canvas function
function saveCurrentCanvas() {
saveCanvas('scene_' + currentScene, 'png');
}
// Change scenes with keypress
function keyPressed() {
if (key === 'n') { // Press 'n' to switch scenes
currentScene++;
if (currentScene > 2) {
currentScene = 0; // Reset to the first scene
}
background(10); // Clear canvas when switching scenes
x = width / 2; // Reset agent position
y = height / 2;
}
}