3d Triangles
Home
Snippets
3d Triangles
HTML
CSS
JS
<input type="checkbox" id="toggle" /> <label for="toggle">Pause</label> <div class="scene"> <div class="octahedron" id="octahedron"> <div class="face"></div> <div class="face"></div> <div class="face"></div> <div class="face"></div> <div class="face"></div> <div class="face"></div> <div class="face"></div> <div class="face"></div> </div> </div>
* { margin: 0; padding: 0; box-sizing: border-box; } body { background: black; color: white; font-family: sans-serif; } .scene { width: 100vw; height: 100vh; display: flex; justify-content: center; align-items: center; perspective: 1600px; } .scene * { position: absolute; transform-style: preserve-3d; } .octahedron { position: relative; display: flex; justify-content: center; align-items: center; animation: rotatearound 40s linear infinite; } @keyframes rotatearound { to { transform: rotateY(3turn) rotateX(-1turn); } } .face { --size: min(60vmin, 20rem); width: var(--size); height: var(--size); clip-path: polygon( 0% 100%, 100% 100%, 50% calc(100% - 0.866 * 100%), 0% 100% ); transform-origin: bottom; } input { position: absolute; left: -100vw; width: 1px; height: 1px; } label { z-index: 10; position: fixed; top: 1rem; left: 1rem; cursor: pointer; color: #ddd; padding: 0.4rem 0.8rem; font-size: 0.875rem; border-radius: 0.25rem; background-color: #222; } label:hover, label:focus-visible { color: #fff; background-color: #555; } input:focus-visible + label { outline: 2px solid white; } input:checked ~ .scene .octahedron { animation-play-state: paused; }
const colors = [ 'red', 'forestgreen', 'white', 'purple', 'gold', 'deeppink', 'darkorange', 'dodgerblue' ]; const style = document.createElement('style'); document.head.appendChild(style); const angle = 'calc(90deg - acos(1 / sqrt(3)))'; colors.forEach((color, i) => { const index = i + 1; const scaleY = index <= 4 ? 1 : -1; const delay = index <= 4 ? '0s' : '4s'; const rotation = `${(index - 1) * 90}deg`; style.sheet.insertRule(` .face:nth-child(${index}) { background-color: ${color}; opacity: 0.5; animation: build-${index} 3s ease-in-out ${delay} forwards; transform: translateY(calc(-0.5 * var(--size))) rotateY(${rotation}) translateZ(calc(0.5 * var(--size))) scaleY(0); } `, style.sheet.cssRules.length); style.sheet.insertRule(` @keyframes build-${index} { 50% { opacity: 1; transform: translateY(calc(-0.5 * var(--size))) rotateY(${rotation}) translateZ(calc(0.5 * var(--size))) scaleY(${scaleY}); } 100% { opacity: 0.9; transform: translateY(calc(-0.5 * var(--size))) rotateY(${rotation}) translateZ(calc(0.5 * var(--size))) scaleY(${scaleY}) rotateX(${angle}); } } `, style.sheet.cssRules.length); });
Ad #1
Ad #2
Scroll to Top