← Demos
Web Pages · Hello World

Brett Alansky Hello World

Hello world with draggable, bouncy physics, the words have gravity and you can toss them around

by Brett Alansky Sep 1, 2026 10 views 5.6 KB
Sandbox · brett-alansky-hello-world · no access to your account
View source — the whole page is this one file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Gravity Words</title>
<style>
  :root { color-scheme: dark; }
  * { box-sizing: border-box; }
  html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; }
  body {
    background: #101329;
    color: #f8f7ff;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  }
  canvas { display: block; width: 100vw; height: 100vh; touch-action: none; }
  .ui {
    position: fixed; inset: 0 0 auto 0; padding: 26px 28px;
    pointer-events: none; display: flex; justify-content: space-between;
    align-items: flex-start;
  }
  h1 { margin: 0; font-size: clamp(1.8rem, 5vw, 3.6rem); letter-spacing: -.07em; line-height: .9; }
  p { margin: 10px 0 0; color: #aeb4d8; font-size: .95rem; }
  button {
    pointer-events: auto; border: 1px solid #626a9b; background: #252b55;
    color: white; border-radius: 999px; padding: 10px 17px; font: inherit;
    cursor: pointer; box-shadow: 0 5px 20px #05071455;
  }
  button:hover { background: #343b70; }
  button:focus-visible { outline: 3px solid #8ff5dc; outline-offset: 3px; }
  .hint {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
    color: #9da5cf; font-size: .8rem; letter-spacing: .04em;
    text-align: center; pointer-events: none; opacity: .85;
  }
</style>
</head>
<body>
<canvas id="scene" aria-label="A playful physics scene with draggable words"></canvas>
<div class="ui">
  <div>
    <h1>hello, world!</h1>
    <p>gravity is enabled</p>
  </div>
  <button id="reset" type="button">Reset words</button>
</div>
<div class="hint">drag a word · toss it · watch it bounce</div>

<script>
const canvas = document.getElementById("scene");
const ctx = canvas.getContext("2d");
const reset = document.getElementById("reset");
let W, H, dpr, last = performance.now(), selected = null, pointer = {x:0,y:0};
const colors = ["#ff7d9c","#ffd166","#72e0c0","#91a8ff","#d49aff","#ff9e68"];
const seeds = [
  ["hello", .28, .30, -0.05], ["world!", .62, .22, .06],
  ["bounce", .46, .45, -.08], ["✨", .76, .49, .12],
  ["gravity", .24, .62, .04], ["wheee", .68, .70, -.07]
];
let words = [];

function fit() {
  dpr = Math.min(devicePixelRatio || 1, 2);
  W = innerWidth; H = innerHeight;
  canvas.width = W * dpr; canvas.height = H * dpr;
  ctx.setTransform(dpr,0,0,dpr,0,0);
}
function makeWords() {
  words = seeds.map((s,i) => ({
    text:s[0], x:s[1]*W, y:s[2]*H, vx:(i%2?1:-1)*.5, vy:0,
    angle:s[3], va:(i%2?1:-1)*.002, w:0, h:48, color:colors[i],
    dragging:false
  }));
}
function sizeWord(o) {
  ctx.font = "700 24px system-ui, sans-serif";
  o.w = ctx.measureText(o.text).width + 34;
  o.h = 48;
}
function resetAll() { makeWords(); words.forEach(sizeWord); }
function pos(e) {
  const r = canvas.getBoundingClientRect();
  return {x:e.clientX-r.left, y:e.clientY-r.top};
}
function hit(x,y) {
  for (let i=words.length-1;i>=0;i--) {
    const o=words[i], dx=x-o.x, dy=y-o.y, c=Math.cos(o.angle), s=Math.sin(o.angle);
    const lx=dx*c+dy*s, ly=-dx*s+dy*c;
    if (Math.abs(lx)<o.w/2 && Math.abs(ly)<o.h/2) return o;
  }
}
canvas.addEventListener("pointerdown", e => {
  pointer=pos(e); selected=hit(pointer.x,pointer.y);
  if (selected) {
    selected.dragging=true; selected.vx=selected.vy=0;
    canvas.setPointerCapture(e.pointerId);
    words.splice(words.indexOf(selected),1); words.push(selected);
  }
});
canvas.addEventListener("pointermove", e => {
  if (!selected) return;
  const p=pos(e), dx=p.x-pointer.x, dy=p.y-pointer.y;
  selected.x=p.x; selected.y=p.y;
  selected.vx=dx*1.8; selected.vy=dy*1.8;
  selected.angle += dx*.002; pointer=p;
});
function release(e) {
  if (selected) { selected.dragging=false; selected=null; try{canvas.releasePointerCapture(e.pointerId)}catch(_){} }
}
canvas.addEventListener("pointerup", release);
canvas.addEventListener("pointercancel", release);
reset.addEventListener("click", resetAll);
addEventListener("resize", () => { fit(); resetAll(); });

function background(t) {
  const g=ctx.createRadialGradient(W*.5,H*.35,0,W*.5,H*.5,Math.max(W,H)*.8);
  g.addColorStop(0,"#252b59"); g.addColorStop(1,"#0c0e20");
  ctx.fillStyle=g; ctx.fillRect(0,0,W,H);
  ctx.globalAlpha=.18;
  for(let i=0;i<45;i++) {
    const x=(i*137.7)%W, y=(i*79.3)%H, r=1+(i%3);
    ctx.fillStyle=i%2?"#91a8ff":"#8ff5dc";
    ctx.beginPath(); ctx.arc(x,y+Math.sin(t*.0005+i)*4,r,0,7); ctx.fill();
  }
  ctx.globalAlpha=1;
}
function draw(o) {
  ctx.save(); ctx.translate(o.x,o.y); ctx.rotate(o.angle);
  ctx.shadowColor=o.color+"66"; ctx.shadowBlur=18;
  ctx.fillStyle=o.color; ctx.beginPath();
  const x=-o.w/2,y=-o.h/2,r=14;
  ctx.roundRect(x,y,o.w,o.h,r); ctx.fill();
  ctx.shadowBlur=0; ctx.fillStyle="#14172e";
  ctx.font="700 24px system-ui, sans-serif"; ctx.textAlign="center"; ctx.textBaseline="middle";
  ctx.fillText(o.text,0,1); ctx.restore();
}
function loop(now) {
  const dt=Math.min((now-last)/16.67,2); last=now;
  background(now);
  words.forEach(o => {
    if (!o.dragging) {
      o.vy += .42*dt; o.x += o.vx*dt; o.y += o.vy*dt; o.angle += o.va*dt;
      const pad=8;
      if(o.x-o.w/2<pad){o.x=pad+o.w/2;o.vx=Math.abs(o.vx)*.82}
      if(o.x+o.w/2>W-pad){o.x=W-pad-o.w/2;o.vx=-Math.abs(o.vx)*.82}
      if(o.y+o.h/2>H-pad){o.y=H-pad-o.h/2;o.vy=-Math.abs(o.vy)*.72;o.vx*=.985}
      if(o.y-o.h/2<pad){o.y=pad+o.h/2;o.vy=Math.abs(o.vy)*.7}
    }
    draw(o);
  });
  requestAnimationFrame(loop);
}
fit(); resetAll(); requestAnimationFrame(loop);
</script>
</body>
</html>

Reaches out to

Nothing. This page is entirely self-contained and contacts no other website when it runs.

Licence

Published on Globefin under the MIT License. You may copy, change, and reuse this page, including commercially — just keep the notice below with it.

Copyright © 2026 Brett Alansky. Licensed under the MIT License. Published on Globefin.

Provided as is, without warranty. Any external libraries it loads keep their own licences. Terms →

Sandbox

Scripts run, but the demo has its own throwaway origin. It can't read your session or cookies, can't reach the page around it, and can't navigate your tab. Don't enter real passwords or personal data into any demo.