• Sakarit Gunkhan (Chankaew) is a Brooklyn-based Graphic/Web Designer & Visual/Concept Artist

Interactive Blend Mode Bubbles

Interactive Blend Mode Bubbles is a lightweight, responsive HTML5 Canvas web application that renders vibrant, bouncing circles with customizable composite blend modes (such as difference, exclusion, and screen) over a fully transparent background.

Built with vanilla JavaScript and styled with Tailwind CSS, the canvas supports smooth mouse and touch interactions, allowing users to grab, drag, and throw individual bubbles with realistic momentum and boundary collision physics.

Additionally, a compact, bottom-centered floating toolbar lets users switch blend modes on the fly, toggle a subtle dotted grid to test alpha transparency, and reset the bubble simulation at any time.

NOTE:

This web application is designed for desktop and tablet screens. Please open it on a laptop, desktop, or tablet for the full experience.

🌐
Interactive_bubbles.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sakarit — Interactive Blend Mode Bubbles</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
 <link rel="stylesheet" href="./style.css">
  <body class="relative select-none">

  <!-- Controls to test blend modes & transparent overlay -->
  <div class="fixed bottom-4 left-1/2 -translate-x-1/2 z-10 flex flex-wrap items-center justify-center gap-2 bg-white/80 dark:bg-zinc-900/80 backdrop-blur-md p-2.5 rounded-2xl shadow-lg border border-black/10 dark:border-white/10 text-xs sm:text-sm max-w-[95vw]">
    <div class="flex items-center gap-1.5 px-2 text-zinc-700 dark:text-zinc-200 font-medium">
      <span class="inline-block w-2.5 h-2.5 rounded-full bg-gradient-to-tr from-pink-500 to-amber-400 animate-pulse"></span>
      <span>Grab & Throw Bubbles</span>
    </div>

    <select id="blendModeSelect" aria-label="Blend Mode" class="bg-zinc-100 dark:bg-zinc-800 text-zinc-800 dark:text-zinc-100 rounded-xl px-2.5 py-1.5 border border-zinc-200 dark:border-zinc-700 focus:outline-none focus:ring-2 focus:ring-pink-500 cursor-pointer">
      <option value="difference" selected>difference</option>
      <option value="exclusion">exclusion</option>
      <option value="multiply">multiply</option>
      <option value="screen">screen</option>
      <option value="overlay">overlay</option>
      <option value="hard-light">hard-light</option>
      <option value="color-dodge">color-dodge</option>
      <option value="source-over">normal</option>
    </select>

    <button id="bgToggleBtn" class="bg-zinc-100 hover:bg-zinc-200 dark:bg-zinc-800 dark:hover:bg-zinc-700 text-zinc-800 dark:text-zinc-100 rounded-xl px-3 py-1.5 border border-zinc-200 dark:border-zinc-700 transition cursor-pointer" title="Toggle a test checkerboard behind the transparent canvas">
      Test Transparency
    </button>

    <button id="resetBtn" class="bg-pink-500 hover:bg-pink-600 text-white font-medium rounded-xl px-3 py-1.5 shadow-sm transition cursor-pointer">
      Reset
    </button>
  </div>

  <!-- Optional subtle backdrop element (hidden by default so canvas is 100% transparent) -->
  <div id="testBackdrop" class="hidden fixed inset-0 -z-10 pointer-events-none flex items-center justify-center" style="background-image: radial-gradient(#cbd5e1 1.5px, transparent 1.5px); background-size: 24px 24px;">
    <h1 class="text-4xl sm:text-6xl font-bold tracking-tight text-slate-800/30 uppercase">Transparent Layer</h1>
  </div>

  <!-- Interactive Canvas -->
  <canvas id="bubbleCanvas"></canvas>
   <!-- Script -->
  <script src="./script.js"></script>
  </body>
