slowmo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +120 -0
- package/dist/index.d.ts +49 -0
- package/dist/slowmo.cjs +1 -0
- package/dist/slowmo.js +90 -0
- package/dist/slowmo.umd.cjs +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Decode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# slowmo
|
|
2
|
+
|
|
3
|
+
Slow down any web animation with one line of code.
|
|
4
|
+
|
|
5
|
+
<!-- TODO: Add hero GIF/video here -->
|
|
6
|
+
<!--  -->
|
|
7
|
+
|
|
8
|
+
[](https://www.npmjs.com/package/slowmo)
|
|
9
|
+
[](https://bundlephobia.com/package/slowmo)
|
|
10
|
+
[](https://github.com/anthropics/slowmo/blob/main/LICENSE)
|
|
11
|
+
|
|
12
|
+
## Why?
|
|
13
|
+
|
|
14
|
+
- **Debug animations** - Slow things down to see exactly what's happening
|
|
15
|
+
- **Record demos** - Capture smooth slow-motion footage for product videos
|
|
16
|
+
- **Create effects** - Add dramatic slow-mo moments to your UI
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install slowmo
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add slowmo
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
yarn add slowmo
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or use a CDN:
|
|
33
|
+
|
|
34
|
+
```html
|
|
35
|
+
<script type="module">
|
|
36
|
+
import { slowmo } from 'https://esm.sh/slowmo';
|
|
37
|
+
slowmo(0.5);
|
|
38
|
+
</script>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { slowmo } from 'slowmo';
|
|
45
|
+
|
|
46
|
+
slowmo(0.5); // Everything runs at half speed
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
That's it. CSS animations, videos, canvas, GSAP, Three.js, Framer Motion - all slowed down automatically.
|
|
50
|
+
|
|
51
|
+
## What It Works With
|
|
52
|
+
|
|
53
|
+
| Type | How |
|
|
54
|
+
|------|-----|
|
|
55
|
+
| CSS Animations | Web Animations API `playbackRate` |
|
|
56
|
+
| CSS Transitions | Web Animations API `playbackRate` |
|
|
57
|
+
| Videos & Audio | `playbackRate` property |
|
|
58
|
+
| requestAnimationFrame | Patched timestamps |
|
|
59
|
+
| performance.now() | Returns virtual time |
|
|
60
|
+
| GSAP | `globalTimeline.timeScale()` (auto-detected) |
|
|
61
|
+
| Three.js | Uses rAF, works automatically |
|
|
62
|
+
| Framer Motion | Uses Web Animations API, works automatically |
|
|
63
|
+
| Canvas animations | Uses rAF, works automatically |
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { slowmo } from 'slowmo';
|
|
69
|
+
|
|
70
|
+
// Set speed (0.5 = half speed, 2 = double speed)
|
|
71
|
+
slowmo(0.5);
|
|
72
|
+
|
|
73
|
+
// Pause everything
|
|
74
|
+
slowmo(0);
|
|
75
|
+
|
|
76
|
+
// Back to normal
|
|
77
|
+
slowmo(1);
|
|
78
|
+
|
|
79
|
+
// Object API for more control
|
|
80
|
+
slowmo.setSpeed(0.5);
|
|
81
|
+
slowmo.pause();
|
|
82
|
+
slowmo.play();
|
|
83
|
+
slowmo.reset();
|
|
84
|
+
slowmo.getSpeed(); // Returns current speed
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Speed Guide
|
|
88
|
+
|
|
89
|
+
| Speed | Effect |
|
|
90
|
+
|-------|--------|
|
|
91
|
+
| `0` | Paused |
|
|
92
|
+
| `0.1` | 10x slower (great for debugging) |
|
|
93
|
+
| `0.5` | Half speed |
|
|
94
|
+
| `1` | Normal |
|
|
95
|
+
| `2` | Double speed |
|
|
96
|
+
|
|
97
|
+
### Excluding Elements
|
|
98
|
+
|
|
99
|
+
Add `data-slowmo-exclude` to opt out specific elements:
|
|
100
|
+
|
|
101
|
+
```html
|
|
102
|
+
<div data-slowmo-exclude>
|
|
103
|
+
This animation runs at normal speed
|
|
104
|
+
</div>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Limitations
|
|
108
|
+
|
|
109
|
+
- **Frame-based animations** that don't use timestamps can't be smoothly slowed (they increment by a fixed amount each frame regardless of time)
|
|
110
|
+
- **Libraries that cache time references** before slowmo loads may not be affected
|
|
111
|
+
- **Video/audio** have browser-imposed limits (~0.0625x to 16x in Chrome)
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
<!-- TODO: Update with actual website URL -->
|
|
120
|
+
[Website](https://slowmo.dev) · [GitHub](https://github.com/anthropics/slowmo) · [npm](https://www.npmjs.com/package/slowmo)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* slowmo - Universal slow-motion control for web animations
|
|
3
|
+
*
|
|
4
|
+
* This library intercepts time at multiple levels to slow down (or speed up)
|
|
5
|
+
* all animations on a web page.
|
|
6
|
+
*
|
|
7
|
+
* ## How it works:
|
|
8
|
+
*
|
|
9
|
+
* 1. **requestAnimationFrame patching**: We replace window.requestAnimationFrame
|
|
10
|
+
* with a wrapper that passes modified timestamps to callbacks. Time-based
|
|
11
|
+
* animations that use the timestamp parameter will automatically slow down.
|
|
12
|
+
*
|
|
13
|
+
* 2. **performance.now() patching**: We replace performance.now() to return
|
|
14
|
+
* virtual time. Libraries that use this for timing will be affected.
|
|
15
|
+
*
|
|
16
|
+
* 3. **Web Animations API**: We poll document.getAnimations() and modify the
|
|
17
|
+
* playbackRate of all Animation objects. This affects CSS animations,
|
|
18
|
+
* CSS transitions, and element.animate() calls.
|
|
19
|
+
*
|
|
20
|
+
* 4. **Media elements**: We set playbackRate on video/audio elements.
|
|
21
|
+
*
|
|
22
|
+
* ## Limitations:
|
|
23
|
+
*
|
|
24
|
+
* - Frame-based animations (that increment by a fixed amount per frame without
|
|
25
|
+
* using timestamps) cannot be smoothly slowed down. See IDEAS.md for a
|
|
26
|
+
* potential "frame throttling" mode.
|
|
27
|
+
*
|
|
28
|
+
* - Animations created by libraries that cache their own time references
|
|
29
|
+
* before we patch may not be affected.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Main slowmo function - set speed directly.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* import { slowmo } from 'slowmo';
|
|
36
|
+
* slowmo(0.5); // Half speed
|
|
37
|
+
* slowmo(0.1); // 10x slower
|
|
38
|
+
* slowmo(2); // Double speed
|
|
39
|
+
* slowmo(0); // Pause
|
|
40
|
+
*/
|
|
41
|
+
export declare function slowmo(speed: number): void;
|
|
42
|
+
export declare namespace slowmo {
|
|
43
|
+
var setSpeed: (speed: number) => void;
|
|
44
|
+
var pause: () => void;
|
|
45
|
+
var play: () => void;
|
|
46
|
+
var reset: () => void;
|
|
47
|
+
var getSpeed: () => number;
|
|
48
|
+
}
|
|
49
|
+
export default slowmo;
|
package/dist/slowmo.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});let l=1,o=!1,f=!1,d,r,p=0,c=0,b=0;const y=new WeakMap,g=new WeakMap;function m(i){if(o)return b;const e=i-c;return p+e*l}function k(){if(typeof document.getAnimations!="function")return;const i=document.getAnimations();for(const e of i){const a=e.effect;if((a==null?void 0:a.target)instanceof Element&&a.target.closest("[data-slowmo-exclude]"))continue;const t=y.get(e);if(t){e.playbackRate!==t.applied&&(t.original=e.playbackRate);const n=t.original*l;e.playbackRate!==n&&(e.playbackRate=n,t.applied=n)}else{const n=e.playbackRate,w=n*l;y.set(e,{original:n,applied:w}),e.playbackRate=w}o?e.playState==="running"&&e.pause():e.playState==="paused"&&e.play()}}function R(){document.querySelectorAll("video, audio").forEach(e=>{if(e.closest("[data-slowmo-exclude]"))return;const a=e;let t=g.get(a);if(t?a.playbackRate!==t.applied&&!o&&(t.original=a.playbackRate):(t={original:a.playbackRate,applied:a.playbackRate*l,wasPaused:!1},g.set(a,t)),o)!a.paused&&!t.wasPaused&&(t.wasPaused=!0,a.pause());else{t.wasPaused&&(t.wasPaused=!1,a.play());const n=t.original*l;a.playbackRate!==n&&(a.playbackRate=n,t.applied=n)}})}function A(){k(),R(),d(A)}function S(){if(f||typeof window>"u")return;d=window.requestAnimationFrame.bind(window),r=performance.now.bind(performance),c=r(),p=c;const i=e=>d(a=>{const t=m(a);o?window.requestAnimationFrame(e):e(t)});window.requestAnimationFrame=i,typeof globalThis<"u"&&(globalThis.requestAnimationFrame=i),performance.now=()=>m(r()),d(A),f=!0}function u(i){f||S();const e=r();if(p=m(e),c=e,l=i,o=i===0,o&&(b=p),k(),R(),typeof window.gsap<"u")try{window.gsap.globalTimeline.timeScale(i||.001)}catch{}}function T(){u(0)}function P(){o&&(c=r(),o=!1),u(l||1)}function F(){u(1)}function M(){return l}function s(i){u(i)}s.setSpeed=u;s.pause=T;s.play=P;s.reset=F;s.getSpeed=M;typeof window<"u"&&S();exports.default=s;exports.slowmo=s;
|
package/dist/slowmo.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
let l = 1, o = !1, u = !1, d, s, f = 0, r = 0, b = 0;
|
|
2
|
+
const y = /* @__PURE__ */ new WeakMap(), g = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function m(n) {
|
|
4
|
+
if (o) return b;
|
|
5
|
+
const e = n - r;
|
|
6
|
+
return f + e * l;
|
|
7
|
+
}
|
|
8
|
+
function k() {
|
|
9
|
+
if (typeof document.getAnimations != "function") return;
|
|
10
|
+
const n = document.getAnimations();
|
|
11
|
+
for (const e of n) {
|
|
12
|
+
const a = e.effect;
|
|
13
|
+
if ((a == null ? void 0 : a.target) instanceof Element && a.target.closest("[data-slowmo-exclude]"))
|
|
14
|
+
continue;
|
|
15
|
+
const t = y.get(e);
|
|
16
|
+
if (t) {
|
|
17
|
+
e.playbackRate !== t.applied && (t.original = e.playbackRate);
|
|
18
|
+
const i = t.original * l;
|
|
19
|
+
e.playbackRate !== i && (e.playbackRate = i, t.applied = i);
|
|
20
|
+
} else {
|
|
21
|
+
const i = e.playbackRate, w = i * l;
|
|
22
|
+
y.set(e, { original: i, applied: w }), e.playbackRate = w;
|
|
23
|
+
}
|
|
24
|
+
o ? e.playState === "running" && e.pause() : e.playState === "paused" && e.play();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function R() {
|
|
28
|
+
document.querySelectorAll("video, audio").forEach((e) => {
|
|
29
|
+
if (e.closest("[data-slowmo-exclude]")) return;
|
|
30
|
+
const a = e;
|
|
31
|
+
let t = g.get(a);
|
|
32
|
+
if (t ? a.playbackRate !== t.applied && !o && (t.original = a.playbackRate) : (t = {
|
|
33
|
+
original: a.playbackRate,
|
|
34
|
+
applied: a.playbackRate * l,
|
|
35
|
+
wasPaused: !1
|
|
36
|
+
}, g.set(a, t)), o)
|
|
37
|
+
!a.paused && !t.wasPaused && (t.wasPaused = !0, a.pause());
|
|
38
|
+
else {
|
|
39
|
+
t.wasPaused && (t.wasPaused = !1, a.play());
|
|
40
|
+
const i = t.original * l;
|
|
41
|
+
a.playbackRate !== i && (a.playbackRate = i, t.applied = i);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function A() {
|
|
46
|
+
k(), R(), d(A);
|
|
47
|
+
}
|
|
48
|
+
function S() {
|
|
49
|
+
if (u || typeof window > "u") return;
|
|
50
|
+
d = window.requestAnimationFrame.bind(window), s = performance.now.bind(performance), r = s(), f = r;
|
|
51
|
+
const n = (e) => d((a) => {
|
|
52
|
+
const t = m(a);
|
|
53
|
+
o ? window.requestAnimationFrame(e) : e(t);
|
|
54
|
+
});
|
|
55
|
+
window.requestAnimationFrame = n, typeof globalThis < "u" && (globalThis.requestAnimationFrame = n), performance.now = () => m(s()), d(A), u = !0;
|
|
56
|
+
}
|
|
57
|
+
function c(n) {
|
|
58
|
+
u || S();
|
|
59
|
+
const e = s();
|
|
60
|
+
if (f = m(e), r = e, l = n, o = n === 0, o && (b = f), k(), R(), typeof window.gsap < "u")
|
|
61
|
+
try {
|
|
62
|
+
window.gsap.globalTimeline.timeScale(n || 1e-3);
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function T() {
|
|
67
|
+
c(0);
|
|
68
|
+
}
|
|
69
|
+
function P() {
|
|
70
|
+
o && (r = s(), o = !1), c(l || 1);
|
|
71
|
+
}
|
|
72
|
+
function F() {
|
|
73
|
+
c(1);
|
|
74
|
+
}
|
|
75
|
+
function h() {
|
|
76
|
+
return l;
|
|
77
|
+
}
|
|
78
|
+
function p(n) {
|
|
79
|
+
c(n);
|
|
80
|
+
}
|
|
81
|
+
p.setSpeed = c;
|
|
82
|
+
p.pause = T;
|
|
83
|
+
p.play = P;
|
|
84
|
+
p.reset = F;
|
|
85
|
+
p.getSpeed = h;
|
|
86
|
+
typeof window < "u" && S();
|
|
87
|
+
export {
|
|
88
|
+
p as default,
|
|
89
|
+
p as slowmo
|
|
90
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s.slowmo={}))})(this,function(s){"use strict";let i=1,l=!1,m=!1,r,f,c=0,u=0,y=0;const g=new WeakMap,b=new WeakMap;function w(n){if(l)return y;const e=n-u;return c+e*i}function k(){if(typeof document.getAnimations!="function")return;const n=document.getAnimations();for(const e of n){const t=e.effect;if((t==null?void 0:t.target)instanceof Element&&t.target.closest("[data-slowmo-exclude]"))continue;const a=g.get(e);if(a){e.playbackRate!==a.applied&&(a.original=e.playbackRate);const o=a.original*i;e.playbackRate!==o&&(e.playbackRate=o,a.applied=o)}else{const o=e.playbackRate,T=o*i;g.set(e,{original:o,applied:T}),e.playbackRate=T}l?e.playState==="running"&&e.pause():e.playState==="paused"&&e.play()}}function R(){document.querySelectorAll("video, audio").forEach(e=>{if(e.closest("[data-slowmo-exclude]"))return;const t=e;let a=b.get(t);if(a?t.playbackRate!==a.applied&&!l&&(a.original=t.playbackRate):(a={original:t.playbackRate,applied:t.playbackRate*i,wasPaused:!1},b.set(t,a)),l)!t.paused&&!a.wasPaused&&(a.wasPaused=!0,t.pause());else{a.wasPaused&&(a.wasPaused=!1,t.play());const o=a.original*i;t.playbackRate!==o&&(t.playbackRate=o,a.applied=o)}})}function A(){k(),R(),r(A)}function S(){if(m||typeof window>"u")return;r=window.requestAnimationFrame.bind(window),f=performance.now.bind(performance),u=f(),c=u;const n=e=>r(t=>{const a=w(t);l?window.requestAnimationFrame(e):e(a)});window.requestAnimationFrame=n,typeof globalThis<"u"&&(globalThis.requestAnimationFrame=n),performance.now=()=>w(f()),r(A),m=!0}function p(n){m||S();const e=f();if(c=w(e),u=e,i=n,l=n===0,l&&(y=c),k(),R(),typeof window.gsap<"u")try{window.gsap.globalTimeline.timeScale(n||.001)}catch{}}function h(){p(0)}function P(){l&&(u=f(),l=!1),p(i||1)}function F(){p(1)}function M(){return i}function d(n){p(n)}d.setSpeed=p,d.pause=h,d.play=P,d.reset=F,d.getSpeed=M,typeof window<"u"&&S(),s.default=d,s.slowmo=d,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "slowmo",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Universal slow-motion control for web animations - CSS, videos, GSAP, Three.js, and more",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/slowmo.cjs",
|
|
7
|
+
"module": "./dist/slowmo.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/slowmo.js",
|
|
12
|
+
"require": "./dist/slowmo.cjs",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "vite demo",
|
|
21
|
+
"build": "vite build",
|
|
22
|
+
"build:demo": "vite build --config vite.demo.config.ts",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"test:watch": "vitest",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"prepublishOnly": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"animation",
|
|
30
|
+
"slow-motion",
|
|
31
|
+
"slowmo",
|
|
32
|
+
"debug",
|
|
33
|
+
"css-animation",
|
|
34
|
+
"gsap",
|
|
35
|
+
"three.js",
|
|
36
|
+
"video",
|
|
37
|
+
"framer-motion"
|
|
38
|
+
],
|
|
39
|
+
"author": "",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^20.11.0",
|
|
43
|
+
"typescript": "^5.3.3",
|
|
44
|
+
"vite": "^5.0.11",
|
|
45
|
+
"vite-plugin-dts": "^3.7.1",
|
|
46
|
+
"vitest": "^1.2.1",
|
|
47
|
+
"jsdom": "^24.0.0",
|
|
48
|
+
"@vitest/coverage-v8": "^1.2.1"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"gsap": ">=3.0.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"gsap": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"packageManager": "pnpm@10.24.0+sha512.01ff8ae71b4419903b65c60fb2dc9d34cf8bb6e06d03bde112ef38f7a34d6904c424ba66bea5cdcf12890230bf39f9580473140ed9c946fef328b6e5238a345a"
|
|
59
|
+
}
|