@arraypress/effects-astro 0.5.0 → 0.6.1

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.
@@ -1,10 +1,19 @@
1
1
  /**
2
2
  * @module @arraypress/effects-astro/random
3
3
  *
4
- * A small seeded random generator (mulberry32), for effects that build
5
- * something from a `seed` and must build the same thing on every load.
4
+ * Small seeded random generators, for effects that build something from a
5
+ * `seed` and must build the same thing on every load: mulberry32 (the
6
+ * default, well mixed from any seed) and the Park–Miller minimal standard
7
+ * (a multiplicative generator, × 16807 mod 2³¹ − 1), kept so a sky first
8
+ * drawn with it can be drawn again exactly.
6
9
  */
7
10
 
11
+ /** Park–Miller's modulus, 2³¹ − 1. */
12
+ const PM_MODULUS = 2147483647;
13
+
14
+ /** Park–Miller's multiplier (the minimal standard's). */
15
+ const PM_MULTIPLIER = 16807;
16
+
8
17
  /**
9
18
  * Makes a seeded generator: the same seed gives the same sequence.
10
19
  *
@@ -20,3 +29,35 @@ export function seededRandom(seed) {
20
29
  return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
21
30
  };
22
31
  }
32
+
33
+ /**
34
+ * Makes a Park–Miller generator: the state times 16807, mod 2³¹ − 1, read
35
+ * as a fraction of the modulus. Exact in double precision. A seed that
36
+ * comes to 0 (or a multiple of the modulus) would stick at 0, so it is
37
+ * moved into range first.
38
+ *
39
+ * @param {number} seed Any number; its integer part is used.
40
+ * @returns {() => number} The next value, over 0 and under 1.
41
+ */
42
+ export function parkMiller(seed) {
43
+ let s = Math.floor(seed) % PM_MODULUS;
44
+ if (s <= 0) s += PM_MODULUS - 1;
45
+ return () => (s = (s * PM_MULTIPLIER) % PM_MODULUS) / PM_MODULUS;
46
+ }
47
+
48
+ /** The generators by name, for an effect's `rng` setting. */
49
+ export const RANDOMS = Object.freeze({ mulberry32: seededRandom, 'park-miller': parkMiller });
50
+
51
+ /**
52
+ * Makes a generator by name.
53
+ *
54
+ * @param {string | undefined} name `mulberry32` (the default) or `park-miller`.
55
+ * @param {number} seed The seed.
56
+ * @returns {() => number} The generator.
57
+ * @throws {TypeError} When the name is not one of RANDOMS.
58
+ */
59
+ export function randomFrom(name, seed) {
60
+ const key = /** @type {keyof typeof RANDOMS} */ (name ?? 'mulberry32');
61
+ if (!Object.hasOwn(RANDOMS, key)) throw new TypeError(`Unknown random generator: ${name} (use ${Object.keys(RANDOMS).join(' or ')})`);
62
+ return RANDOMS[key](seed);
63
+ }
@@ -1,7 +1,7 @@
1
- import type { SceneOptions } from '@arraypress/shaders';
1
+ import type { SceneOptions } from '@logomarks/shaders';
2
2
  import type { PaletteName } from './palettes.js';
3
3
 
4
- /** A scene's name: one of `@arraypress/shaders`' scenes. */
4
+ /** A scene's name: one of `@logomarks/shaders`' scenes. */
5
5
  export type SceneName =
6
6
  | 'marble'
7
7
  | 'frosted'
@@ -51,7 +51,7 @@ export interface SceneConfig {
51
51
  near?: boolean | string;
52
52
  }
53
53
 
54
- /** A `<Scene>`'s settings as `@arraypress/shaders`' options: the palette found by name, `near` as `lazy`. */
54
+ /** A `<Scene>`'s settings as `@logomarks/shaders`' options: the palette found by name, `near` as `lazy`. */
55
55
  export function sceneSettings(config: SceneConfig): { name: SceneName; options: SceneOptions };
56
56
 
57
57
  /** Starts a scene on an element's canvas; returns a function that stops it. */
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module @arraypress/effects-astro/scene
3
3
  *
4
- * A scene from `@arraypress/shaders` on an effect's canvas: liquid marble,
4
+ * A scene from `@logomarks/shaders` on an effect's canvas: liquid marble,
5
5
  * frosted glass, glossy tiles, chrome, glitter, holographic foil, ink in
6
6
  * water and the rest, recoloured by a palette and shaped by the scene's own
7
7
  * settings. `<Scene>` is the component.
@@ -14,14 +14,14 @@
14
14
  * with reduced motion. `setColors` recolours it live.
15
15
  */
16
16
 
17
- import { scene as run, LOOK } from '@arraypress/shaders/core';
18
- import { load } from '@arraypress/shaders/load';
17
+ import { scene as run, LOOK } from '@logomarks/shaders/core';
18
+ import { load } from '@logomarks/shaders/load';
19
19
  import { findPalette } from './palettes.js';
20
20
  import { watchColors } from './mount.js';
21
21
 
22
22
  /**
23
23
  * @typedef {import('./scene.js').SceneConfig} SceneConfig
24
- * @typedef {import('@arraypress/shaders').SceneOptions} SceneOptions
24
+ * @typedef {import('@logomarks/shaders').SceneOptions} SceneOptions
25
25
  */
26
26
 
27
27
  /** The settings a `<Scene>` hands to its scene as they are: the look, the colours, the params, the frame rate and resolution. */
@@ -61,7 +61,7 @@ export function scene(el, config) {
61
61
  if (!canvas) return () => {};
62
62
  const { name, options } = sceneSettings(config);
63
63
  let stopped = false;
64
- /** @type {import('@arraypress/shaders').SceneHandle | null} */
64
+ /** @type {import('@logomarks/shaders').SceneHandle | null} */
65
65
  let handle = null;
66
66
  let unwatch = () => {};
67
67
  load(name)