Braylon's Strobe

This sketch creates a high-energy strobe effect with a responsive canvas filled with colorful squares that pulse in different rhythms and the name "Braylon" flashing at the center. The entire scene toggles between light and dark backgrounds, giving it a dynamic, attention-grabbing visual presence.

🧪 Try This!

Experiment with the code by making these changes:

  1. Make the text bigger and bolder — Increasing the text size makes "Braylon" dominate the center of the screen and easier to read during the strobe
  2. Add a fourth pair of flashing squares — Duplicate one of the rectangle pairs and change its color and position to create more visual rhythm—try magenta or cyan
  3. Slow down the entire strobe effect — The main flashing uses % 30—try % 60 to make the entire effect flicker half as fast and feel more hypnotic
  4. Change the text color scheme — Swap the text colors so the text is white on light background and black on dark—creating an inverted contrast effect
Prefer the full editor? Open it there →

📖 About This Sketch

This sketch animates a bold strobe effect where six colored squares pulse independently around the canvas while the name "Braylon" flashes in synchrony with the background. The whole scene toggles between light gray and black every quarter-second, creating an energetic, club-like atmosphere. It demonstrates three essential p5.js techniques: using frameCount to create timed animations, conditional logic to switch between visual states, and responsive canvas sizing.

The code is organized around a single draw() function that redraws everything sixty times per second, using modulo arithmetic (the % operator) to trigger color changes at different intervals. By studying it, you will learn how to layer multiple independent animations on top of each other and how small frame-counting tricks create synchronized or staggered visual rhythms.

⚙️ How It Works

  1. When the sketch loads, setup() creates a canvas that fills the entire browser window using windowWidth and windowHeight, making it responsive to window resizing
  2. Every frame, draw() first checks frameCount to decide whether the background should be light gray or black—it alternates every 15 frames to create the main flashing effect
  3. Next, the code draws six rectangles in two pairs and four corners, each pair flashing between white and a different color (red, blue, or green) at different rates: red pulses every 20 frames, blue every 30 frames, and green every 40 frames
  4. The background color also determines the text color: black text appears when the background is light, and white text when the background is dark, keeping "Braylon" readable at all times
  5. The windowResized() function ensures the canvas and all elements scale smoothly if the browser window is resized

🎓 Concepts You'll Learn

Frame counting and timingModulo operator (%) for cycling behaviorConditional logic for state switchingResponsive canvas sizingColor and fill propertiesText rendering and alignmentIndependent animation cycles

📝 Code Breakdown

setup()

setup() runs once when the sketch starts. The windowWidth and windowHeight variables are built into p5.js and always hold the current browser window dimensions, making this technique perfect for full-screen sketches.

function setup() {
  createCanvas(windowWidth, windowHeight);
  // Tip: Use windowWidth/windowHeight for responsive canvas
}
Line-by-line explanation (1 lines)
createCanvas(windowWidth, windowHeight);
Creates a canvas that stretches to fill the entire browser window, making the sketch responsive—it adapts if the window is resized

draw()

draw() is the heartbeat of p5.js—it runs 60 times per second by default. Every frame, it clears the canvas (background) and redraws everything. By checking frameCount with modulo arithmetic, you can create animations that cycle at different rates. The key insight is that smaller modulo divisors (like % 20) create faster cycles, while larger ones (like % 60) create slower cycles.

🔬 These lines draw two red/white squares. What happens if you change the second parameter (10) to 5? What about changing it to 15? How does that change the rhythm?

  // Rectangle 1 & 2: Flash between white and red every 10 frames
  if (frameCount % 20 < 10) {
    fill(255); // White
  } else {
    fill(255, 0, 0); // Red
  }
  rect(50, 50, 100, 100); // Top-left
  rect(width - 150, height - 150, 100, 100); // Bottom-right

🔬 Notice this blue rectangle uses the SAME frameCount condition as the background. What happens if you change this to frameCount % 20 < 10 instead? The blue squares will now flash at a different rhythm than the background—can you predict if they'll sync up, or always be out of step?

  // Rectangle 3 & 4: Flash between white and blue every 15 frames
  if (frameCount % 30 < 15) {
    fill(255); // White
  } else {
    fill(0, 0, 255); // Blue
  }