</html>
🎨
style.css
 html, body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
      overflow: hidden;
      background-color: transparent !important;
      font-family: 'Inter', sans-serif;
    }

    canvas {
      display: block;
      width: 100%;
      height: 100%;
      background-color: transparent !important;
      touch-action: none; /* Prevent default touch scrolling while dragging */
      cursor: grab;
    }

    canvas.grabbing {
      cursor: grabbing;
📄
style.js
    const canvas = document.getElementById('bubbleCanvas');
    const c = canvas.getContext('2d');

    // UI Controls
    const blendModeSelect = document.getElementById('blendModeSelect');
    const bgToggleBtn = document.getElementById('bgToggleBtn');
    const resetBtn = document.getElementById('resetBtn');
    const testBackdrop = document.getElementById('testBackdrop');

    let currentBlendMode = 'difference';
    let showTestBackdrop = false;

    blendModeSelect.addEventListener('change', (e) => {
      currentBlendMode = e.target.value;
    });

    bgToggleBtn.addEventListener('click', () => {
      showTestBackdrop = !showTestBackdrop;
      testBackdrop.classList.toggle('hidden', !showTestBackdrop);
      bgToggleBtn.textContent = showTestBackdrop ? 'Hide Grid' : 'Test Transparency';
    });

    // Set initial canvas dimensions
    function resizeCanvas() {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
    }
    resizeCanvas();

    window.addEventListener('resize', () => {
      resizeCanvas();
      // Keep existing bubbles within new bounds
      circleArray.forEach(circle => {
        if (circle.x + circle.radius > canvas.width) circle.x = Math.max(circle.radius, canvas.width - circle.radius);
        if (circle.y + circle.radius > canvas.height) circle.y = Math.max(circle.radius, canvas.height - circle.radius);
      });
    });

    const colors = ['blue', 'red', '#ff3131', 'deeppink', 'orange', '#ffcc00'];

    // Interaction / Grabbing state
    let grabbedCircle = null;
    let grabOffsetX = 0;
    let grabOffsetY = 0;
    let lastPointerX = 0;
    let lastPointerY = 0;
    let throwVx = 0;
    let throwVy = 0;

    class Circle {
      constructor(x, y, dx, dy, radius, color) {
        this.x = x;
        this.y = y;
        this.dx = dx;
        this.dy = dy;
        this.radius = radius;
        this.color = color;
        this.isGrabbed = false;
      }

      draw() {
        c.save(); // Save the current state
        c.globalCompositeOperation = currentBlendMode;
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();

        // Draw subtle feedback ring when grabbed
        if (this.isGrabbed) {
          c.globalCompositeOperation = 'source-over';
          c.beginPath();
          c.arc(this.x, this.y, this.radius + 4, 0, Math.PI * 2, false);
          c.strokeStyle = 'rgba(255, 255, 255, 0.7)';
          c.lineWidth = 2.5;
          c.stroke();
          c.closePath();
        }

        c.restore(); // Restore to avoid affecting the next drawing
      }

      update() {
        // Only apply autonomous movement if the bubble is not currently grabbed
        if (!this.isGrabbed) {
          // Bounce off horizontal walls
          if (this.x + this.radius > canvas.width) {
            this.x = canvas.width - this.radius;
            this.dx = -this.dx;
          } else if (this.x - this.radius < 0) {
            this.x = this.radius;
            this.dx = -this.dx;
          }

          // Bounce off vertical walls
          if (this.y + this.radius > canvas.height) {
            this.y = canvas.height - this.radius;
            this.dy = -this.dy;
          } else if (this.y - this.radius < 0) {
            this.y = this.radius;
            this.dy = -this.dy;
          }

          // Gentle damping if thrown very fast so it settles back to calm floating speed
          const speed = Math.hypot(this.dx, this.dy);
          if (speed > 4) {
            this.dx *= 0.985;
            this.dy *= 0.985;
          }

          this.x += this.dx;
          this.y += this.dy;
        }

        this.draw();
      }
    }

    let circleArray = [];

    function initBubbles() {
      circleArray = [];
      // Scale bubble count slightly for smaller mobile screens while keeping desktop at 30
      const count = window.innerWidth < 640 ? 18 : 30;

      for (let i = 0; i < count; i++) {
        const maxRadius = window.innerWidth < 640 ? 55 : 80;
        const radius = Math.random() * maxRadius + 20;
        const x = Math.random() * (canvas.width - radius * 2) + radius;
        const y = Math.random() * (canvas.height - radius * 2) + radius;
        const dx = (Math.random() - 0.5) * 4;
        const dy = (Math.random() - 0.5) * 4;
        const color = colors[Math.floor(Math.random() * colors.length)];

        circleArray.push(new Circle(x, y, dx, dy, radius, color));
      }
    }

    resetBtn.addEventListener('click', initBubbles);

    // Helper to extract pointer coordinates for both mouse and touch
    function getPointerPos(e) {
      const rect = canvas.getBoundingClientRect();
      if (e.touches && e.touches.length > 0) {
        return {
          x: e.touches[0].clientX - rect.left,
          y: e.touches[0].clientY - rect.top
        };
      }
      return {
        x: e.clientX - rect.left,
        y: e.clientY - rect.top
      };
    }

    // Grab start (Mouse & Touch)
    function handlePointerDown(e) {
      const pos = getPointerPos(e);

      // Iterate backwards so the topmost rendered circle gets grabbed first
      for (let i = circleArray.length - 1; i >= 0; i--) {
        const circle = circleArray[i];
        const dist = Math.hypot(pos.x - circle.x, pos.y - circle.y);

        if (dist <= circle.radius) {
          grabbedCircle = circle;
          grabbedCircle.isGrabbed = true;
          grabOffsetX = pos.x - circle.x;
          grabOffsetY = pos.y - circle.y;
          lastPointerX = pos.x;
          lastPointerY = pos.y;
          throwVx = 0;
          throwVy = 0;

          // Bring grabbed bubble to top layer
          circleArray.splice(i, 1);
          circleArray.push(grabbedCircle);

          canvas.classList.add('grabbing');
          break;
        }
      }
    }

    // Dragging (Mouse & Touch)
    function handlePointerMove(e) {
      const pos = getPointerPos(e);

      if (grabbedCircle) {
        if (e.cancelable) e.preventDefault();

        // Calculate instantaneous throw velocity
        throwVx = (pos.x - lastPointerX) * 0.45;
        throwVy = (pos.y - lastPointerY) * 0.45;

        lastPointerX = pos.x;
        lastPointerY = pos.y;

        // Clamp position inside canvas bounds while dragging
        const targetX = pos.x - grabOffsetX;
        const targetY = pos.y - grabOffsetY;

        grabbedCircle.x = Math.max(grabbedCircle.radius, Math.min(canvas.width - grabbedCircle.radius, targetX));
        grabbedCircle.y = Math.max(grabbedCircle.radius, Math.min(canvas.height - grabbedCircle.radius, targetY));
      } else if (!e.touches) {
        // Update hover cursor when hovering over a bubble on desktop
        let hovering = false;
        for (let i = circleArray.length - 1; i >= 0; i--) {
          const dist = Math.hypot(pos.x - circleArray[i].x, pos.y - circleArray[i].y);
          if (dist <= circleArray[i].radius) {
            hovering = true;
            break;
          }
        }
        canvas.style.cursor = hovering ? 'grab' : 'default';
      }
    }

    // Release / Throw (Mouse & Touch)
    function handlePointerUp() {
      if (grabbedCircle) {
        grabbedCircle.isGrabbed = false;

        // Apply throw velocity (capped so bubbles don't glitch through walls)
        const maxThrow = 18;
        if (Math.abs(throwVx) > 0.2 || Math.abs(throwVy) > 0.2) {
          grabbedCircle.dx = Math.max(-maxThrow, Math.min(maxThrow, throwVx));
          grabbedCircle.dy = Math.max(-maxThrow, Math.min(maxThrow, throwVy));
        }

        grabbedCircle = null;
        canvas.classList.remove('grabbing');
        canvas.style.cursor = 'grab';
      }
    }

    // Mouse event listeners
    canvas.addEventListener('mousedown', handlePointerDown);
    window.addEventListener('mousemove', handlePointerMove);
    window.addEventListener('mouseup', handlePointerUp);

    // Touch event listeners
    canvas.addEventListener('touchstart', handlePointerDown, { passive: false });
    window.addEventListener('touchmove', handlePointerMove, { passive: false });
    window.addEventListener('touchend', handlePointerUp);
    window.addEventListener('touchcancel', handlePointerUp);

    function animate() {
      requestAnimationFrame(animate);
      // clearRect clears all pixels to rgba(0, 0, 0, 0) making the canvas 100% transparent
      c.clearRect(0, 0, canvas.width, canvas.height);

      circleArray.forEach(circle => circle.update());
    }

    window.onload = function () {
      initBubbles();
      animate();
    };
Tirakas Gunkhan
Tirakas Gunkhan
http://tirakas.com
Brooklyn-based designer and visual artist with an omnidirectional creative practice, passionately integrating digital craft, physical narratives, and diverse artistic forms into immersive visual worlds. Driven by material curiosity, wearable art, circular design principles, and whimsical experimentation, his work transforms disparate media, forms, and ideas into unexpected, cohesive experiences that challenge conventions and reimagine the possibilities of artistic expression.
0:00
0:00