|
| 1 | +# FrameRateHelper.js |
| 2 | + |
| 3 | +**FrameRateHelper** is a lightweight, zero-dependency JavaScript utility that calculates the user's display refresh rate and provides a stable, clamped frame duration. It is ideal for synchronizing animations with screen refresh rates for smoother UI interactions. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🔍 Detects real screen refresh rate using `requestAnimationFrame` |
| 8 | +- 🧠 Includes fallback to `requestIdleCallback` and `setTimeout` |
| 9 | +- 🧱 Clamp logic prevents frame duration spikes on low-power or inactive tabs |
| 10 | +- 💡 Asynchronous, event-driven design |
| 11 | +- 🪶 No dependencies, pure vanilla JS |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +### ✅ CDN |
| 18 | +Use directly in HTML via jsDelivr: |
| 19 | + |
| 20 | +```html |
| 21 | +<script src="https://cdn.jsdelivr.net/gh/yourusername/FrameRateHelper@1.0.0/dist/FrameRateHelper.js"></script> |
| 22 | +``` |
| 23 | + |
| 24 | +Or via unpkg: |
| 25 | + |
| 26 | +```html |
| 27 | +<script src="https://unpkg.com/framerate-helper@1.0.0/dist/FrameRateHelper.js"></script> |
| 28 | +``` |
| 29 | + |
| 30 | +### ✅ NPM (for modern build environments) |
| 31 | +```bash |
| 32 | +npm install framerate-helper |
| 33 | +``` |
| 34 | + |
| 35 | +Then in your module: |
| 36 | +```js |
| 37 | +import FrameRateHelper from 'framerate-helper'; |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### Basic Example |
| 45 | +```js |
| 46 | +const fps = new FrameRateHelper(); |
| 47 | + |
| 48 | +fps.onReady((hz) => { |
| 49 | + console.log('Detected refresh rate:', hz.toFixed(2), 'Hz'); |
| 50 | + console.log('Adjusted duration:', fps.getDuration(300), 'ms'); |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## API |
| 57 | + |
| 58 | +### `new FrameRateHelper()` |
| 59 | +Creates an instance and begins measuring refresh rate immediately. |
| 60 | + |
| 61 | +### `onReady(callback)` |
| 62 | +Registers a callback to be called when the refresh rate is determined. |
| 63 | + |
| 64 | +**Parameters:** |
| 65 | +- `callback (function)`: Function that receives the detected refresh rate in Hz. |
| 66 | + |
| 67 | +### `getDuration(offset = 0)` |
| 68 | +Returns a calculated duration adjusted for the screen's refresh rate. |
| 69 | + |
| 70 | +**Parameters:** |
| 71 | +- `offset (number)`: Optional additional duration in milliseconds. |
| 72 | + |
| 73 | +**Returns:** |
| 74 | +- `number`: Total adjusted duration in ms |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Use Case |
| 79 | + |
| 80 | +Perfect for developers building custom sliders, carousels, animation loops, or precision-based timing utilities where frame-perfect sync matters. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## License |
| 85 | + |
| 86 | +MIT License |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## Author |
| 91 | + |
| 92 | +Developed by **[INFINITUM FORM®](https://infinitumform.com)** |
| 93 | +Author: [Ivijan-Stefan Stipić](https://www.linkedin.com/in/ivijanstefanstipic/) |
| 94 | +Copyright © 2025 Ivijan-Stefan Stipić. All rights reserved. |
0 commit comments