function draw() {
  // Background flashing effect
  // This will toggle the background between light gray (220) and black (0)
  // every 15 frames (about 0.25 seconds at 60fps), creating a flashing effect.
  if (frameCount % 30 < 15) {
    background(220); // Light gray
  } else {
    background(0); // Black
  }

  // Flashing rectangles
  // We'll draw a few rectangles around the canvas that also change color.
  noStroke(); // No border for the rectangles

  // Rectangle 1 & 2: Flash between white and red every 10 frames
  if (frameCount % 20 < 10) {
    fill(255); // White
  } else {
    fill(255, 0, 0); // Red
  }
  rect(50, 50, 100, 100); // Top-left
  rect(width - 150, height - 150, 100, 100); // Bottom-right

  // Rectangle 3 & 4: Flash between white and blue every 15 frames
  if (frameCount % 30 < 15) {
    fill(255); // White
  } else {
    fill(0, 0, 255); // Blue
  }
  rect(width - 150, 50, 100, 100); // Top-right
  rect(50, height - 150, 100, 100); // Bottom-left

  // Rectangle 5 & 6: Flash between white and green every 20 frames
  if (frameCount % 40 < 20) {
    fill(255); // White
  } else {
    fill(0, 255, 0); // Green
  }
  rect(width / 2 - 50, height / 4, 100, 100); // Mid-top
  rect(width / 2 - 50, height * 3 / 4 - 100, 100, 100); // Mid-bottom


  // Add your name, Braylon!
  // Set text color to toggle with the background for better visibility.
  if (frameCount % 30 < 15) {
    fill(0); // Black text when background is light gray
  } else {
    fill(255); // White text when background is black
  }
  textSize(48); // Set the text size
  textAlign(CENTER, CENTER); // Align text horizontally and vertically in the center
  text("Braylon", width / 2, height / 2); // Display "Braylon" in the middle of the canvas
}
Line-by-line explanation (10 lines)

🔧 Subcomponents:

