Skip to content

Commit 5b70bb6

Browse files
committed
Version 1.0.1
1 parent 3136560 commit 5b70bb6

7 files changed

Lines changed: 320 additions & 56 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
.DS_Store
3+
*.log
4+
rollup.config.js
5+
package-lock.json

README.md

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
# FrameRateHelper.js
22

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.
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 to produce smooth, consistent visual experiences.
44

5-
## Features
5+
---
6+
7+
## 🚀 Features
68

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
9+
- 🔍 Automatically detects true screen refresh rate using `requestAnimationFrame`
10+
- 🧠 Falls back to `requestIdleCallback` or `setTimeout` when needed
11+
- 🧱 Built-in clamping prevents duration spikes on slow devices or inactive tabs
12+
- 📦 Offers methods for calculating precise animation timing
13+
- 📐 Frame-based timing with optional min/max/rounding controls
14+
- 🪶 Lightweight and dependency-free — pure vanilla JavaScript
1215

1316
---
1417

15-
## Installation
18+
## 📦 Installation
1619

17-
### ✅ CDN
18-
Use directly in HTML via jsDelivr:
20+
### ✅ Use via CDN (for direct browser usage)
1921

22+
**jsDelivr:**
2023
```html
21-
<script src="https://cdn.jsdelivr.net/gh/InfinitumForm/FrameRateHelper@v1.0.0/dist/FrameRateHelper.js"></script>
24+
<script src="https://cdn.jsdelivr.net/gh/InfinitumForm/FrameRateHelper@v1.0.1/dist/FrameRateHelper.js"></script>
2225
```
2326

24-
Or via unpkg:
25-
27+
**unpkg:**
2628
```html
27-
<script src="https://unpkg.com/framerate-helper@1.0.0/dist/FrameRateHelper.js"></script>
29+
<script src="https://unpkg.com/framerate-helper@1.0.1/dist/FrameRateHelper.js"></script>
2830
```
2931

30-
### ✅ NPM (for modern build environments)
32+
This exposes `window.FrameRateHelper` globally.
33+
34+
### ✅ Use via NPM (modern JavaScript projects)
35+
3136
```bash
3237
npm install framerate-helper
3338
```
@@ -39,56 +44,99 @@ import FrameRateHelper from 'framerate-helper';
3944

4045
---
4146

42-
## Usage
47+
## 🧪 Usage Tutorial
4348

44-
### Basic Example
49+
### 1. Basic Setup (auto-detect refresh rate)
4550
```js
4651
const fps = new FrameRateHelper();
4752

4853
fps.onReady((hz) => {
4954
console.log('Detected refresh rate:', hz.toFixed(2), 'Hz');
50-
console.log('Adjusted duration:', fps.getDuration(300), 'ms');
55+
console.log('Estimated frame duration:', fps.getDuration(), 'ms');
5156
});
5257
```
5358

59+
### 2. Get adjusted duration for animations
60+
```js
61+
const duration = fps.getDuration(300); // base + 300ms offset
62+
```
63+
64+
### 3. Calculate animation duration from frame count
65+
```js
66+
const duration = fps.getDurationForFrames(90); // Duration for 90 frames
67+
```
68+
69+
### 4. Clamp duration with min/max values
70+
```js
71+
const duration = fps.getDurationForFrames(90, {
72+
min: 1000, // minimum 1 second
73+
max: 2000, // maximum 2 seconds
74+
rounded: true // round to nearest integer
75+
});
76+
```
77+
78+
### 5. Animate based on desired frames with fallback
79+
```js
80+
const desiredFrames = 120;
81+
let duration = fps.getDurationForFrames(desiredFrames, {
82+
max: 2500, // prevent overly long animations
83+
min: 800, // ensure minimum visibility
84+
rounded: true
85+
});
86+
87+
myElement.style.transitionDuration = `${duration}ms`;
88+
```
89+
5490
---
5591

56-
## API
92+
## 🧰 API Reference
5793

5894
### `new FrameRateHelper()`
59-
Creates an instance and begins measuring refresh rate immediately.
95+
Creates a new instance and begins refresh rate measurement immediately.
6096

6197
### `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.
98+
Waits for refresh rate calculation to complete and then executes the callback.
99+
- `callback (function)`: Receives detected refresh rate (Hz)
66100

67101
### `getDuration(offset = 0)`
68-
Returns a calculated duration adjusted for the screen's refresh rate.
102+
Returns an adjusted frame duration based on screen refresh rate.
103+
- `offset (number)`: Optional duration to add (in ms)
104+
- **Returns:** total duration in ms
105+
106+
### `getDurationForFrames(frames, options)`
107+
Calculates duration based on number of frames.
108+
- `frames (number)`: Desired number of animation frames
109+
- `options (object)` (optional):
110+
- `min (number)`: Minimum allowed duration in ms
111+
- `max (number)`: Maximum allowed duration in ms
112+
- `rounded (boolean)`: If `true`, rounds result
113+
- **Returns:** clamped, optionally rounded duration in ms
114+
115+
---
69116

70-
**Parameters:**
71-
- `offset (number)`: Optional additional duration in milliseconds.
117+
## 💡 Real-World Use Cases
72118

73-
**Returns:**
74-
- `number`: Total adjusted duration in ms
119+
- Precision animations in sliders, carousels, or onboarding steps
120+
- Smooth frame-based motion control in games or interactive UIs
121+
- Avoiding stutters in custom scroll or fade effects
122+
- Performance-friendly frame sync for canvas/webgl renderers
75123

76124
---
77125

78-
## Use Case
126+
## 📄 License
79127

80-
Perfect for developers building custom sliders, carousels, animation loops, or precision-based timing utilities where frame-perfect sync matters.
128+
MIT License — free for personal and commercial use.
81129

82130
---
83131

84-
## License
132+
## 👤 Author
85133

86-
MIT License
134+
Developed by [**INFINITUM FORM®**](https://infinitumform.com)
135+
Author: [Ivijan-Stefan Stipić](https://www.linkedin.com/in/ivijanstefanstipic/)
136+
© 2025 Ivijan-Stefan Stipić. All rights reserved.
87137

88138
---
89139

90-
## Author
140+
For issues, contributions, or improvements, please visit the [GitHub repository](https://github.com/InfinitumForm/FrameRateHelper).
91141

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.
142+
Happy animating! 🎨

dist/FrameRateHelper.js

Lines changed: 184 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)