Skip to content

Instantly share code, notes, and snippets.

@channprj
Created September 12, 2024 23:14
Show Gist options
  • Save channprj/c91ee300ed567c5cf42e0efa7bc68c1b to your computer and use it in GitHub Desktop.
Save channprj/c91ee300ed567c5cf42e0efa7bc68c1b to your computer and use it in GitHub Desktop.
CSS Scroll to Decrypt
<header>
<h1 class="fluid">Scroll to Decrypt</h1>
</header>
<main class="fluid">
<div class="encrypted fluid"></div>
</main>
<footer>ʕ•ᴥ•ʔ @jh3yy &copy; '24</footer>
<a
class="bear-link"
href="https://twitter.com/intent/follow?screen_name=jh3yy"
target="_blank"
rel="noreferrer noopener"
>
<svg
class="w-9"
viewBox="0 0 969 955"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<circle
cx="161.191"
cy="320.191"
r="133.191"
stroke="currentColor"
stroke-width="20"
></circle>
<circle
cx="806.809"
cy="320.191"
r="133.191"
stroke="currentColor"
stroke-width="20"
></circle>
<circle
cx="695.019"
cy="587.733"
r="31.4016"
fill="currentColor"
></circle>
<circle
cx="272.981"
cy="587.733"
r="31.4016"
fill="currentColor"
></circle>
<path
d="M564.388 712.083C564.388 743.994 526.035 779.911 483.372 779.911C440.709 779.911 402.356 743.994 402.356 712.083C402.356 680.173 440.709 664.353 483.372 664.353C526.035 664.353 564.388 680.173 564.388 712.083Z"
fill="currentColor"
></path>
<rect
x="310.42"
y="448.31"
width="343.468"
height="51.4986"
fill="#FF1E1E"
></rect>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M745.643 288.24C815.368 344.185 854.539 432.623 854.539 511.741H614.938V454.652C614.938 433.113 597.477 415.652 575.938 415.652H388.37C366.831 415.652 349.37 433.113 349.37 454.652V511.741L110.949 511.741C110.949 432.623 150.12 344.185 219.845 288.24C289.57 232.295 384.138 200.865 482.744 200.865C581.35 200.865 675.918 232.295 745.643 288.24Z"
fill="currentColor"
></path>
</svg>
</a>
import { Pane } from 'https://cdn.skypack.dev/tweakpane'
import Splitting from 'https://cdn.skypack.dev/splitting'
const heading = document.querySelector('.encrypted')
const config = {
debug: false,
title: 'Pretty cool',
sub: 'Translate clipped text on scroll',
theme: 'system',
track: 10,
start: 1,
end: -0.2,
}
const ctrl = new Pane({
title: 'Config',
expanded: true,
})
const randomString = (length) => {
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+[]{}|;:,.<>?~'
return [...Array(length)]
.map(() => chars[Math.floor(Math.random() * chars.length)])
.join('')
}
// For escaping the special characters for HTML output:
const escapeHTML = (str) =>
str.replace(
/[&<>"']/g,
(char) =>
({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
}[char])
)
const getWords = (txt) => {
const hold = Object.assign(document.createElement('div'), {
innerHTML: Splitting.html({ content: txt, whitespace: true }),
})
const chars = hold.querySelectorAll('.char, .whitespace')
hold.firstChild.setAttribute('aria-hidden', 'true')
for (let c = 0; c < chars.length; c++) {
const char = chars[c]
char.dataset.char = char.innerHTML
char.innerHTML = `<span>${char.innerHTML}${escapeHTML(
randomString(config.track)
)}</span>`
}
return hold.innerHTML
}
const update = () => {
heading.innerHTML = `
<h1>
<span class="sr-only">${config.title}</span>
${getWords(config.title)}
</h1>
<p>
<span class="sr-only">${config.sub}</span>
${getWords(config.sub)}
</p>
`
document.documentElement.dataset.theme = config.theme
document.documentElement.dataset.debug = config.debug
document.documentElement.style.setProperty('--track', config.track)
document.documentElement.style.setProperty('--start', config.start)
document.documentElement.style.setProperty('--end', config.end)
}
const sync = (event) => {
if (
!document.startViewTransition ||
event.target.controller.view.labelElement.innerText !== 'Theme'
)
return update()
document.startViewTransition(() => update())
}
ctrl.addBinding(config, 'title', {
label: 'Text',
})
ctrl.addBinding(config, 'sub', {
label: 'Sub',
})
ctrl.addBinding(config, 'track', {
min: 2,
max: 50,
step: 1,
label: 'Chars',
})
const range = ctrl.addFolder({ title: 'Range Step' })
range.addBinding(config, 'start', {
min: -10,
max: 10,
step: 0.01,
label: 'Start',
})
range.addBinding(config, 'end', {
min: -10,
max: 10,
step: 0.01,
label: 'End',
})
ctrl.addBinding(config, 'debug', {
label: 'Debug',
})
ctrl.addBinding(config, 'theme', {
label: 'Theme',
options: {
System: 'system',
Light: 'light',
Dark: 'dark',
},
})
ctrl.on('change', sync)
update()
@import url('https://unpkg.com/normalize.css') layer(normalize);
@font-face {
font-family: 'Departure Mono';
src: url('https://assets.codepen.io/605876/DepartureMono-Regular.woff');
}
@layer normalize, base, demo;
@layer demo {
header,
main {
--font-level: 3.5;
min-height: 100vh;
display: grid;
place-items: center;
font-family: 'Departure Mono', monospace;
text-transform: uppercase;
}
footer {
font-family: 'Departure Mono', monospace;
text-transform: uppercase;
padding: 1rem;
}
[data-debug='true'] [data-char] {
outline: 2px dashed hotpink;
overflow: visible;
}
[data-debug='true'] h1 {
position: fixed;
top: 50%;
translate: 0 -50%;
}
[data-debug='true'] :where(header, footer, p) {
opacity: 0;
}
.encrypted {
--step-start: calc(var(--start, 0) * 1%);
--step-end: calc(var(--end, -0.2) * 1%);
view-timeline: --heading;
width: 30ch;
max-width: calc(100vw - 2rem);
}
p {
margin: 0;
opacity: 0.8;
line-height: 1;
}
[data-char] {
width: 1ch;
height: 1lh;
overflow: hidden;
position: relative;
display: inline-block;
}
[data-char] span {
position: absolute;
width: 1ch;
translate: -50% 0;
word-break: break-word;
white-space: break-spaces;
bottom: 0%;
left: 50%;
animation: track both steps(var(--track, 10));
animation-timeline: --heading;
animation-range: contain calc(0% + (var(--char-index) * var(--step-start)))
calc(50% + (var(--char-index) * var(--step-end)));
}
.word {
white-space: nowrap;
}
h1 {
line-height: 1;
margin: 0;
}
@keyframes track {
to {
translate: -50% calc(100% - 1lh);
}
}
}
@layer base {
:root {
--font-size-min: 16;
--font-size-max: 20;
--font-ratio-min: 1.2;
--font-ratio-max: 1.33;
--font-width-min: 375;
--font-width-max: 1500;
}
html {
color-scheme: light dark;
}
[data-theme='light'] {
color-scheme: light only;
}
[data-theme='dark'] {
color-scheme: dark only;
}
:where(.fluid *),
:where(.fluid) {
--fluid-min: calc(
var(--font-size-min) * pow(var(--font-ratio-min), var(--font-level, 0))
);
--fluid-max: calc(
var(--font-size-max) * pow(var(--font-ratio-max), var(--font-level, 0))
);
--fluid-preferred: calc(
(var(--fluid-max) - var(--fluid-min)) /
(var(--font-width-max) - var(--font-width-min))
);
--fluid-type: clamp(
(var(--fluid-min) / 16) * 1rem,
((var(--fluid-min) / 16) * 1rem) -
(((var(--fluid-preferred) * var(--font-width-min)) / 16) * 1rem) +
(var(--fluid-preferred) * var(--variable-unit, 100vi)),
(var(--fluid-max) / 16) * 1rem
);
font-size: var(--fluid-type);
}
*,
*:after,
*:before {
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
min-height: 100vh;
font-family: 'SF Pro Text', 'SF Pro Icons', 'AOS Icons', 'Helvetica Neue',
Helvetica, Arial, sans-serif, system-ui;
}
body::before {
--size: 45px;
--line: color-mix(in lch, canvasText, transparent 70%);
content: '';
height: 100vh;
width: 100vw;
position: fixed;
background: linear-gradient(
90deg,
var(--line) 1px,
transparent 1px var(--size)
)
50% 50% / var(--size) var(--size),
linear-gradient(var(--line) 1px, transparent 1px var(--size)) 50% 50% /
var(--size) var(--size);
mask: linear-gradient(-20deg, transparent 50%, white);
top: 0;
transform-style: flat;
pointer-events: none;
z-index: -1;
}
.bear-link {
color: canvasText;
position: fixed;
top: 1rem;
left: 1rem;
width: 48px;
aspect-ratio: 1;
display: grid;
place-items: center;
opacity: 0.8;
}
:where(.x-link, .bear-link):is(:hover, :focus-visible) {
opacity: 1;
}
.bear-link svg {
width: 75%;
}
/* Utilities */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
}
div.tp-dfwv {
position: fixed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment