xxxxxxxxxx
146
let circleX = null;
let circleY = null;
let circleColor;
let initialAppleColor;
let targetAppleColor;
let colorTransitionAmount = 0;
let flipDirection = 1; //assume worm faces right
let lastMouseX;
function setup() {
createCanvas(400, 400);
aka = "#F22929";
midori = "#9AA63F";
pinku = "#DD9795";
burann = "#594540";
b = "#F2F2F2";
background(b);
// define the initial and final colors using RGB
initialAppleColor = color(aka);
finalAppleColor = color(midori);
setPositionAndColor();
lastMouseX = mouseX;
}
function setPositionAndColor() {
circleColor = burann;
let appleCenterX = 130,
appleCenterY = 180,
appleRadius = 75;
let wormbiteRadius = 10;
let maxRadius = appleRadius - wormbiteRadius;
// use polar coordinates to ensure the point is strictly inside the circle
let angle = random(TWO_PI);
let r = maxRadius * sqrt(random());
circleX = appleCenterX + r * cos(angle);
circleY = appleCenterY + r * sin(angle);
}
// have the wormbite change position everytime the worm hovers over
function mouseMoved() {
let wormbiteRadius = 10;
if (dist(mouseX, mouseY, circleX, circleY) <= wormbiteRadius) {
setPositionAndColor();
}
}
function draw() {
// apple color transition
colorTransitionAmount += 0.005;
if (colorTransitionAmount > 1) colorTransitionAmount = 0;
let appleColor = lerpColor(
initialAppleColor,
finalAppleColor,
colorTransitionAmount
);
// redraw background
background(b);
// mouse assist
fill(240);
noStroke();
rect(10, 10, 160, 30);
fill(0);
text(`Mouse Position: (${mouseX}, ${mouseY})`, 20, 30);
// apple
fill(appleColor);
stroke(0);
strokeWeight(1.2);
ellipse(130, 180, 150, 150);
ellipse(width - 130, 180, 150, 150);
quad(196, 141, 190, 96, 215, 105, 204, 141);
quad(72, 227, 328, 227, 232, 320, 168, 320);
line(193, 140, 196, 140);
line(204, 140, 207, 140);
noStroke();
rect(120, 160, 100, 100);
rect(150, 140, 100, 50);
quad(70.5, 225, 328.5, 225, 231, 319, 168, 319);
// random worm bite
if (circleX !== null && circleY !== null) {
fill(circleColor);
circle(circleX, circleY, 20);
}
// window
stroke(0);
strokeWeight(1);
fill(b);
ellipse(120, 160, 32, 35);
// door
rect(202, 275, 26, 35);
ellipse(215, 275, 26, 26);
noStroke();
rect(203, 275, 24, 33.5);
//worm
// only flip if movement is large enough
let mouseMovedDistance = mouseX - lastMouseX;
if (Math.abs(mouseMovedDistance) > 25) {
flipDirection = mouseMovedDistance > 0 ? 1 : -1;
lastMouseX = mouseX;
}
drawWorm(mouseX, mouseY, flipDirection);
}
function drawWorm(x, y, direction) {
let baseLength = 10;
stroke(pinku);
strokeWeight(10);
noFill();
beginShape();
curveVertex(x, y);
curveVertex(x - direction * baseLength, y);
curveVertex(x - direction * baseLength * 2.6, y);
curveVertex(x - direction * baseLength * 4, y - 20);
curveVertex(x - direction * baseLength * 7, y - 3);
curveVertex(x - direction * baseLength * 8, y);
curveVertex(x - direction * baseLength * 10, y);
endShape();
//worm eye
fill(0);
noStroke();
circle(x - direction * baseLength, y - 1, 3.5);
}