Skip to content

Instantly share code, notes, and snippets.

@IcodeAlpha
Created June 3, 2025 20:20
Show Gist options
  • Save IcodeAlpha/c2128fe20600db6c834c6fa386a18499 to your computer and use it in GitHub Desktop.
Save IcodeAlpha/c2128fe20600db6c834c6fa386a18499 to your computer and use it in GitHub Desktop.
3DSplitFlap
<div class="board"></div>
<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@4.0.4'
import gsap from 'https://cdn.skypack.dev/gsap@3.12.0'
gsap.defaults({
duration: 1,
ease: 'none',
})
const flips = {}
const config = {
theme: 'dark',
perspective: 1,
length: 10,
characters: 'abcdefghijklmnopqrstuvwxyz!',
}
const ctrl = new Pane({
title: 'Config',
expanded: true,
})
const update = () => {
document.documentElement.dataset.theme = config.theme
document.documentElement.dataset.explode = config.explode
document.documentElement.style.setProperty(
'--perspective',
config.perspective
)
}
const sync = (event) => {
if (
!document.startViewTransition ||
event.target.controller.view.labelElement.innerText !== 'Theme'
)
return update()
document.startViewTransition(() => update())
}
ctrl.addBinding(config, 'perspective', {
min: 0.5,
max: 4,
step: 0.1,
label: 'perspective',
})
ctrl
.addBinding(config, 'length', {
min: 4,
max: 20,
step: 1,
label: 'length',
})
.on('change', () => {
for (const flip of Object.values(flips))
flip.flipper.lineLength = config.length
})
ctrl.addBinding(config, 'theme', {
label: 'theme',
options: {
system: 'system',
light: 'light',
dark: 'dark',
},
})
ctrl.on('change', sync)
update()
const DEFAULT_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz'
class FlipSlot {
constructor(options = {}) {
const {
characters = DEFAULT_CHARACTERS,
color = 'canvasText',
pad = 0,
} = options
this.characters = Array.from(` ${characters} `)
this.colorSet = color
this.pad = pad
this.element = this.create()
this.generateTimeline()
}
set chars(value) {
this.characters = Array.from(` ${value} `)
this.generateTimeline()
}
set color(value) {
this.element?.style.setProperty('--color', value)
}
create() {
const element = Object.assign(document.createElement('div'), {
className: 'flip',
style: `--color: ${this.colorSet}`,
innerHTML: `
<!-- fold top -->
<div></div>
<!-- fold bottom -->
<div></div>
<!-- unfold top -->
<div></div>
<!-- unfold bottom -->
<div></div>
`,
})
return element
}
flip(character, delay = 0) {
// const chars = Array.from(` ${config.characters} `)
// const desired = config.character
const { characters: chars, pad, timeline, scrubber } = this
const currentIndex = chars.indexOf(chars[timeline.totalTime()])
const desiredIndex =
chars.indexOf(character) !== -1 ? chars.indexOf(character) : 0
// if the current index is greater, loop around
// we seem to have to add an extra 0.5 to make up for gaps
const shift =
currentIndex > desiredIndex
? chars.length - 1 - currentIndex + desiredIndex
: desiredIndex - currentIndex
// this is how you throw an extra loop in for the stagger
const padding = currentIndex === desiredIndex ? 0 : pad * (chars.length - 1)
gsap.to(scrubber, {
delay,
totalTime: `+=${shift + padding}`,
ease: 'power1.out',
duration: (shift + padding) * gsap.utils.random(0.02, 0.06),
})
}
generateTimeline() {
const { timeline: currentTimeline, scrubber, element } = this
if (currentTimeline) currentTimeline.kill()
if (scrubber) this.scrubber.kill()
const [unfoldTop, unfoldBottom, foldTop, foldBottom] = Array.from(
element.querySelectorAll('& > div')
)
const chars = this.characters
gsap.set([foldTop, unfoldBottom], { clearProps: 'all' })
unfoldTop.innerText = unfoldBottom.innerText = chars[1]
foldTop.innerText = foldBottom.innerText = chars[0]
const timeline = gsap
.timeline({
paused: true,
// account for the extra space
repeat: chars.length - 2,
onRepeat: () => {
const index = Math.floor(timeline.totalTime() / timeline.duration())
const next = chars[index % chars.length]
const current = chars[(index + 1) % chars.length]
unfoldTop.innerText = unfoldBottom.innerText = current
foldTop.innerText = foldBottom.innerText = next
},
})
.fromTo(
unfoldBottom,
{ rotateX: 180 },
{
rotateX: 0,
duration: 1,
},
0
)
.fromTo(
unfoldTop,
{ filter: 'brightness(0)' },
{
filter: 'brightness(1)',
duration: 1,
},
0
)
.fromTo(
foldTop,
{ rotateX: 0 },
{
duration: 1,
rotateX: -180,
},
0
)
.fromTo(
foldBottom,
{ filter: 'brightness(1)' },
{
duration: 1,
filter: 'brightness(0)',
},
0
)
const duration = timeline.totalDuration()
this.scrubber = gsap.to(timeline, {
totalTime: duration,
repeat: -1,
paused: true,
duration: duration,
ease: 'none',
})
this.scrubber.time(timeline.totalDuration())
this.timeline = timeline
}
}
class FlipLine {
constructor(options = {}) {
const { color, length = 10, pad = 0 } = options
this.colorSet = color
this.length = length
this.padding = pad
this.options = options
this.setup()
}
setup() {
const { colorSet, length, padding } = this
if (this.element) {
this.element.innerHTML = ''
} else {
this.element = Object.assign(document.createElement('div'), {
className: 'flip-line',
})
}
this.flips = []
for (let i = 0; i < length; i++) {
const newSlot = new FlipSlot({
pad: padding,
characters: config.characters,
color: colorSet,
})
this.element.appendChild(newSlot.element)
this.flips.push(newSlot)
}
}
set lineLength(value) {
this.length = value
this.setup()
}
set pad(value) {
const { flips } = this
if (flips) {
for (let i = 0; i < flips.length; i++) flips[i].pad = value
}
}
set color(value) {
const { flips } = this
this.colorSet = value
if (flips) {
for (let i = 0; i < flips.length; i++) flips[i].color = value
}
}
run(update) {
const letters = Array.from(update.padEnd(10, ' '))
for (let i = 0; i < Math.min(letters.length, this.length); i++) {
this.flips[i]?.flip(letters[i], i / 10)
}
}
}
const board = document.querySelector('.board')
const addLine = ({
text = '',
pad = 1,
color = 'hsl(0,0%,90%)',
alignment = 'left',
}) => {
const lineConfig = {
text,
length: 10,
pad,
color: color,
characters: DEFAULT_CHARACTERS,
alignment,
id: crypto.randomUUID(),
}
const newLine = new FlipLine({
length: lineConfig.line,
color: color,
pad,
})
board.appendChild(newLine.element)
newLine.run(
lineConfig.alignment === 'right'
? lineConfig.text.toLowerCase().padStart(10, ' ')
: lineConfig.text.toLowerCase()
)
flips[lineConfig.id] = {
config: lineConfig,
flipper: newLine,
}
const lineFolder = ctrl.addFolder({
title: 'Line',
expanded: false,
})
lineFolder.addBinding(lineConfig, 'text', {
label: 'text',
})
lineFolder
.addBinding(lineConfig, 'characters', {
label: 'characters',
})
.on('change', () => {
for (const flip of newLine.flips) {
flip.chars = lineConfig.characters
}
})
lineFolder
.addBinding(lineConfig, 'pad', {
label: 'pad',
min: 0,
max: 4,
step: 1,
})
.on('change', () => {
newLine.pad = lineConfig.pad
})
lineFolder.addBinding(lineConfig, 'alignment', {
options: {
left: 'left',
right: 'right',
},
})
lineFolder
.addBinding(lineConfig, 'color', {
view: 'color',
color: { alpha: true },
})
.on('change', () => {
newLine.color = lineConfig.color
})
// create a flipper
lineFolder.addButton({ title: 'Run' }).on('click', () => {
newLine.run(
lineConfig.alignment === 'right'
? lineConfig.text.toLowerCase().padStart(10, ' ')
: lineConfig.text.toLowerCase()
)
})
lineFolder.addButton({ title: 'Remove' }).on('click', () => {
// must delete the item from flippers
delete flips[lineConfig.id]
ctrl.remove(lineFolder)
newLine.element.remove()
})
}
ctrl.addButton({ title: 'Play' }).on('click', () => {
for (const line of Object.values(flips)) {
line.flipper.run(
line.config.alignment === 'right'
? line.config.text.toLowerCase().padStart(10, ' ')
: line.config.text.toLowerCase()
)
}
})
ctrl.addButton({ title: 'Blank' }).on('click', () => {
for (const line of Object.values(flips)) {
line.flipper.run('')
}
})
ctrl.addButton({ title: 'Add Line' }).on('click', addLine)
// addLine({
// text: 'You can',
// pad: 1,
// alignment: 'right',
// })
// addLine({
// text: 'just ship',
// pad: 2,
// alignment: 'right',
// })
// addLine({
// text: 'things',
// pad: 3,
// alignment: 'right',
// })
// addLine({
// text: 'on time',
// pad: 4,
// alignment: 'right',
// color: 'hsl(44,82%,49%)',
// })
addLine({
text: 'Babe!',
pad: 1,
alignment: 'left',
})
addLine({
text: 'new craft',
pad: 2,
alignment: 'right',
})
addLine({
text: 'of ui',
pad: 3,
alignment: 'right',
})
addLine({
text: 'dropped',
pad: 4,
alignment: 'right',
color: 'hsl(44,82%,49%)',
})
@import url('https://unpkg.com/normalize.css') layer(normalize);
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap');
@layer normalize, base, demo, flip;
@layer flip {
.flip > div:nth-of-type(2) {
z-index: 2;
transform: rotateX(-180deg);
backface-visibility: hidden;
}
.flip div:nth-of-type(3) {
z-index: 3;
backface-visibility: hidden;
}
}
@layer demo {
body {
align-content: center;
gap: 2rem;
}
.board {
display: flex;
gap: 0.5ch;
flex-direction: column;
text-transform: uppercase;
font-family: 'Roboto Mono', monospace;
padding: 6px;
background: light-dark(hsl(0 0% 40%), hsl(0 0% 0%));
}
.flip-line {
display: flex;
gap: 0.5ch;
}
.flip {
--font-level: 8;
font-size: 2.5rem;
color: var(--color, canvasText);
width: 1.5ch;
line-height: 1.2;
display: inline-block;
height: 1lh;
text-align: center;
position: relative;
transform-style: preserve-3d;
perspective: calc(var(--perspective) * 1em);
}
.flip::before,
.flip::after {
content: '';
position: absolute;
width: calc(10% - 0.05em);
height: calc(20% - 0.05em);
background: color-mix(in oklch, canvas, canvasText 15%);
top: 50%;
translate: 0 -50%;
}
.flip::before {
left: 0;
}
.flip::after {
right: 0;
}
.flip .flip-holder {
position: absolute;
inset: 0;
transform-style: preserve-3d;
}
.flip div {
position: absolute;
transform-style: preserve-3d;
position: absolute;
overflow: hidden;
/* border-radius: 6px; */
inset: 0;
background: radial-gradient(
100% 100% at 50% 100%,
light-dark(hsl(0 0% 88% / 0.2), hsl(0 0% 60% / 0.2)),
#0000
),
light-dark(hsl(0 0% 92%), hsl(0 0% 15%));
}
.flip div:nth-of-type(odd) {
clip-path: polygon(
0 0,
100% 0,
100% 40%,
calc(90% + 0.025em) 40%,
calc(90% + 0.025em) 48%,
calc(10% - 0.025em) 48%,
calc(10% - 0.025em) 40%,
0 40%
);
}
.flip div:nth-of-type(even) {
clip-path: polygon(
0 60%,
calc(10% - 0.025em) 60%,
calc(10% - 0.025em) 52%,
calc(90% + 0.025em) 52%,
calc(90% + 0.025em) 60%,
100% 60%,
100% 100%,
0 100%
);
}
}
@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) {
--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 {
background: light-dark(#fff, #000);
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 {
width: 280px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment