04Balls Whose Bounce Height Splits by Mass
Each ball's size doubles as a mass value used to calculate its drop force, so a light ball springs high and a heavy one barely lifts off, even from the same height.
Best forPhysics-demo card comparing balls of different sizes
BALL_MASS_BOUNCE_FACTORBASE_MASS_RESTrestitutionFor

<main class="stage">
<div class="bp is-demo" id="bpRoot">
<canvas class="bp__canvas" id="bpCanvas" width="480" height="300"></canvas>
<p class="bp__caption">Each ball's size doubles as its mass when computing drop force, so even from the same height a light ball bounces high while a heavy one stays low.</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-paper);
.bp {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
background: $stage-paper;
}
.bp__canvas {
display: block;
width: 100%;
height: 100%;
}
.bp__caption {
position: absolute;
left: $sp-3;
top: $sp-3;
max-width: 80%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 255, 255, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.bp__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.bp { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.bp {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
background: #ffffff;
}
.bp__canvas {
display: block;
width: 100%;
height: 100%;
}
.bp__caption {
position: absolute;
left: 12px;
top: 12px;
max-width: 80%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.bp__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.bp {
cursor: default;
}
}(function () {
// 기법: 공의 반지름을 그대로 질량으로 쓰고, 바닥에 닿을 때 되튀는 반발계수를
// BASE_MASS_REST * BALL_MASS_BOUNCE_FACTOR / mass 로 계산한다 — 질량이 작을수록
// 나눗셈 결과가 커져 반발계수가 크고, 질량이 클수록 작아 낮게 튄다. 네 공이 같은
// 높이·같은 시각에 떨어지므로 튀는 높이 차이가 질량 하나로만 갈린다.
var root = document.getElementById('bpRoot');
var canvas = document.getElementById('bpCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
var CALC_W = 320, CALC_H = 200;
var GRAVITY = 900;
var BALL_MASS_BOUNCE_FACTOR = 1.4;
var BASE_MASS_REST = 6;
var SLEEP_THRESHOLD = 24;
var SLEEP_HOLD_MS = 300;
var STAGE_BG = '255,255,255';
var MASSES = [10, 16, 22, 30];
var COLORS = ['#6ec8ff', '#4cd4a6', '#b9a0ff', '#ff9ebb'];
var XPOS = [0.16, 0.4, 0.64, 0.86];
function restitutionFor(mass) {
return Math.max(0.22, Math.min(0.95, BASE_MASS_REST * BALL_MASS_BOUNCE_FACTOR / mass));
}
var balls = [];
function reset() {
balls = [];
for (var i = 0; i < MASSES.length; i += 1) {
balls.push({
x: CALC_W * XPOS[i],
y: 26,
vy: 0,
mass: MASSES[i],
r: MASSES[i] * 0.5,
color: COLORS[i],
rest: restitutionFor(MASSES[i]),
squash: 0,
settled: false
});
}
impacts = [];
}
var impacts = [];
function stepSim(dtMs, nowMs) {
var dtS = dtMs / 1000;
for (var i = 0; i < balls.length; i += 1) {
var b = balls[i];
if (b.settled) { b.squash = Math.max(0, b.squash - dtS * 6); continue; }
b.vy += GRAVITY * dtS;
b.y += b.vy * dtS;
var floor = CALC_H - b.r;
if (b.y >= floor) {
b.y = floor;
if (Math.abs(b.vy) < SLEEP_THRESHOLD) {
b.vy = 0; b.settled = true; b.squash = 0.5;
} else {
impacts.push({ x: b.x, y: floor, born: nowMs, strength: Math.min(1, Math.abs(b.vy) / 320) });
b.squash = Math.max(b.squash, Math.min(0.85, Math.abs(b.vy) / 300));
b.vy = -b.vy * b.rest;
}
}
b.squash = Math.max(0, b.squash - dtS * 3.5);
}
var live = [];
for (var k = 0; k < impacts.length; k += 1) { if (nowMs - impacts[k].born < 260) live.push(impacts[k]); }
impacts = live;
}
function allSettled() {
for (var i = 0; i < balls.length; i += 1) { if (!balls[i].settled) return false; }
return true;
}
function draw(nowMs) {
var sx = cw / CALC_W, sy = ch / CALC_H;
ctx.clearRect(0, 0, cw, ch);
ctx.strokeStyle = 'rgba(23,20,26,0.08)';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, (CALC_H - 2) * sy);
ctx.lineTo(cw, (CALC_H - 2) * sy);
ctx.stroke();
for (var j = 0; j < balls.length; j += 1) {
var b = balls[j];
var px = b.x * sx, py = b.y * sy, pr = b.r * ((sx + sy) / 2);
var shf = Math.max(0, 1 - (CALC_H - b.r - b.y) / 130);
var shw = pr * (0.9 + shf * 0.4);
ctx.beginPath();
ctx.ellipse(px, (CALC_H - 3) * sy, shw, shw * 0.24, 0, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(23,20,26,' + (0.08 + shf * 0.14) + ')';
ctx.fill();
var sqX = 1 + b.squash * 0.5, sqY = 1 - b.squash * 0.35;
ctx.save();
ctx.translate(px, py + pr * b.squash * 0.35);
ctx.scale(sqX, sqY);
ctx.beginPath();
ctx.arc(0, 0, pr, 0, Math.PI * 2);
ctx.fillStyle = b.color;
ctx.fill();
ctx.restore();
}
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(elapsed);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var elapsed = 0;
if (reduce) root.classList.remove('is-demo');
reset();
var lastTs = null;
var acc = 0;
var FIXED_DT = 16;
var settledAt = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var frameDt = Math.min(ts - lastTs, 48);
lastTs = ts;
if (!reduce) {
acc += frameDt;
while (acc >= FIXED_DT) {
elapsed += FIXED_DT;
stepSim(FIXED_DT, elapsed);
if (allSettled()) {
if (settledAt === null) settledAt = elapsed;
else if (elapsed - settledAt > SLEEP_HOLD_MS) { settledAt = null; elapsed = 0; reset(); }
}
acc -= FIXED_DT;
}
draw(elapsed);
}
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./BallPhysicsMassBounceHeight.module.scss";
type Ball = { x: number; y: number; vy: number; mass: number; r: number; color: string; rest: number; squash: number; settled: boolean };
export default function BallPhysicsMassBounceHeight() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
const CALC_W = 320, CALC_H = 200;
const GRAVITY = 900;
const BALL_MASS_BOUNCE_FACTOR = 1.4;
const BASE_MASS_REST = 6;
const SLEEP_THRESHOLD = 24;
const SLEEP_HOLD_MS = 300;
const MASSES = [10, 16, 22, 30];
const COLORS = ["#6ec8ff", "#4cd4a6", "#b9a0ff", "#ff9ebb"];
const XPOS = [0.16, 0.4, 0.64, 0.86];
function restitutionFor(mass: number) {
return Math.max(0.22, Math.min(0.95, (BASE_MASS_REST * BALL_MASS_BOUNCE_FACTOR) / mass));
}
let balls: Ball[] = [];
let impacts: { x: number; y: number; born: number; strength: number }[] = [];
function reset() {
balls = MASSES.map((mass, i) => ({
x: CALC_W * XPOS[i], y: 26, vy: 0, mass, r: mass * 0.5,
color: COLORS[i], rest: restitutionFor(mass), squash: 0, settled: false
}));
impacts = [];
}
reset();
function stepSim(dtMs: number, nowMs: number) {
const dtS = dtMs / 1000;
for (const b of balls) {
if (b.settled) { b.squash = Math.max(0, b.squash - dtS * 6); continue; }
b.vy += GRAVITY * dtS;
b.y += b.vy * dtS;
const floor = CALC_H - b.r;
if (b.y >= floor) {
b.y = floor;
if (Math.abs(b.vy) < SLEEP_THRESHOLD) {
b.vy = 0; b.settled = true; b.squash = 0.5;
} else {
impacts.push({ x: b.x, y: floor, born: nowMs, strength: Math.min(1, Math.abs(b.vy) / 320) });
b.squash = Math.max(b.squash, Math.min(0.85, Math.abs(b.vy) / 300));
b.vy = -b.vy * b.rest;
}
}
b.squash = Math.max(0, b.squash - dtS * 3.5);
}
impacts = impacts.filter((im) => nowMs - im.born < 260);
}
function allSettled() { return balls.every((b) => b.settled); }
let cw = 480, ch = 300;
function draw() {
const sx = cw / CALC_W, sy = ch / CALC_H;
ctx!.clearRect(0, 0, cw, ch);
ctx!.strokeStyle = "rgba(23,20,26,0.08)";
ctx!.lineWidth = 1;
ctx!.beginPath();
ctx!.moveTo(0, (CALC_H - 2) * sy);
ctx!.lineTo(cw, (CALC_H - 2) * sy);
ctx!.stroke();
for (const b of balls) {
const px = b.x * sx, py = b.y * sy, pr = b.r * ((sx + sy) / 2);
const shf = Math.max(0, 1 - (CALC_H - b.r - b.y) / 130);
const shw = pr * (0.9 + shf * 0.4);
ctx!.beginPath();
ctx!.ellipse(px, (CALC_H - 3) * sy, shw, shw * 0.24, 0, 0, Math.PI * 2);
ctx!.fillStyle = "rgba(23,20,26," + (0.08 + shf * 0.14) + ")";
ctx!.fill();
const sqX = 1 + b.squash * 0.5, sqY = 1 - b.squash * 0.35;
ctx!.save();
ctx!.translate(px, py + pr * b.squash * 0.35);
ctx!.scale(sqX, sqY);
ctx!.beginPath();
ctx!.arc(0, 0, pr, 0, Math.PI * 2);
ctx!.fillStyle = b.color;
ctx!.fill();
ctx!.restore();
}
}
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw();
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let elapsed = 0;
if (reduce) root.classList.remove("is-demo");
let lastTs: number | null = null;
let acc = 0;
const FIXED_DT = 16;
let settledAt: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const frameDt = Math.min(ts - lastTs, 48);
lastTs = ts;
if (!reduce) {
acc += frameDt;
while (acc >= FIXED_DT) {
elapsed += FIXED_DT;
stepSim(FIXED_DT, elapsed);
if (allSettled()) {
if (settledAt === null) settledAt = elapsed;
else if (elapsed - settledAt > SLEEP_HOLD_MS) { settledAt = null; elapsed = 0; reset(); }
}
acc -= FIXED_DT;
}
draw();
}
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw();
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.bp} is-demo`} ref={rootRef}>
<canvas className={styles.bp__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.bp__caption}>공마다 다른 크기를 질량값으로 써서 떨어질 때 힘을 계산해, 같은 높이에서 떨어져도 가벼운 공은 높이 튀어 오르고 무거운 공은 낮게 튄다</p>
</div>
</main>
);
}04 Balls Whose Bounce Height Splits by Mass — Measured area 11.113% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · 9 Bouncing Ball Animation Patterns With Physics
The post this demo lives in
9 Bouncing Ball Animation Patterns With PhysicsThis bouncing ball animation set has nine builds below. Ball 01 trades velocity through a 0.82 restitution coefficient, See all demos in this post
If you like this effect - similar demos
game02 Ball Whose Reflection Angle Splits by Hit PositionBrick Breaker Game JavaScript — 9 Physics Patterns
game08 A Snake Whose Food Bursts Into Gravity-Bound Particles While the Score Digits Bounce UpCSS Snake Game: 9 Boards That Find Their Own Path
game01 A Raffle Board Where Name Balls Fall Through PinsJavaScript Pinball Game: 9 Physics Scenes