conditional Background Flash Toggle if (frameCount % 30 < 15) {

Uses modulo arithmetic to switch the background between light gray and black every 15 frames, creating the main strobe effect

conditional Red Rectangle Flash if (frameCount % 20 < 10) {

Makes the top-left and bottom-right squares pulse between white and red at a different rate than the background

conditional Blue Rectangle Flash if (frameCount % 30 < 15) {

Synchronizes the blue squares with the background flash, creating layered visual rhythm

conditional Green Rectangle Flash if (frameCount % 40 < 20) {

Pulses the green squares at yet another rate, creating independent animation cycles

conditional Text Color Contrast if (frameCount % 30 < 15) {

Ensures the text color inverts with the background so "Braylon" stays readable at all times

if (frameCount % 30 < 15) {
frameCount is a p5.js variable that increments by 1 every frame. The % operator (modulo) finds the remainder when frameCount is divided by 30. If that remainder is less than 15, the condition is true for the first half of a 30-frame cycle
background(220); // Light gray
Fills the entire canvas with light gray (RGB value 220) when the condition is true, creating the first state of the strobe
} else {
When the first condition is false (remainder is 15 or higher), this block runs instead
background(0); // Black
Fills the canvas with black, creating the opposite state of the strobe effect
noStroke(); // No border for the rectangles
Tells p5.js not to draw borders around the rectangles—only their filled interior will show
rect(50, 50, 100, 100); // Top-left
Draws a rectangle starting at coordinates (50, 50) with width 100 and height 100—this is the top-left square
rect(width - 150, height - 150, 100, 100); // Bottom-right
Draws a second rectangle in the bottom-right corner by calculating its position from the canvas width and height, ensuring it stays in the corner even when the window is resized
textSize(48); // Set the text size
Sets the font size for all text drawn after this line to 48 pixels tall
textAlign(CENTER, CENTER); // Align text horizontally and vertically in the center
Tells p5.js that the text coordinates should define the CENTER of the text, not the top-left corner—this makes it easy to position text exactly where you want it
text("Braylon", width / 2, height / 2); // Display "Braylon" in the middle of the canvas
Draws the string "Braylon" centered at the middle of the canvas (width/2, height/2)

windowResized()

windowResized() is a built-in p5.js function that is called automatically whenever the browser window changes size. By placing resizeCanvas() inside it, you ensure that your canvas and all positioning calculations (like width/2 and height/2) stay valid even after resizing.

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
  // Called when preview panel is resized
}
Line-by-line explanation (1 lines)
resizeCanvas(windowWidth, windowHeight);
Automatically resizes the p5.js canvas to match the new window dimensions whenever the browser window is resized, keeping the sketch responsive

📦 Key Variables

frameCount number

A built-in p5.js variable that increments by 1 every frame (60 times per second by default). Used with modulo to create timed animations

if (frameCount % 30 < 15) { ... }
width number

A built-in p5.js variable that holds the current canvas width in pixels, allowing you to position elements relative to canvas size

rect(width - 150, 50, 100, 100);
height number

A built-in p5.js variable that holds the current canvas height in pixels, allowing you to position elements relative to canvas size

rect(50, height - 150, 100, 100);
windowWidth number

A built-in p5.js variable that holds the browser window's current width, used to make the canvas fill the entire window

createCanvas(windowWidth, windowHeight);
windowHeight number

A built-in p5.js variable that holds the browser window's current height, used to make the canvas fill the entire window

createCanvas(windowWidth, windowHeight);

🔧 Potential Improvements (4)

Here are some ways this code could be enhanced:

STYLE draw() — rectangle positioning

The rectangle positions are hardcoded with magic numbers like 50, 150, and calculated values, making it hard to adjust the layout or understand the spacing

💡 Create variables at the top of draw() like let margin = 50; let rectSize = 100; then use them: rect(margin, margin, rectSize, rectSize) instead. This makes the layout scalable and easier to tweak

FEATURE draw() — color flash patterns

All rectangles flash between white and a color, but some pairs share the same frameCount logic (e.g., background and blue rectangles both use % 30 < 15), reducing visual variety

💡 Use different modulo values for each color pair to create more interesting staggered rhythms. For example, use % 35 < 17 for one pair and % 25 < 12 for another to create polyrhythmic effects

PERFORMANCE draw() — text rendering

textAlign(), textSize(), and text color are set every single frame even though they don't change, wasting CPU cycles

💡 Move textAlign() and textSize() to setup() since they are static—only keep the fill() and text() calls in draw() to update the text color

BUG windowResized()

If rectangles are positioned with hard pixel values like 50, 150 instead of relative to width/height, they won't reposition when the window resizes, staying in fixed corners instead of scaling

💡 Adjust all rect() calls to use expressions like width - 150 and height - 150, or refactor with margin variables so all positions scale responsively

🔄 Code Flow

Code flow showing setup, draw, windowresized

💡 Click on function names in the diagram to jump to their code

graph TD start[Start] --> setup[setup] setup --> draw[draw loop] draw --> backgroundflash[background-flash] draw --> redrectanglesflash[red-rectangles-flash] draw --> bluerectanglesflash[blue-rectangles-flash] draw --> greenrectanglesflash[green-rectangles-flash] draw --> textcolortoggle[text-color-toggle] backgroundflash --> bgtoggle[Background Flash Toggle] bgtoggle --> draw click setup href "#fn-setup" click draw href "#fn-draw" click backgroundflash href "#sub-background-flash" click redrectanglesflash href "#sub-red-rectangles-flash" click bluerectanglesflash href "#sub-blue-rectangles-flash" click greenrectanglesflash href "#sub-green-rectangles-flash" click textcolortoggle href "#sub-text-color-toggle" redrectanglesflash --> redtoggle[Red Rectangle Flash] redtoggle --> draw bluerectanglesflash --> bluetoggle[Blue Rectangle Flash] bluetoggle --> draw greenrectanglesflash --> greentoggle[Green Rectangle Flash] greentoggle --> draw textcolortoggle --> textinvert[Text Color Inversion] textinvert --> draw draw -->|60 times per second| draw

❓ Frequently Asked Questions

What visual effects does the p5.js sketch 'Sketch 2026-02-20 20:37' create?

The sketch features a bold, flashing canvas with colorful squares pulsing in different rhythms and dynamically displays the name 'Braylon' at the center, all while transitioning between light and dark backgrounds.

Is there any user interaction available in this p5.js creative coding sketch?

The sketch does not include interactive elements; it is a visual display that runs continuously without user inputs.

What creative coding concepts are demonstrated in this sketch?

This sketch showcases techniques such as frame-based animations, color toggling, and responsive design by utilizing the window dimensions for canvas creation.

Preview

Braylon's Strobe - p5.js creative coding sketch preview
Sketch Preview
Code flow diagram showing the structure of Braylon's Strobe - Code flow showing setup, draw, windowresized
Code Flow Diagram