drawGlowingEye()
This helper is called four times per frame (twice per monster) and shows how to combine drawingContext.shadowBlur with simple trigonometry (atan2, cos, sin) to build an expressive, reusable eye component instead of hardcoding each eye separately.
🔬 The flicker range is -10% to +10% of the iris size. What happens visually if you widen it to -irisSize * 0.5 to irisSize * 0.5?
let baseGlowAmount = irisSize * 0.7; // Base blur amount for the glow
let flickerAmount = random(-irisSize * 0.1, irisSize * 0.1); // Small random variation for flicker
function drawGlowingEye(irisX, irisY, irisSize, glowColor, targetX, targetY) {
let pupilSize = irisSize * 0.4;
let baseGlowAmount = irisSize * 0.7; // Base blur amount for the glow
let flickerAmount = random(-irisSize * 0.1, irisSize * 0.1); // Small random variation for flicker
let currentGlowAmount = baseGlowAmount + flickerAmount; // Apply flicker
// Set shadow properties for the glowing effect
drawingContext.shadowColor = glowColor;
drawingContext.shadowBlur = currentGlowAmount;
// Draw the iris (the glowing part of the eye)
fill(glowColor);
circle(irisX, irisY, irisSize);
// Reset shadow blur and color for the pupil
// This ensures only the iris glows, not the pupil or other shapes
drawingContext.shadowBlur = 0;
drawingContext.shadowColor = 'black'; // Reset to a default or black
// Calculate pupil position based on targetX, targetY
// The pupil moves towards the target within a limited range
let dx = targetX - irisX;
let dy = targetY - irisY;
let angle = atan2(dy, dx);
let pupilOffset = irisSize * 0.2; // How far the pupil can move from the center
let pupilX = irisX + cos(angle) * pupilOffset;
let pupilY = irisY + sin(angle) * pupilOffset;
// Draw the pupil
fill(0); // Black pupil
circle(pupilX, pupilY, pupilSize);
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
let currentGlowAmount = baseGlowAmount + flickerAmount; // Apply flicker
Adds a small random offset each frame so the glow subtly flickers instead of staying static
let angle = atan2(dy, dx);
Finds the angle from the eye center to the target (mouse) so the pupil can point toward it
let pupilSize = irisSize * 0.4;- The pupil is always 40% the size of the iris, keeping proportions consistent no matter how big the eye is.
let flickerAmount = random(-irisSize * 0.1, irisSize * 0.1);- Picks a small random number every frame so the glow blur size jitters slightly, simulating a flickering light.
drawingContext.shadowBlur = currentGlowAmount;- Uses the raw HTML5 canvas API (via drawingContext) to add a soft blurred glow behind the next shape drawn.
drawingContext.shadowBlur = 0;- Turns the glow off again immediately after drawing the iris so the pupil and later shapes don't also glow.
let angle = atan2(dy, dx);- atan2 converts the difference in x/y between the eye and the mouse into an angle in radians, pointing at the target.
let pupilX = irisX + cos(angle) * pupilOffset;- Moves the pupil a fixed small distance in the direction of that angle, so it looks like it's gazing at the mouse without leaving the iris.