|
| 1 | +# DepthText |
| 2 | + |
| 3 | +DepthText is a lightweight, dependency-free JavaScript library that creates smooth multi-layer 3D text with depth, parallax, and interactive rotation. |
| 4 | +Designed for high performance, accessibility, and modern UX motion guidelines. |
| 5 | + |
| 6 | +It is the spiritual successor of ztext.js, but rewritten from scratch with a cleaner API, better performance, and a modern ES module architecture. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## ✨ Features |
| 11 | + |
| 12 | +- 🌀 **True 3D layered depth** using CSS `transform-style: preserve-3d` |
| 13 | +- 🖱️ **Interactive rotation** (auto, pointer, scroll…) |
| 14 | +- 🎚️ **Configurable number of layers** |
| 15 | +- 🎨 Optional **fade effect** across layers |
| 16 | +- ⚡ **GPU-optimized** and jank-free |
| 17 | +- 🦾 **Accessibility-friendly** (`aria-hidden`, reduced-motion support) |
| 18 | +- 🔥 **No dependencies**, only 4–6 KB minified |
| 19 | +- 📦 Works with bundlers, ES modules, and browsers |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🛠 Installation |
| 24 | + |
| 25 | +### NPM |
| 26 | + |
| 27 | +```bash |
| 28 | +npm install depthtext |
| 29 | +``` |
| 30 | + |
| 31 | +### Import (ES module) |
| 32 | + |
| 33 | +```js |
| 34 | +import { DepthTextify } from "depthtext"; |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 🚀 Quick Start |
| 40 | + |
| 41 | +### HTML |
| 42 | + |
| 43 | +```html |
| 44 | +<h1 class="depthtext" data-depth="4" data-depth-event="pointer">DepthText Rocks</h1> |
| 45 | +``` |
| 46 | + |
| 47 | +### JavaScript |
| 48 | + |
| 49 | +```js |
| 50 | +import { DepthTextify } from "depthtext"; |
| 51 | + |
| 52 | +DepthTextify(); // Enhances all .depthtext elements |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## 🧩 Options |
| 58 | + |
| 59 | +DepthText can be configured using either: |
| 60 | + |
| 61 | +- **HTML data attributes** |
| 62 | +- or **JavaScript options** |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +### HTML Attributes |
| 67 | + |
| 68 | +| Attribute | Type | Default | Description | |
| 69 | +| --------------------------- | ------- | ------- | ----------------------------------------------------- | |
| 70 | +| `data-depth` | number | `8` | Number of 3D layers | |
| 71 | +| `data-depth-depth` | number | `20` | Z distance between layers | |
| 72 | +| `data-depth-event` | string | `none` | Interaction type: `none`, `pointer`, `scroll`, `auto` | |
| 73 | +| `data-depth-direction` | number | `1` | Reverse or invert orientation | |
| 74 | +| `data-depth-fade` | boolean | `false` | Fade layers as depth increases | |
| 75 | +| `data-depth-eventdirection` | number | `1` | Invert pointer/scroll direction | |
| 76 | +| `data-depth-eventrotation` | number | `20` | Max rotation angle on interaction | |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +### JavaScript API |
| 81 | + |
| 82 | +```js |
| 83 | +import { DepthTextInstance } from "depthtext"; |
| 84 | + |
| 85 | +const instance = new DepthTextInstance(element, { |
| 86 | + layers: 8, |
| 87 | + depth: 20, |
| 88 | + fade: false, |
| 89 | + event: "pointer", |
| 90 | + eventRotation: 20, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## 🌐 Automatic Initialization |
| 97 | + |
| 98 | +If you want DepthText to automatically enhance all matching elements: |
| 99 | + |
| 100 | +```js |
| 101 | +import { DepthTextify } from "depthtext"; |
| 102 | + |
| 103 | +DepthTextify({ |
| 104 | + selector: ".depthtext", |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## 🧼 Accessibility |
| 111 | + |
| 112 | +DepthText is designed to remain invisible to assistive technologies: |
| 113 | + |
| 114 | +- Wrappers are automatically marked with `aria-hidden="true"` |
| 115 | +- Motion reacts to `prefers-reduced-motion: reduce` |
| 116 | +- Original text remains intact and readable in source |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## 🎛 Custom Styling |
| 121 | + |
| 122 | +Each generated depth layer receives: |
| 123 | + |
| 124 | +```html |
| 125 | +<span class="depthtext-layer"></span> |
| 126 | +``` |
| 127 | + |
| 128 | +You can style them globally: |
| 129 | + |
| 130 | +```css |
| 131 | +.depthtext-layer { |
| 132 | + color: #d0d0d0; |
| 133 | +} |
| 134 | +.depthtext-layer:first-child { |
| 135 | + color: #000; |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## 📦 Browser Usage (no bundler) |
| 142 | + |
| 143 | +```html |
| 144 | +<script src="dist/depthtext.global.js"></script> |
| 145 | +<script> |
| 146 | + // DepthText is available globally |
| 147 | + DepthText.DepthTextify(); |
| 148 | +</script> |
| 149 | +``` |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## ⚛️ Framework Integration |
| 154 | + |
| 155 | +DepthText is framework-agnostic. You can use it with React, Vue, Angular, Svelte, etc., by instantiating `DepthTextInstance` on a mounted DOM element. |
| 156 | + |
| 157 | +### React / Next.js |
| 158 | + |
| 159 | +```jsx |
| 160 | +import { useEffect, useRef } from 'react'; |
| 161 | +import { DepthTextInstance } from 'depthtext'; |
| 162 | + |
| 163 | +export default function DepthComponent() { |
| 164 | + const textRef = useRef(null); |
| 165 | + |
| 166 | + useEffect(() => { |
| 167 | + if (!textRef.current) return; |
| 168 | + |
| 169 | + const dt = new DepthTextInstance(textRef.current, { |
| 170 | + layers: 10, |
| 171 | + depth: "1rem", |
| 172 | + event: "pointer" |
| 173 | + }); |
| 174 | + |
| 175 | + // Cleanup on unmount |
| 176 | + return () => dt.destroy(); |
| 177 | + }, []); |
| 178 | + |
| 179 | + return <h1 ref={textRef}>DepthText in React</h1>; |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### Vue 3 |
| 184 | + |
| 185 | +```html |
| 186 | +<script setup> |
| 187 | +import { onMounted, onUnmounted, ref } from 'vue'; |
| 188 | +import { DepthTextInstance } from 'depthtext'; |
| 189 | +
|
| 190 | +const textRef = ref(null); |
| 191 | +let dt = null; |
| 192 | +
|
| 193 | +onMounted(() => { |
| 194 | + if (textRef.value) { |
| 195 | + dt = new DepthTextInstance(textRef.value, { |
| 196 | + layers: 10, |
| 197 | + event: 'pointer' |
| 198 | + }); |
| 199 | + } |
| 200 | +}); |
| 201 | +
|
| 202 | +onUnmounted(() => { |
| 203 | + if (dt) dt.destroy(); |
| 204 | +}); |
| 205 | +</script> |
| 206 | + |
| 207 | +<template> |
| 208 | + <h1 ref="textRef">DepthText in Vue</h1> |
| 209 | +</template> |
| 210 | +``` |
| 211 | + |
| 212 | +### Angular |
| 213 | + |
| 214 | +```ts |
| 215 | +import { Component, ElementRef, ViewChild, AfterViewInit, OnDestroy } from '@angular/core'; |
| 216 | +import { DepthTextInstance } from 'depthtext'; |
| 217 | + |
| 218 | +@Component({ |
| 219 | + selector: 'app-depth-text', |
| 220 | + template: '<h1 #depthText>DepthText in Angular</h1>' |
| 221 | +}) |
| 222 | +export class DepthTextComponent implements AfterViewInit, OnDestroy { |
| 223 | + @ViewChild('depthText') textRef!: ElementRef; |
| 224 | + private dt?: DepthTextInstance; |
| 225 | + |
| 226 | + ngAfterViewInit() { |
| 227 | + this.dt = new DepthTextInstance(this.textRef.nativeElement, { |
| 228 | + layers: 10, |
| 229 | + event: 'pointer' |
| 230 | + }); |
| 231 | + } |
| 232 | + |
| 233 | + ngOnDestroy() { |
| 234 | + this.dt?.destroy(); |
| 235 | + } |
| 236 | +} |
| 237 | +``` |
| 238 | + |
| 239 | +## 🐛 Known Issues & Notes |
| 240 | + |
| 241 | +- DepthText uses CSS transforms; parent elements must not flatten 3D contexts. |
| 242 | +- Avoid nested DepthText unless you understand `transform-style: preserve-3d`. |
| 243 | + |
| 244 | +--- |
| 245 | + |
| 246 | +## 🧑💻 Contributing |
| 247 | + |
| 248 | +Pull requests are welcome! |
| 249 | +If you add a major feature, please include documentation updates. |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +## 📄 License |
| 254 | + |
| 255 | +MIT License — free to use in commercial and open-source projects. |
| 256 | + |
| 257 | +--- |
| 258 | + |
| 259 | +## 💬 Author |
| 260 | + |
| 261 | +Created by **MobiWise**. |
| 262 | +A modern reimagining of the classic [ztext.js](https://github.com/bennettfeely/ztext). |
0 commit comments