@studiometa/js-toolkit 3.1.0 → 3.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -43,6 +43,7 @@ export declare function smoothTo(start?: number, options?: SmoothToOptions): {
43
43
  */
44
44
  unsubscribe: (fn: (value: number) => any) => boolean;
45
45
  raw(): number;
46
+ add(val: number): number;
46
47
  damping(val?: number): number;
47
48
  precision(val?: number): number;
48
49
  stiffness(val?: number): number;
@@ -1,10 +1,7 @@
1
- import { useRaf } from "../../services/RafService.js";
2
1
  import { damp } from "./damp.js";
3
2
  import { spring } from "./spring.js";
4
3
  import { isFunction, isDefined } from "../is.js";
5
4
  function smoothTo(start = 0, options = {}) {
6
- const { add, has, remove } = useRaf();
7
- const key = Symbol();
8
5
  const fns = /* @__PURE__ */ new Set();
9
6
  let damping = options.damping ?? 0.6;
10
7
  let precision = options.precision ?? 1 / 1e8;
@@ -23,7 +20,7 @@ function smoothTo(start = 0, options = {}) {
23
20
  for (const fn of fns) {
24
21
  fn(smoothed);
25
22
  }
26
- if (smoothed === value) remove(key);
23
+ if (smoothed !== value) requestAnimationFrame(tick);
27
24
  }
28
25
  function subscribe(fn) {
29
26
  if (isFunction(fn)) {
@@ -39,14 +36,13 @@ function smoothTo(start = 0, options = {}) {
39
36
  return smoothed;
40
37
  }
41
38
  value = newValue;
42
- if (!has(key)) {
43
- add(key, tick);
44
- }
39
+ tick();
45
40
  return smoothed;
46
41
  }
47
42
  update.subscribe = subscribe;
48
43
  update.unsubscribe = unsubscribe;
49
44
  update.raw = () => value;
45
+ update.add = (val) => update(value + val);
50
46
  update.damping = (val) => damping = val ?? damping;
51
47
  update.precision = (val) => precision = val ?? precision;
52
48
  update.stiffness = (val) => stiffness = val ?? stiffness;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../packages/js-toolkit/utils/math/smoothTo.ts"],
4
- "sourcesContent": ["import { useRaf } from '../../services/RafService.js';\nimport { damp } from './damp.js';\nimport { spring } from './spring.js';\nimport { isFunction, isDefined } from '../is.js';\n\nexport type SmoothToOptions = Partial<{\n /**\n * Damping factor, the smaller the smoother, defaults to 0.6.\n */\n damping: number;\n /**\n * Precision used to decide when the smoothing should end, defaults to 1 / 1e8.\n */\n precision: number;\n /**\n * Enable spring physics, defaults to `true` if one of `stiffness` or `mass` is set, defaults to `false` otherwise.\n */\n spring: boolean;\n /**\n * Stiffness factor for spring physics, the higher the stiffer, defaults to 0.1.\n */\n stiffness: number;\n /**\n * Mass factor when spring is enabled, the higher the heavier, defaults to 1.\n */\n mass: number;\n}>;\n\n/**\n * Create a smoothing function that will smoothly tween between two values.\n * @param {number} [start] The initial value.\n * @param {SmoothToOptions} [options] Options for the transition.\n * @see https://js-toolkit.studiometa.dev/utils/math/smoothTo.html\n * @example\n * ```js\n * const x = smoothTo(0);\n * x.subscribe((value) => console.log(value));\n * x(100); // 0.1\n * ```\n */\nexport function smoothTo(start = 0, options: SmoothToOptions = {}) {\n const { add, has, remove } = useRaf();\n const key = Symbol();\n const fns = new Set<(value: number) => any>();\n\n let damping = options.damping ?? 0.6;\n let precision = options.precision ?? 1 / 1e8;\n let stiffness = options.stiffness ?? 0.1;\n let mass = options.mass ?? 1;\n let isSpring = options.spring === true || isDefined(options.stiffness) || isDefined(options.mass);\n\n let value = start;\n let smoothed = start;\n let velocity = 0;\n\n /**\n * Callback for the useRaf service.\n */\n function tick() {\n if (isSpring) {\n [smoothed, velocity] = spring(value, smoothed, velocity, stiffness, damping, mass, precision);\n } else {\n smoothed = damp(value, smoothed, damping, precision);\n }\n\n for (const fn of fns) {\n fn(smoothed);\n }\n\n if (smoothed === value) remove(key);\n }\n\n /**\n * Subscribe a callback to the value update.\n */\n function subscribe(fn: (value: number) => any) {\n if (isFunction(fn)) {\n fns.add(fn);\n }\n\n return () => unsubscribe(fn);\n }\n\n /**\n * Unsubscribe a callback to the value update.\n */\n function unsubscribe(fn: (value: number) => any) {\n return fns.delete(fn);\n }\n\n /**\n * Update the value if a parameter is given, get the value if no parameter is given.\n */\n function update(newValue?: number): number {\n if (!isDefined(newValue)) {\n return smoothed;\n }\n\n value = newValue;\n\n if (!has(key)) {\n add(key, tick);\n }\n\n return smoothed;\n }\n\n /**\n * Subscribe a callback to the value update.\n */\n update.subscribe = subscribe;\n\n /**\n * Unsubscribe a callback from the value update.\n */\n update.unsubscribe = unsubscribe;\n\n /**\n * Get the raw target value.\n */\n update.raw = () => value;\n /**\n * Get or set the damping factor.\n */\n update.damping = (val?: number) => (damping = val ?? damping);\n\n /**\n * Get or set the precision.\n */\n update.precision = (val?: number) => (precision = val ?? precision);\n\n /**\n * Get or set the stiffness factor.\n */\n update.stiffness = (val?: number) => (stiffness = val ?? stiffness);\n\n /**\n * Get or set the spring option.\n */\n update.spring = (val?: boolean) => (isSpring = val ?? isSpring);\n\n update(start);\n return update;\n}\n"],
5
- "mappings": "AAAA,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,cAAc;AACvB,SAAS,YAAY,iBAAiB;AAqC/B,SAAS,SAAS,QAAQ,GAAG,UAA2B,CAAC,GAAG;AACjE,QAAM,EAAE,KAAK,KAAK,OAAO,IAAI,OAAO;AACpC,QAAM,MAAM,OAAO;AACnB,QAAM,MAAM,oBAAI,IAA4B;AAE5C,MAAI,UAAU,QAAQ,WAAW;AACjC,MAAI,YAAY,QAAQ,aAAa,IAAI;AACzC,MAAI,YAAY,QAAQ,aAAa;AACrC,MAAI,OAAO,QAAQ,QAAQ;AAC3B,MAAI,WAAW,QAAQ,WAAW,QAAQ,UAAU,QAAQ,SAAS,KAAK,UAAU,QAAQ,IAAI;AAEhG,MAAI,QAAQ;AACZ,MAAI,WAAW;AACf,MAAI,WAAW;AAKf,WAAS,OAAO;AACd,QAAI,UAAU;AACZ,OAAC,UAAU,QAAQ,IAAI,OAAO,OAAO,UAAU,UAAU,WAAW,SAAS,MAAM,SAAS;AAAA,IAC9F,OAAO;AACL,iBAAW,KAAK,OAAO,UAAU,SAAS,SAAS;AAAA,IACrD;AAEA,eAAW,MAAM,KAAK;AACpB,SAAG,QAAQ;AAAA,IACb;AAEA,QAAI,aAAa,MAAO,QAAO,GAAG;AAAA,EACpC;AAKA,WAAS,UAAU,IAA4B;AAC7C,QAAI,WAAW,EAAE,GAAG;AAClB,UAAI,IAAI,EAAE;AAAA,IACZ;AAEA,WAAO,MAAM,YAAY,EAAE;AAAA,EAC7B;AAKA,WAAS,YAAY,IAA4B;AAC/C,WAAO,IAAI,OAAO,EAAE;AAAA,EACtB;AAKA,WAAS,OAAO,UAA2B;AACzC,QAAI,CAAC,UAAU,QAAQ,GAAG;AACxB,aAAO;AAAA,IACT;AAEA,YAAQ;AAER,QAAI,CAAC,IAAI,GAAG,GAAG;AACb,UAAI,KAAK,IAAI;AAAA,IACf;AAEA,WAAO;AAAA,EACT;AAKA,SAAO,YAAY;AAKnB,SAAO,cAAc;AAKrB,SAAO,MAAM,MAAM;AAInB,SAAO,UAAU,CAAC,QAAkB,UAAU,OAAO;AAKrD,SAAO,YAAY,CAAC,QAAkB,YAAY,OAAO;AAKzD,SAAO,YAAY,CAAC,QAAkB,YAAY,OAAO;AAKzD,SAAO,SAAS,CAAC,QAAmB,WAAW,OAAO;AAEtD,SAAO,KAAK;AACZ,SAAO;AACT;",
4
+ "sourcesContent": ["import { damp } from './damp.js';\nimport { spring } from './spring.js';\nimport { isFunction, isDefined } from '../is.js';\n\nexport type SmoothToOptions = Partial<{\n /**\n * Damping factor, the smaller the smoother, defaults to 0.6.\n */\n damping: number;\n /**\n * Precision used to decide when the smoothing should end, defaults to 1 / 1e8.\n */\n precision: number;\n /**\n * Enable spring physics, defaults to `true` if one of `stiffness` or `mass` is set, defaults to `false` otherwise.\n */\n spring: boolean;\n /**\n * Stiffness factor for spring physics, the higher the stiffer, defaults to 0.1.\n */\n stiffness: number;\n /**\n * Mass factor when spring is enabled, the higher the heavier, defaults to 1.\n */\n mass: number;\n}>;\n\n/**\n * Create a smoothing function that will smoothly tween between two values.\n * @param {number} [start] The initial value.\n * @param {SmoothToOptions} [options] Options for the transition.\n * @see https://js-toolkit.studiometa.dev/utils/math/smoothTo.html\n * @example\n * ```js\n * const x = smoothTo(0);\n * x.subscribe((value) => console.log(value));\n * x(100); // 0.1\n * ```\n */\nexport function smoothTo(start = 0, options: SmoothToOptions = {}) {\n const fns = new Set<(value: number) => any>();\n\n let damping = options.damping ?? 0.6;\n let precision = options.precision ?? 1 / 1e8;\n let stiffness = options.stiffness ?? 0.1;\n let mass = options.mass ?? 1;\n let isSpring = options.spring === true || isDefined(options.stiffness) || isDefined(options.mass);\n\n let value = start;\n let smoothed = start;\n let velocity = 0;\n\n /**\n * Callback for the useRaf service.\n */\n function tick() {\n if (isSpring) {\n [smoothed, velocity] = spring(value, smoothed, velocity, stiffness, damping, mass, precision);\n } else {\n smoothed = damp(value, smoothed, damping, precision);\n }\n\n for (const fn of fns) {\n fn(smoothed);\n }\n\n if (smoothed !== value) requestAnimationFrame(tick);\n }\n\n /**\n * Subscribe a callback to the value update.\n */\n function subscribe(fn: (value: number) => any) {\n if (isFunction(fn)) {\n fns.add(fn);\n }\n\n return () => unsubscribe(fn);\n }\n\n /**\n * Unsubscribe a callback to the value update.\n */\n function unsubscribe(fn: (value: number) => any) {\n return fns.delete(fn);\n }\n\n /**\n * Update the value if a parameter is given, get the value if no parameter is given.\n */\n function update(newValue?: number): number {\n if (!isDefined(newValue)) {\n return smoothed;\n }\n\n value = newValue;\n\n tick();\n\n return smoothed;\n }\n\n /**\n * Subscribe a callback to the value update.\n */\n update.subscribe = subscribe;\n\n /**\n * Unsubscribe a callback from the value update.\n */\n update.unsubscribe = unsubscribe;\n\n /**\n * Get the raw target value.\n */\n update.raw = () => value;\n\n /**\n * Add the given value to the current raw value.\n */\n update.add = (val: number) => update(value + val);\n\n /**\n * Get or set the damping factor.\n */\n update.damping = (val?: number) => (damping = val ?? damping);\n\n /**\n * Get or set the precision.\n */\n update.precision = (val?: number) => (precision = val ?? precision);\n\n /**\n * Get or set the stiffness factor.\n */\n update.stiffness = (val?: number) => (stiffness = val ?? stiffness);\n\n /**\n * Get or set the spring option.\n */\n update.spring = (val?: boolean) => (isSpring = val ?? isSpring);\n\n update(start);\n return update;\n}\n"],
5
+ "mappings": "AAAA,SAAS,YAAY;AACrB,SAAS,cAAc;AACvB,SAAS,YAAY,iBAAiB;AAqC/B,SAAS,SAAS,QAAQ,GAAG,UAA2B,CAAC,GAAG;AACjE,QAAM,MAAM,oBAAI,IAA4B;AAE5C,MAAI,UAAU,QAAQ,WAAW;AACjC,MAAI,YAAY,QAAQ,aAAa,IAAI;AACzC,MAAI,YAAY,QAAQ,aAAa;AACrC,MAAI,OAAO,QAAQ,QAAQ;AAC3B,MAAI,WAAW,QAAQ,WAAW,QAAQ,UAAU,QAAQ,SAAS,KAAK,UAAU,QAAQ,IAAI;AAEhG,MAAI,QAAQ;AACZ,MAAI,WAAW;AACf,MAAI,WAAW;AAKf,WAAS,OAAO;AACd,QAAI,UAAU;AACZ,OAAC,UAAU,QAAQ,IAAI,OAAO,OAAO,UAAU,UAAU,WAAW,SAAS,MAAM,SAAS;AAAA,IAC9F,OAAO;AACL,iBAAW,KAAK,OAAO,UAAU,SAAS,SAAS;AAAA,IACrD;AAEA,eAAW,MAAM,KAAK;AACpB,SAAG,QAAQ;AAAA,IACb;AAEA,QAAI,aAAa,MAAO,uBAAsB,IAAI;AAAA,EACpD;AAKA,WAAS,UAAU,IAA4B;AAC7C,QAAI,WAAW,EAAE,GAAG;AAClB,UAAI,IAAI,EAAE;AAAA,IACZ;AAEA,WAAO,MAAM,YAAY,EAAE;AAAA,EAC7B;AAKA,WAAS,YAAY,IAA4B;AAC/C,WAAO,IAAI,OAAO,EAAE;AAAA,EACtB;AAKA,WAAS,OAAO,UAA2B;AACzC,QAAI,CAAC,UAAU,QAAQ,GAAG;AACxB,aAAO;AAAA,IACT;AAEA,YAAQ;AAER,SAAK;AAEL,WAAO;AAAA,EACT;AAKA,SAAO,YAAY;AAKnB,SAAO,cAAc;AAKrB,SAAO,MAAM,MAAM;AAKnB,SAAO,MAAM,CAAC,QAAgB,OAAO,QAAQ,GAAG;AAKhD,SAAO,UAAU,CAAC,QAAkB,UAAU,OAAO;AAKrD,SAAO,YAAY,CAAC,QAAkB,YAAY,OAAO;AAKzD,SAAO,YAAY,CAAC,QAAkB,YAAY,OAAO;AAKzD,SAAO,SAAS,CAAC,QAAmB,WAAW,OAAO;AAEtD,SAAO,KAAK;AACZ,SAAO;AACT;",
6
6
  "names": []
7
7
  }