js实现带音效海底深潜开关滑动滑块代码
代码语言:html
所属分类:其他
代码由minimax-m3.1-flash ai生成,可能有错误,仅供参考:点击查看提示词
代码描述:带音效海底深潜开关滑动滑块
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="dark light">
<meta name="description" content="Material Studies No. 062 — Daybreak Switch. A WebGL2 and Web Audio night-to-morning toggle with a pixel-art room inside.">
<title>Daybreak Switch — Material Studies No. 062</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
:root {
--bg: #0c0f22;
--fg: #ccd2ef;
--dim: #7d85b6;
--accent: #f0a468;
--line: rgba(176, 188, 255, 0.16);
--serif: Georgia, "Iowan Old Style", "Times New Roman", Times, serif;
--sans: "Helvetica Neue", Helvetica, Inter, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
html, body { height: 100%; }
body {
margin: 0;
background: var(--bg);
color: var(--fg);
font-family: var(--sans);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
display: flex;
align-items: center;
justify-content: center;
padding: clamp(20px, 5vw, 56px) clamp(16px, 4vw, 40px);
overflow-x: hidden;
}
.sheet {
width: 100%;
max-width: 820px;
text-align: center;
}
/* ------------------------------------------------------------- header */
.kicker {
margin: 0 0 clamp(14px, 3vw, 22px);
font-size: 10.5px;
line-height: 1.4;
letter-spacing: 0.26em;
text-transform: uppercase;
color: var(--dim);
}
.kicker .sep { opacity: 0.5; padding: 0 0.35em; }
.title {
margin: 0;
font-family: var(--serif);
font-style: italic;
font-weight: 400;
font-size: clamp(38px, 9.2vw, 66px);
line-height: 1;
letter-spacing: -0.012em;
color: var(--fg);
}
.caption {
margin: clamp(12px, 2.4vw, 18px) auto 0;
max-width: 62ch;
font-size: clamp(13px, 1.55vw, 15px);
line-height: 1.6;
color: var(--dim);
}
/* -------------------------------------------------------------- stage */
.stage {
margin: clamp(30px, 6vw, 58px) 0 clamp(26px, 5vw, 44px);
display: flex;
justify-content: center;
}
.switch {
position: relative;
display: inline-block;
line-height: 0;
border-radius: 999px;
cursor: grab;
touch-action: none;
user-select: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
outline: none;
}
.switch:active { cursor: grabbing; }
.switch canvas { display: block; }
.switch:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 7px;
}
/* ----------------------------------------------------------- readouts */
.readouts {
display: flex;
justify-content: center;
align-items: flex-start;
gap: clamp(18px, 5vw, 54px);
padding: clamp(14px, 3vw, 20px) 0;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.ro { display: flex; flex-direction: column; align-items: center; gap: 7px; min-width: 84px; }
.ro-k {
font-size: 9.5px;
letter-spacing: 0.24em;
text-transform: uppercase;
color: var(--dim);
}
.ro-v {
font-family: var(--serif);
font-size: clamp(16px, 2.4vw, 21px);
font-style: italic;
line-height: 1;
color: var(--fg);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
/* --------------------------------------------------------------- foot */
.hint {
margin: clamp(18px, 3.4vw, 26px) 0 clamp(16px, 3vw, 22px);
font-size: 11.5px;
line-height: 1.7;
letter-spacing: 0.03em;
color: var(--dim);
}
.sound {
appearance: none;
-webkit-appearance: none;
font-family: var(--sans);
font-size: 10px;
letter-spacing: 0.22em;
text-transform: uppercase;
color: var(--fg);
background: transparent;
border: 1px solid var(--line);
border-radius: 999px;
padding: 10px 20px 9px;
cursor: pointer;
transition: border-color .2s ease, color .2s ease, background-color .2s ease;
}
.sound:hover { border-color: var(--accent); color: var(--accent); }
.sound:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
@media (max-width: 520px) {
.readouts { gap: 14px; }
.ro { min-width: 70px; }
.kicker { letter-spacing: 0.18em; font-size: 9.5px; }
}
</style>
</head>
<body>
<main class="sheet">
<header>
<p class="kicker">Material Studies <span class="sep">/</span> No. 062 <span class="sep">·</span> WebGL + Web Audio</p>
<h1 class="title">Daybreak Switch.</h1>
<p class="caption">A toggle that drags the dawn across a small room.</p>
</header>
<div class="stage">
<div class="switch" id="switch" role="slider" tabindex="0"
aria-label="Daybreak switch — time of day"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"
aria-valuetext="02:00, night">
<canvas id="gl"></canvas>
</div>
</div>
<section class="readouts">
<div class="ro"><span class="ro-k">Clock</span><span class="ro-v" id="ro-clock">02:00</span></div>
<div class="ro"><span class="ro-k">Light</span><span class="ro-v" id="ro-light">Night</span></div>
<div class="ro"><span class="ro-k">Music</span><span class="ro-v" id="ro-music">Crickets</span></div>
</section>
<p class="hint">Drag the knob, or drag anywhere along the track · tap to toggle to the other end · ← → to nudge · Home / End for deep night and full morning</p>
<button class="sound" id="sound" type="button" aria-pressed="true">Sound on</button>
</main>
<script>
/* ==== pix.js ==== */
/* ============================================================================
pix.js — tiny pixel buffer + sprite stamping engine
Every object in the room is a "filled shape stamped with an automatic 1px
outline and a light top / dark bottom" (see SPEC).
========================================================================== */
const RW = 176; // room pixel width
const RH = 50; // room pixel height
/* --- colour helpers: colours are packed as 0xAABBGGRR for LE Uint32Array --- */
function C(hex, a = 255) {
const h = hex[0] === '#' ? hex.slice(1) : hex;
const r = parseInt(h.slice(0, 2), 16), g = parseInt(h.slice(2, 4), 16), b = parseInt(h.slice(4, 6), 16);
return ((a << 24) | (b << 16) | (g << 8) | r) >>> 0;
}
const cr = c => c & 255, cg = c => (c >> 8) & 255, cb = c => (c >> 16) & 255, ca = c => (c >>> 24) & 255;
const pack = (r, g, b, a = 255) => (((a & 255) << 24) | ((b & 255) << 16) | ((g & 255) << 8) | (r & 255)) >>> 0;
/* amt > 0 lightens toward white, amt < 0 darkens toward black */
function shade(c, amt) {
if (amt >= 0) return pack(Math.round(cr(c) + (255 - cr(c)) * amt), Math.round(cg(c) + (255 - cg(c)) * amt), Math.round(cb(c) + (255 - cb(c)) * amt));
const k = 1 + amt;
return pack(Math.round(cr(c) * k), Math.round(cg(c) * k), Math.round(cb(c) * k));
}
function lerpC(c1, c2, t) {
if (t <= 0) return c1; if (t >= 1) return c2;
return pack(Math.round(cr(c1) + (cr(c2) - cr(c1)) * t), Math.round(cg(c1) + (cg(c2) - cg(c1)) * t), Math.round(cb(c1) + (cb(c2) - cb(c1)) * t));
}
/* blend `tint` into `c` by amount a (0..1) */
function mixT(c, tint, a) {
return pack(Math.round(cr(c) + (cr(tint) - cr(c)) * a), Math.round(cg(c) + (cg(tint) - cg(c)) * a), Math.round(cb(c) + (cb(tint) - cb(c)) * a));
}
/* additive colour */
function addC(c, tint, a) {
return pack(Math.min(255, cr(c) + cr(tint) * a), Math.min(255, cg(c) + cg(tint) * a), Math.min(255, cb(c) + cb(tint) * a));
}
/* --- buffers ------------------------------------------------------------- */
const roomBuf = new Uint32Array(RW * RH); // live frame
const staticBuf = new Uint32Array(RW * RH); // static room furniture
const maskBuf = new Uint8Array(RW * RH); // scratch shape mask
function bufSet(buf, x, y, c) { if (x >= 0 && y >= 0 && x < RW && y < RH) buf[y * RW + x] = c; }
function bufGet(buf, x, y) { return (x >= 0 && y >= 0 && x < RW && y < RH) ? buf[y * RW + x] : 0; }
function bufRect(buf, x, y, w, h, c) {
const x1 = Math.min(RW, x + w), y1 = Math.min(RH, y + h);
for (let yy = Math.max(0, y); yy < y1; yy++) for (let xx = Math.max(0, x); xx < x1; xx++) buf[yy * RW + xx] = c;
}
function bufFill(buf, c) { buf.fill(c); }
/* --- shape predicates ---------------------------------------------------- */
const inRect = (x, y, x0, y0, w, h) => x >= x0 && x < x0 + w && y >= y0 && y < y0 + h;
const inEll = (x, y, cx, cy, rx, ry) => { const dx = (x - cx) / rx, dy = (y - cy) / ry; return dx * dx + dy * dy <= 1; };
function inRoundRect(x, y, x0, y0, w, h, r) {
const x1 = x0 + w - 1, y1 = y0 + h - 1;
if (x < x0 || x > x1 || y < y0 || y > y1) return false;
const cx = Math.min(Math.max(x, x0 + r), x1 - r), cy = Math.min(Math.max(y, y0 + r), y1 - r);
const dx = x - cx, dy = y - cy;
return dx * dx + dy * dy <= r * r + 0.001;
}
function inTri(x, y, ax, ay, bx, by, cx, cy) {
const d1 = (x - bx) * (ay - by) - (ax - bx) * (y - by);
const d2 = (x - cx) * (by - cy) - (bx - cx) * (y - cy);
const d3 = (x - ax) * (cy - ay) - (cx - ax) * (y - ay);
const neg = (d1 < 0) || (d2 < 0) || (d3 < 0), pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
return !(neg && pos);
}
/* vertical capsule: segment x=cx, y from cy0..cy1, radius r */
const inCapsule = (x, y, cx, cy0, cy1, r) => {
const t = Math.min(Math.max(y, cy0), cy1);
const dx = x - cx, dy = y - t;
return dx * dx + dy * dy <= r * r + 0.001;
};
/* thick line */
function inLine(x, y, x0, y0, x1, y1, r) {
const dx = x1 - x0, dy = y1 - y0, l2 = dx * dx + dy * dy;
let t = l2 ? ((x - x0) * dx + (y - y0) * dy) / l2 : 0;
t = Math.min(1, Math.max(0, t));
const px = x0 + t * dx, py = y0 + t * dy;
return (x - px) * (x - px) + (y - py) * (y - py) <= r * r + 0.001;
}
/* --- stamping ------------------------------------------------------------
stamp(buf, inside, bbox, base, opts)
outline : draw 1px outline around the shape (default true)
outlineCol : explicit outline colour
grad : vertical light->dark gradient strength (default .22)
edgeHi/Lo : extra highlight/shade on the true top/bottom edge
----------------------------.........完整代码请登录后点击上方下载按钮下载查看















网友评论0