@runtypelabs/terminal-animations 0.2.3 → 0.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtypelabs/terminal-animations",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Terminal loading animations for Runtype CLI — AnimatedVariant, LoadingAnimation, and variant library.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -19,12 +19,12 @@
19
19
  "react": "^19.2.8"
20
20
  },
21
21
  "devDependencies": {
22
- "@types/react": "^19.2.17"
22
+ "@types/react": "^19.2.18"
23
23
  },
24
24
  "publishConfig": {
25
25
  "access": "public"
26
26
  },
27
- "author": "Runtype Labs",
27
+ "author": "Runtype",
28
28
  "license": "MIT",
29
29
  "repository": {
30
30
  "type": "git",
@@ -44,6 +44,6 @@
44
44
  ],
45
45
  "scripts": {
46
46
  "build": "echo 'No build - source consumed directly'",
47
- "lint": "eslint ."
47
+ "lint": "oxlint ."
48
48
  }
49
49
  }
@@ -311,8 +311,6 @@ const resolveCanvasSize = viewport => {
311
311
  }
312
312
 
313
313
  return {
314
- // Variant cells are rendered as two-character-wide glyphs, so fit them to
315
- // the current terminal width before projection to avoid wrapping.
316
314
  width: Math.max(1, Math.floor(viewport.width / 2)),
317
315
  height: Math.max(1, Math.floor(viewport.height))
318
316
  };
package/src/index.d.ts CHANGED
@@ -6,8 +6,67 @@ export function LoadingAnimation(props?: {
6
6
  width?: number
7
7
  height?: number
8
8
  scale?: number
9
+ random?: () => number
9
10
  }): ReactNode
10
11
 
12
+ export interface AnimationCell {
13
+ text: string
14
+ color?: string
15
+ backgroundColor?: string
16
+ dimColor?: boolean
17
+ bold?: boolean
18
+ }
19
+
20
+ export interface LoadingMotion {
21
+ x: number
22
+ y: number
23
+ dx: number
24
+ dy: number
25
+ }
26
+
27
+ export interface LoadingBounds {
28
+ normalizedScale: number
29
+ scaledLogoWidth: number
30
+ scaledLogoHeight: number
31
+ boundedWidth: number
32
+ boundedHeight: number
33
+ maxX: number
34
+ maxY: number
35
+ }
36
+
37
+ export const LOGO_WIDTH: number
38
+ export const LOGO_HEIGHT: number
39
+ export const DEFAULT_LOGO_SCALE: number
40
+ export const POSITION_TICK_MS: number
41
+ export function pickLoadingVariant(random?: () => number): string
42
+ export function resolveLoadingBounds(options?: {
43
+ width?: number
44
+ height?: number
45
+ scale?: number
46
+ }): LoadingBounds
47
+ export function createInitialMotion(
48
+ width: number,
49
+ height: number,
50
+ scaledLogoWidth: number,
51
+ scaledLogoHeight: number,
52
+ random?: () => number
53
+ ): LoadingMotion
54
+ export function clampMotion(current: LoadingMotion, maxX: number, maxY: number): LoadingMotion
55
+ export function stepLoadingMotion(current: LoadingMotion, maxX: number, maxY: number): LoadingMotion
56
+ export function loadingAnimationFrame(options: {
57
+ width?: number
58
+ height?: number
59
+ scale?: number
60
+ random?: () => number
61
+ tick: number
62
+ }): {
63
+ variantId: string
64
+ bounds: LoadingBounds
65
+ motion: LoadingMotion
66
+ logoFrame: number
67
+ logo: AnimationCell[][]
68
+ }
69
+
11
70
  export function StartupGridIconCompactLightInverted(props?: {
12
71
  frame?: number
13
72
  initialFrame?: number
package/src/index.js CHANGED
@@ -6,4 +6,16 @@ export {
6
6
  StartupGridIconCompactLightInverted,
7
7
  } from './components.js'
8
8
  export { LoadingAnimation, LOADING_VARIANTS } from './loading-animation.js'
9
+ export {
10
+ DEFAULT_LOGO_SCALE,
11
+ LOGO_HEIGHT,
12
+ LOGO_WIDTH,
13
+ POSITION_TICK_MS,
14
+ clampMotion,
15
+ createInitialMotion,
16
+ loadingAnimationFrame,
17
+ pickLoadingVariant,
18
+ resolveLoadingBounds,
19
+ stepLoadingMotion,
20
+ } from './loading-motion.js'
9
21
  export { getVariant, variantNames, getAllVariants } from './variants.js'
@@ -1,105 +1,56 @@
1
1
  import React, { useEffect, useMemo, useState } from 'react'
2
2
  import { Box } from 'ink'
3
3
  import { AnimatedVariant } from './components.js'
4
+ import {
5
+ DEFAULT_LOGO_SCALE,
6
+ LOADING_VARIANTS,
7
+ POSITION_TICK_MS,
8
+ clampMotion,
9
+ createInitialMotion,
10
+ pickLoadingVariant,
11
+ resolveLoadingBounds,
12
+ stepLoadingMotion,
13
+ } from './loading-motion.js'
4
14
 
5
- /** Variants available for the loading animation (randomly selected on mount) */
6
- export const LOADING_VARIANTS = [
7
- 'neon-pulse',
8
- 'scanline-sweep',
9
- 'glitch-shift',
10
- 'matrix-drip',
11
- 'packet-trace',
12
- 'firewall-lock',
13
- 'quantum-lag',
14
- 'signal-bloom',
15
- 'ghost-shell',
16
- 'core-breach',
17
- ]
18
-
19
- const LOGO_WIDTH = 16
20
- const LOGO_HEIGHT = 8
21
- const DEFAULT_LOGO_SCALE = 2
22
- const POSITION_TICK_MS = 80
23
-
24
- function clamp(value, min, max) {
25
- return Math.min(max, Math.max(min, value))
26
- }
27
-
28
- function createInitialMotion(width, height, scaledLogoWidth, scaledLogoHeight) {
29
- const maxX = Math.max(0, width - scaledLogoWidth)
30
- const maxY = Math.max(0, height - scaledLogoHeight)
31
-
32
- return {
33
- x: Math.floor(maxX / 2),
34
- y: Math.floor(maxY / 2),
35
- dx: maxX > 0 ? (Math.random() < 0.5 ? 1 : -1) : 0,
36
- dy: maxY > 0 ? (Math.random() < 0.5 ? 1 : -1) : 0,
37
- }
38
- }
15
+ export { LOADING_VARIANTS }
39
16
 
40
17
  /**
41
18
  * Short loading animation that randomly selects a variant on mount and
42
19
  * bounces around the available area like a DVD screensaver logo.
20
+ * `random` replaces Math.random for the variant pick and the initial direction.
43
21
  */
44
22
  export function LoadingAnimation({
45
23
  width = 48,
46
24
  height = 16,
47
25
  scale = DEFAULT_LOGO_SCALE,
26
+ random = Math.random,
48
27
  } = {}) {
49
- const variantId = useMemo(
50
- () => LOADING_VARIANTS[Math.floor(Math.random() * LOADING_VARIANTS.length)],
51
- []
52
- )
53
- const normalizedScale = Number.isInteger(scale) && scale > 0 ? scale : DEFAULT_LOGO_SCALE
54
- const scaledLogoWidth = LOGO_WIDTH * normalizedScale
55
- const scaledLogoHeight = LOGO_HEIGHT * normalizedScale
56
- const boundedWidth = Math.max(scaledLogoWidth, Math.floor(width))
57
- const boundedHeight = Math.max(scaledLogoHeight, Math.floor(height))
28
+ const variantId = useMemo(() => pickLoadingVariant(random), [])
29
+ const { normalizedScale, scaledLogoWidth, scaledLogoHeight, boundedWidth, boundedHeight } =
30
+ resolveLoadingBounds({ width, height, scale })
58
31
  const [motion, setMotion] = useState(() =>
59
- createInitialMotion(boundedWidth, boundedHeight, scaledLogoWidth, scaledLogoHeight)
32
+ createInitialMotion(boundedWidth, boundedHeight, scaledLogoWidth, scaledLogoHeight, random)
60
33
  )
61
34
 
62
35
  useEffect(() => {
63
- setMotion((current) => {
64
- const maxX = Math.max(0, boundedWidth - scaledLogoWidth)
65
- const maxY = Math.max(0, boundedHeight - scaledLogoHeight)
66
-
67
- return {
68
- x: clamp(current.x, 0, maxX),
69
- y: clamp(current.y, 0, maxY),
70
- dx: maxX > 0 ? current.dx || 1 : 0,
71
- dy: maxY > 0 ? current.dy || 1 : 0,
72
- }
73
- })
36
+ setMotion((current) =>
37
+ clampMotion(
38
+ current,
39
+ Math.max(0, boundedWidth - scaledLogoWidth),
40
+ Math.max(0, boundedHeight - scaledLogoHeight)
41
+ )
42
+ )
74
43
  }, [boundedHeight, boundedWidth, scaledLogoHeight, scaledLogoWidth])
75
44
 
76
45
  useEffect(() => {
77
46
  const timer = globalThis.setInterval(() => {
78
- setMotion((current) => {
79
- const maxX = Math.max(0, boundedWidth - scaledLogoWidth)
80
- const maxY = Math.max(0, boundedHeight - scaledLogoHeight)
81
- let nextX = current.x + current.dx
82
- let nextY = current.y + current.dy
83
- let nextDx = current.dx
84
- let nextDy = current.dy
85
-
86
- if (nextX <= 0 || nextX >= maxX) {
87
- nextDx = -nextDx || 0
88
- nextX = clamp(nextX, 0, maxX)
89
- }
90
-
91
- if (nextY <= 0 || nextY >= maxY) {
92
- nextDy = -nextDy || 0
93
- nextY = clamp(nextY, 0, maxY)
94
- }
95
-
96
- return {
97
- x: nextX,
98
- y: nextY,
99
- dx: nextDx,
100
- dy: nextDy,
101
- }
102
- })
47
+ setMotion((current) =>
48
+ stepLoadingMotion(
49
+ current,
50
+ Math.max(0, boundedWidth - scaledLogoWidth),
51
+ Math.max(0, boundedHeight - scaledLogoHeight)
52
+ )
53
+ )
103
54
  }, POSITION_TICK_MS)
104
55
 
105
56
  return () => globalThis.clearInterval(timer)
@@ -0,0 +1,121 @@
1
+ import {getVariant} from "./variants.js";
2
+
3
+ /** Variants available for the loading animation (randomly selected on mount) */
4
+ export const LOADING_VARIANTS = [
5
+ "neon-pulse",
6
+ "scanline-sweep",
7
+ "glitch-shift",
8
+ "matrix-drip",
9
+ "packet-trace",
10
+ "firewall-lock",
11
+ "quantum-lag",
12
+ "signal-bloom",
13
+ "ghost-shell",
14
+ "core-breach"
15
+ ];
16
+
17
+ export const LOGO_WIDTH = 16;
18
+ export const LOGO_HEIGHT = 8;
19
+ export const DEFAULT_LOGO_SCALE = 2;
20
+ export const POSITION_TICK_MS = 80;
21
+
22
+ const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
23
+
24
+ /** Picks the loading variant with one draw from `random`. */
25
+ export const pickLoadingVariant = (random = Math.random) =>
26
+ LOADING_VARIANTS[Math.floor(random() * LOADING_VARIANTS.length)];
27
+
28
+ /** Resolves the integer scale and the box that the logo bounces inside. */
29
+ export const resolveLoadingBounds = ({width = 48, height = 16, scale = DEFAULT_LOGO_SCALE} = {}) => {
30
+ const normalizedScale = Number.isInteger(scale) && scale > 0 ? scale : DEFAULT_LOGO_SCALE;
31
+ const scaledLogoWidth = LOGO_WIDTH * normalizedScale;
32
+ const scaledLogoHeight = LOGO_HEIGHT * normalizedScale;
33
+ const boundedWidth = Math.max(scaledLogoWidth, Math.floor(width));
34
+ const boundedHeight = Math.max(scaledLogoHeight, Math.floor(height));
35
+
36
+ return {
37
+ normalizedScale,
38
+ scaledLogoWidth,
39
+ scaledLogoHeight,
40
+ boundedWidth,
41
+ boundedHeight,
42
+ maxX: Math.max(0, boundedWidth - scaledLogoWidth),
43
+ maxY: Math.max(0, boundedHeight - scaledLogoHeight)
44
+ };
45
+ };
46
+
47
+ /** Centres the logo and draws dx, then dy, from `random`; an axis with no room draws nothing. */
48
+ export const createInitialMotion = (width, height, scaledLogoWidth, scaledLogoHeight, random = Math.random) => {
49
+ const maxX = Math.max(0, width - scaledLogoWidth);
50
+ const maxY = Math.max(0, height - scaledLogoHeight);
51
+
52
+ return {
53
+ x: Math.floor(maxX / 2),
54
+ y: Math.floor(maxY / 2),
55
+ dx: maxX > 0 ? (random() < 0.5 ? 1 : -1) : 0,
56
+ dy: maxY > 0 ? (random() < 0.5 ? 1 : -1) : 0
57
+ };
58
+ };
59
+
60
+ /** Re-clamps a motion after the bounds change. */
61
+ export const clampMotion = (current, maxX, maxY) => ({
62
+ x: clamp(current.x, 0, maxX),
63
+ y: clamp(current.y, 0, maxY),
64
+ dx: maxX > 0 ? current.dx || 1 : 0,
65
+ dy: maxY > 0 ? current.dy || 1 : 0
66
+ });
67
+
68
+ /** One 80 ms position tick: move, then reverse and clamp on reaching either edge. */
69
+ export const stepLoadingMotion = (current, maxX, maxY) => {
70
+ let nextX = current.x + current.dx;
71
+ let nextY = current.y + current.dy;
72
+ let nextDx = current.dx;
73
+ let nextDy = current.dy;
74
+
75
+ if (nextX <= 0 || nextX >= maxX) {
76
+ nextDx = -nextDx || 0;
77
+ nextX = clamp(nextX, 0, maxX);
78
+ }
79
+
80
+ if (nextY <= 0 || nextY >= maxY) {
81
+ nextDy = -nextDy || 0;
82
+ nextY = clamp(nextY, 0, maxY);
83
+ }
84
+
85
+ return {
86
+ x: nextX,
87
+ y: nextY,
88
+ dx: nextDx,
89
+ dy: nextDy
90
+ };
91
+ };
92
+
93
+ /**
94
+ * The loading animation at position tick `tick`, without Ink: the chosen variant, the logo offset,
95
+ * the logo frame (the logo's own timer modelled as floor(tick * 80 / variant.speed)), and the logo grid.
96
+ * @param {{width?: number, height?: number, scale?: number, random?: () => number, tick: number}} options
97
+ */
98
+ export const loadingAnimationFrame = ({width = 48, height = 16, scale = DEFAULT_LOGO_SCALE, random = Math.random, tick}) => {
99
+ const variantId = pickLoadingVariant(random);
100
+ const bounds = resolveLoadingBounds({width, height, scale});
101
+ let motion = clampMotion(
102
+ createInitialMotion(bounds.boundedWidth, bounds.boundedHeight, bounds.scaledLogoWidth, bounds.scaledLogoHeight, random),
103
+ bounds.maxX,
104
+ bounds.maxY
105
+ );
106
+
107
+ for (let step = 0; step < tick; step += 1) {
108
+ motion = stepLoadingMotion(motion, bounds.maxX, bounds.maxY);
109
+ }
110
+
111
+ const variant = getVariant(variantId);
112
+ const logoFrame = Math.floor((tick * POSITION_TICK_MS) / variant.speed);
113
+
114
+ return {
115
+ variantId,
116
+ bounds,
117
+ motion,
118
+ logoFrame,
119
+ logo: variant.painter(logoFrame, {})
120
+ };
121
+ };