@effing/tween 0.38.4 → 0.40.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/dist/index.d.ts CHANGED
@@ -18,7 +18,9 @@ type TweenInterval = {
18
18
  * Tween frames with concurrency control
19
19
  * @param count Number of frames to generate
20
20
  * @param fn Function that takes a tween interval and index, returns a promise
21
- * @param options Configuration options
21
+ * @param options Configuration options; `concurrency` defaults to the number
22
+ * of available cores capped at 8 (concurrency overlaps async work such as
23
+ * encoding, not the synchronous draw)
22
24
  * @yields Resulting frames in order
23
25
  */
24
26
  declare function tween<T>(count: number, fn: (interval: TweenInterval, index: number) => Promise<T>, options?: {
@@ -28,7 +30,9 @@ declare function tween<T>(count: number, fn: (interval: TweenInterval, index: nu
28
30
  * Tween frames with concurrency control, returning an array
29
31
  * @param count Number of frames to generate
30
32
  * @param fn Function that takes a tween interval and index, returns a promise
31
- * @param options Configuration options
33
+ * @param options Configuration options; `concurrency` defaults to the number
34
+ * of available cores capped at 8 (concurrency overlaps async work such as
35
+ * encoding, not the synchronous draw)
32
36
  * @returns Promise resolving to array of resulting frames
33
37
  */
34
38
  declare function tweenToArray<T>(count: number, fn: (interval: TweenInterval, index: number) => Promise<T>, options?: {
package/dist/index.js CHANGED
@@ -5,8 +5,10 @@ function steps(count) {
5
5
 
6
6
  // src/tween.ts
7
7
  import os from "os";
8
+ var MAX_DEFAULT_CONCURRENCY = 8;
9
+ var defaultConcurrency = () => Math.min(os.availableParallelism(), MAX_DEFAULT_CONCURRENCY);
8
10
  async function* tween(count, fn, options = {
9
- concurrency: os.availableParallelism()
11
+ concurrency: defaultConcurrency()
10
12
  }) {
11
13
  if (count === 0) {
12
14
  return;
@@ -14,7 +16,7 @@ async function* tween(count, fn, options = {
14
16
  const range = steps(count);
15
17
  const maxConcurrency = Math.max(
16
18
  1,
17
- options.concurrency ?? os.availableParallelism()
19
+ options.concurrency ?? defaultConcurrency()
18
20
  );
19
21
  const activeTasks = /* @__PURE__ */ new Map();
20
22
  let nextIndexToStart = 0;
@@ -46,7 +48,7 @@ async function* tween(count, fn, options = {
46
48
  }
47
49
  }
48
50
  async function tweenToArray(count, fn, options = {
49
- concurrency: os.availableParallelism()
51
+ concurrency: defaultConcurrency()
50
52
  }) {
51
53
  return Array.fromAsync(tween(count, fn, options));
52
54
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/steps.ts","../src/tween.ts","../src/easing.ts"],"sourcesContent":["/**\n * Generate an array of step values for an animation\n * @param count Number of steps\n * @returns Array of step values from 0 to (count-1)/count\n */\nexport function steps(count: number): number[] {\n return Array.from({ length: count }, (_, i) => i / count);\n}\n","import os from \"os\";\nimport { steps } from \"./steps\";\n\n/**\n * Tween interval representing a frame's position in the animation\n */\nexport type TweenInterval = {\n /** Lower bound (inclusive), in range [0, 1) */\n lower: number;\n /** Upper bound (exclusive for all but last frame), in range (0, 1] */\n upper: number;\n};\n\n/**\n * Tween frames with concurrency control\n * @param count Number of frames to generate\n * @param fn Function that takes a tween interval and index, returns a promise\n * @param options Configuration options\n * @yields Resulting frames in order\n */\nexport async function* tween<T>(\n count: number,\n fn: (interval: TweenInterval, index: number) => Promise<T>,\n options: { concurrency?: number } = {\n concurrency: os.availableParallelism(),\n },\n): AsyncGenerator<T> {\n if (count === 0) {\n return;\n }\n\n const range = steps(count);\n const maxConcurrency = Math.max(\n 1,\n options.concurrency ?? os.availableParallelism(),\n );\n const activeTasks = new Map<number, Promise<T>>();\n\n let nextIndexToStart = 0;\n let nextIndexToYield = 0;\n\n const startTask = (index: number): void => {\n const lower = range[index];\n const upper = range[index + 1] ?? 1;\n const promise = fn({ lower, upper }, index);\n activeTasks.set(index, promise);\n };\n\n const fillConcurrencySlots = (): void => {\n while (nextIndexToStart < count && activeTasks.size < maxConcurrency) {\n startTask(nextIndexToStart++);\n }\n };\n\n fillConcurrencySlots();\n\n while (nextIndexToYield < count) {\n const taskPromise = activeTasks.get(nextIndexToYield);\n\n if (!taskPromise) {\n throw new Error(\n `Internal error: task at index ${nextIndexToYield} not found in active tasks`,\n );\n }\n\n const result = await taskPromise;\n activeTasks.delete(nextIndexToYield);\n yield result;\n\n fillConcurrencySlots();\n nextIndexToYield++;\n }\n}\n\n/**\n * Tween frames with concurrency control, returning an array\n * @param count Number of frames to generate\n * @param fn Function that takes a tween interval and index, returns a promise\n * @param options Configuration options\n * @returns Promise resolving to array of resulting frames\n */\nexport async function tweenToArray<T>(\n count: number,\n fn: (interval: TweenInterval, index: number) => Promise<T>,\n options: { concurrency?: number } = {\n concurrency: os.availableParallelism(),\n },\n): Promise<T[]> {\n return Array.fromAsync(tween(count, fn, options));\n}\n","// ============================================\n// Easing functions\n// See: https://easings.net/\n// ============================================\n\n/** Linear interpolation (no easing) */\nexport function linear(x: number): number {\n return x;\n}\n\n// Sine\n\n/** Ease in using sine curve */\nexport function easeInSine(x: number): number {\n return 1 - Math.cos((x * Math.PI) / 2);\n}\n\n/** Ease out using sine curve */\nexport function easeOutSine(x: number): number {\n return Math.sin((x * Math.PI) / 2);\n}\n\n/** Ease in-out using sine curve */\nexport function easeInOutSine(x: number): number {\n return -(Math.cos(Math.PI * x) - 1) / 2;\n}\n\n// Quad\n\n/** Ease in using quadratic curve */\nexport function easeInQuad(x: number): number {\n return x * x;\n}\n\n/** Ease out using quadratic curve */\nexport function easeOutQuad(x: number): number {\n return 1 - Math.pow(1 - x, 2);\n}\n\n/** Ease in-out using quadratic curve */\nexport function easeInOutQuad(x: number): number {\n return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;\n}\n\n// Cubic\n\n/** Ease in using cubic curve */\nexport function easeInCubic(x: number): number {\n return x * x * x;\n}\n\n/** Ease out using cubic curve */\nexport function easeOutCubic(x: number): number {\n return 1 - Math.pow(1 - x, 3);\n}\n\n/** Ease in-out using cubic curve */\nexport function easeInOutCubic(x: number): number {\n return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;\n}\n\n// Quart\n\n/** Ease in using quartic curve */\nexport function easeInQuart(x: number): number {\n return x * x * x * x;\n}\n\n/** Ease out using quartic curve */\nexport function easeOutQuart(x: number): number {\n return 1 - Math.pow(1 - x, 4);\n}\n\n/** Ease in-out using quartic curve */\nexport function easeInOutQuart(x: number): number {\n return x < 0.5 ? 8 * x * x * x * x : 1 - Math.pow(-2 * x + 2, 4) / 2;\n}\n\n// Quint\n\n/** Ease in using quintic curve */\nexport function easeInQuint(x: number): number {\n return x * x * x * x * x;\n}\n\n/** Ease out using quintic curve */\nexport function easeOutQuint(x: number): number {\n return 1 - Math.pow(1 - x, 5);\n}\n\n/** Ease in-out using quintic curve */\nexport function easeInOutQuint(x: number): number {\n return x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.pow(-2 * x + 2, 5) / 2;\n}\n\n// Expo\n\n/** Ease in using exponential curve */\nexport function easeInExpo(x: number): number {\n return x === 0 ? 0 : Math.pow(2, 10 * x - 10);\n}\n\n/** Ease out using exponential curve */\nexport function easeOutExpo(x: number): number {\n return x === 1 ? 1 : 1 - Math.pow(2, -10 * x);\n}\n\n/** Ease in-out using exponential curve */\nexport function easeInOutExpo(x: number): number {\n return x === 0\n ? 0\n : x === 1\n ? 1\n : x < 0.5\n ? Math.pow(2, 20 * x - 10) / 2\n : (2 - Math.pow(2, -20 * x + 10)) / 2;\n}\n\n// Circ\n\n/** Ease in using circular curve */\nexport function easeInCirc(x: number): number {\n return 1 - Math.sqrt(1 - Math.pow(x, 2));\n}\n\n/** Ease out using circular curve */\nexport function easeOutCirc(x: number): number {\n return Math.sqrt(1 - Math.pow(x - 1, 2));\n}\n\n/** Ease in-out using circular curve */\nexport function easeInOutCirc(x: number): number {\n return x < 0.5\n ? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2\n : (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2;\n}\n\n// Back\n\n/** Ease in with overshoot (back) */\nexport function easeInBack(x: number): number {\n const c1 = 1.70158;\n const c3 = c1 + 1;\n return c3 * x * x * x - c1 * x * x;\n}\n\n/** Ease out with overshoot (back) */\nexport function easeOutBack(x: number): number {\n const c1 = 1.70158;\n const c3 = c1 + 1;\n return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** Ease in-out with overshoot (back) */\nexport function easeInOutBack(x: number): number {\n const c1 = 1.70158;\n const c2 = c1 * 1.525;\n\n return x < 0.5\n ? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2\n : (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;\n}\n\n// Elastic\n\n/** Ease in with elastic bounce */\nexport function easeInElastic(x: number): number {\n const c4 = (2 * Math.PI) / 3;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * c4);\n}\n\n/** Ease out with elastic bounce */\nexport function easeOutElastic(x: number): number {\n const c4 = (2 * Math.PI) / 3;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : Math.pow(2, -10 * x) * Math.sin((x * 10 - 0.75) * c4) + 1;\n}\n\n/** Ease in-out with elastic bounce */\nexport function easeInOutElastic(x: number): number {\n const c5 = (2 * Math.PI) / 4.5;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : x < 0.5\n ? -(Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * c5)) / 2\n : (Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * c5)) / 2 +\n 1;\n}\n\n// Bounce\n\n/** Ease out with bounce */\nexport function easeOutBounce(x: number): number {\n const n1 = 7.5625;\n const d1 = 2.75;\n\n if (x < 1 / d1) {\n return n1 * x * x;\n } else if (x < 2 / d1) {\n return n1 * (x -= 1.5 / d1) * x + 0.75;\n } else if (x < 2.5 / d1) {\n return n1 * (x -= 2.25 / d1) * x + 0.9375;\n } else {\n return n1 * (x -= 2.625 / d1) * x + 0.984375;\n }\n}\n\n/** Ease in with bounce */\nexport function easeInBounce(x: number): number {\n return 1 - easeOutBounce(1 - x);\n}\n\n/** Ease in-out with bounce */\nexport function easeInOutBounce(x: number): number {\n return x < 0.5\n ? (1 - easeOutBounce(1 - 2 * x)) / 2\n : (1 + easeOutBounce(2 * x - 1)) / 2;\n}\n"],"mappings":";AAKO,SAAS,MAAM,OAAyB;AAC7C,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,GAAG,CAAC,GAAG,MAAM,IAAI,KAAK;AAC1D;;;ACPA,OAAO,QAAQ;AAoBf,gBAAuB,MACrB,OACA,IACA,UAAoC;AAAA,EAClC,aAAa,GAAG,qBAAqB;AACvC,GACmB;AACnB,MAAI,UAAU,GAAG;AACf;AAAA,EACF;AAEA,QAAM,QAAQ,MAAM,KAAK;AACzB,QAAM,iBAAiB,KAAK;AAAA,IAC1B;AAAA,IACA,QAAQ,eAAe,GAAG,qBAAqB;AAAA,EACjD;AACA,QAAM,cAAc,oBAAI,IAAwB;AAEhD,MAAI,mBAAmB;AACvB,MAAI,mBAAmB;AAEvB,QAAM,YAAY,CAAC,UAAwB;AACzC,UAAM,QAAQ,MAAM,KAAK;AACzB,UAAM,QAAQ,MAAM,QAAQ,CAAC,KAAK;AAClC,UAAM,UAAU,GAAG,EAAE,OAAO,MAAM,GAAG,KAAK;AAC1C,gBAAY,IAAI,OAAO,OAAO;AAAA,EAChC;AAEA,QAAM,uBAAuB,MAAY;AACvC,WAAO,mBAAmB,SAAS,YAAY,OAAO,gBAAgB;AACpE,gBAAU,kBAAkB;AAAA,IAC9B;AAAA,EACF;AAEA,uBAAqB;AAErB,SAAO,mBAAmB,OAAO;AAC/B,UAAM,cAAc,YAAY,IAAI,gBAAgB;AAEpD,QAAI,CAAC,aAAa;AAChB,YAAM,IAAI;AAAA,QACR,iCAAiC,gBAAgB;AAAA,MACnD;AAAA,IACF;AAEA,UAAM,SAAS,MAAM;AACrB,gBAAY,OAAO,gBAAgB;AAC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IAAI;AACd,WAAO,KAAK,IAAI;AAAA,EAClB,WAAW,IAAI,IAAI,IAAI;AACrB,WAAO,MAAM,KAAK,MAAM,MAAM,IAAI;AAAA,EACpC,WAAW,IAAI,MAAM,IAAI;AACvB,WAAO,MAAM,KAAK,OAAO,MAAM,IAAI;AAAA,EACrC,OAAO;AACL,WAAO,MAAM,KAAK,QAAQ,MAAM,IAAI;AAAA,EACtC;AACF;AAGO,SAAS,aAAa,GAAmB;AAC9C,SAAO,IAAI,cAAc,IAAI,CAAC;AAChC;AAGO,SAAS,gBAAgB,GAAmB;AACjD,SAAO,IAAI,OACN,IAAI,cAAc,IAAI,IAAI,CAAC,KAAK,KAChC,IAAI,cAAc,IAAI,IAAI,CAAC,KAAK;AACvC;","names":[]}
1
+ {"version":3,"sources":["../src/steps.ts","../src/tween.ts","../src/easing.ts"],"sourcesContent":["/**\n * Generate an array of step values for an animation\n * @param count Number of steps\n * @returns Array of step values from 0 to (count-1)/count\n */\nexport function steps(count: number): number[] {\n return Array.from({ length: count }, (_, i) => i / count);\n}\n","import os from \"os\";\nimport { steps } from \"./steps\";\n\n// Default concurrency: available cores, capped at 8. Frame callbacks are\n// dominated by synchronous draw work on the JS main thread, which cannot run\n// in parallel — extra concurrency only overlaps the async portions (e.g.\n// native encoding on libuv's threadpool, which defaults to 4 threads).\n// Benchmarks show throughput saturates around 4–8 in-flight frames, while\n// in-order yielding retains one completed frame buffer per slot, so peak\n// memory keeps growing with each extra slot. Pass `options.concurrency` to\n// override (e.g. alongside a larger `UV_THREADPOOL_SIZE`).\nconst MAX_DEFAULT_CONCURRENCY = 8;\nconst defaultConcurrency = (): number =>\n Math.min(os.availableParallelism(), MAX_DEFAULT_CONCURRENCY);\n\n/**\n * Tween interval representing a frame's position in the animation\n */\nexport type TweenInterval = {\n /** Lower bound (inclusive), in range [0, 1) */\n lower: number;\n /** Upper bound (exclusive for all but last frame), in range (0, 1] */\n upper: number;\n};\n\n/**\n * Tween frames with concurrency control\n * @param count Number of frames to generate\n * @param fn Function that takes a tween interval and index, returns a promise\n * @param options Configuration options; `concurrency` defaults to the number\n * of available cores capped at 8 (concurrency overlaps async work such as\n * encoding, not the synchronous draw)\n * @yields Resulting frames in order\n */\nexport async function* tween<T>(\n count: number,\n fn: (interval: TweenInterval, index: number) => Promise<T>,\n options: { concurrency?: number } = {\n concurrency: defaultConcurrency(),\n },\n): AsyncGenerator<T> {\n if (count === 0) {\n return;\n }\n\n const range = steps(count);\n const maxConcurrency = Math.max(\n 1,\n options.concurrency ?? defaultConcurrency(),\n );\n const activeTasks = new Map<number, Promise<T>>();\n\n let nextIndexToStart = 0;\n let nextIndexToYield = 0;\n\n const startTask = (index: number): void => {\n const lower = range[index];\n const upper = range[index + 1] ?? 1;\n const promise = fn({ lower, upper }, index);\n activeTasks.set(index, promise);\n };\n\n const fillConcurrencySlots = (): void => {\n while (nextIndexToStart < count && activeTasks.size < maxConcurrency) {\n startTask(nextIndexToStart++);\n }\n };\n\n fillConcurrencySlots();\n\n while (nextIndexToYield < count) {\n const taskPromise = activeTasks.get(nextIndexToYield);\n\n if (!taskPromise) {\n throw new Error(\n `Internal error: task at index ${nextIndexToYield} not found in active tasks`,\n );\n }\n\n const result = await taskPromise;\n activeTasks.delete(nextIndexToYield);\n yield result;\n\n fillConcurrencySlots();\n nextIndexToYield++;\n }\n}\n\n/**\n * Tween frames with concurrency control, returning an array\n * @param count Number of frames to generate\n * @param fn Function that takes a tween interval and index, returns a promise\n * @param options Configuration options; `concurrency` defaults to the number\n * of available cores capped at 8 (concurrency overlaps async work such as\n * encoding, not the synchronous draw)\n * @returns Promise resolving to array of resulting frames\n */\nexport async function tweenToArray<T>(\n count: number,\n fn: (interval: TweenInterval, index: number) => Promise<T>,\n options: { concurrency?: number } = {\n concurrency: defaultConcurrency(),\n },\n): Promise<T[]> {\n return Array.fromAsync(tween(count, fn, options));\n}\n","// ============================================\n// Easing functions\n// See: https://easings.net/\n// ============================================\n\n/** Linear interpolation (no easing) */\nexport function linear(x: number): number {\n return x;\n}\n\n// Sine\n\n/** Ease in using sine curve */\nexport function easeInSine(x: number): number {\n return 1 - Math.cos((x * Math.PI) / 2);\n}\n\n/** Ease out using sine curve */\nexport function easeOutSine(x: number): number {\n return Math.sin((x * Math.PI) / 2);\n}\n\n/** Ease in-out using sine curve */\nexport function easeInOutSine(x: number): number {\n return -(Math.cos(Math.PI * x) - 1) / 2;\n}\n\n// Quad\n\n/** Ease in using quadratic curve */\nexport function easeInQuad(x: number): number {\n return x * x;\n}\n\n/** Ease out using quadratic curve */\nexport function easeOutQuad(x: number): number {\n return 1 - Math.pow(1 - x, 2);\n}\n\n/** Ease in-out using quadratic curve */\nexport function easeInOutQuad(x: number): number {\n return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;\n}\n\n// Cubic\n\n/** Ease in using cubic curve */\nexport function easeInCubic(x: number): number {\n return x * x * x;\n}\n\n/** Ease out using cubic curve */\nexport function easeOutCubic(x: number): number {\n return 1 - Math.pow(1 - x, 3);\n}\n\n/** Ease in-out using cubic curve */\nexport function easeInOutCubic(x: number): number {\n return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;\n}\n\n// Quart\n\n/** Ease in using quartic curve */\nexport function easeInQuart(x: number): number {\n return x * x * x * x;\n}\n\n/** Ease out using quartic curve */\nexport function easeOutQuart(x: number): number {\n return 1 - Math.pow(1 - x, 4);\n}\n\n/** Ease in-out using quartic curve */\nexport function easeInOutQuart(x: number): number {\n return x < 0.5 ? 8 * x * x * x * x : 1 - Math.pow(-2 * x + 2, 4) / 2;\n}\n\n// Quint\n\n/** Ease in using quintic curve */\nexport function easeInQuint(x: number): number {\n return x * x * x * x * x;\n}\n\n/** Ease out using quintic curve */\nexport function easeOutQuint(x: number): number {\n return 1 - Math.pow(1 - x, 5);\n}\n\n/** Ease in-out using quintic curve */\nexport function easeInOutQuint(x: number): number {\n return x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.pow(-2 * x + 2, 5) / 2;\n}\n\n// Expo\n\n/** Ease in using exponential curve */\nexport function easeInExpo(x: number): number {\n return x === 0 ? 0 : Math.pow(2, 10 * x - 10);\n}\n\n/** Ease out using exponential curve */\nexport function easeOutExpo(x: number): number {\n return x === 1 ? 1 : 1 - Math.pow(2, -10 * x);\n}\n\n/** Ease in-out using exponential curve */\nexport function easeInOutExpo(x: number): number {\n return x === 0\n ? 0\n : x === 1\n ? 1\n : x < 0.5\n ? Math.pow(2, 20 * x - 10) / 2\n : (2 - Math.pow(2, -20 * x + 10)) / 2;\n}\n\n// Circ\n\n/** Ease in using circular curve */\nexport function easeInCirc(x: number): number {\n return 1 - Math.sqrt(1 - Math.pow(x, 2));\n}\n\n/** Ease out using circular curve */\nexport function easeOutCirc(x: number): number {\n return Math.sqrt(1 - Math.pow(x - 1, 2));\n}\n\n/** Ease in-out using circular curve */\nexport function easeInOutCirc(x: number): number {\n return x < 0.5\n ? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2\n : (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2;\n}\n\n// Back\n\n/** Ease in with overshoot (back) */\nexport function easeInBack(x: number): number {\n const c1 = 1.70158;\n const c3 = c1 + 1;\n return c3 * x * x * x - c1 * x * x;\n}\n\n/** Ease out with overshoot (back) */\nexport function easeOutBack(x: number): number {\n const c1 = 1.70158;\n const c3 = c1 + 1;\n return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** Ease in-out with overshoot (back) */\nexport function easeInOutBack(x: number): number {\n const c1 = 1.70158;\n const c2 = c1 * 1.525;\n\n return x < 0.5\n ? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2\n : (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;\n}\n\n// Elastic\n\n/** Ease in with elastic bounce */\nexport function easeInElastic(x: number): number {\n const c4 = (2 * Math.PI) / 3;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * c4);\n}\n\n/** Ease out with elastic bounce */\nexport function easeOutElastic(x: number): number {\n const c4 = (2 * Math.PI) / 3;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : Math.pow(2, -10 * x) * Math.sin((x * 10 - 0.75) * c4) + 1;\n}\n\n/** Ease in-out with elastic bounce */\nexport function easeInOutElastic(x: number): number {\n const c5 = (2 * Math.PI) / 4.5;\n return x === 0\n ? 0\n : x === 1\n ? 1\n : x < 0.5\n ? -(Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * c5)) / 2\n : (Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * c5)) / 2 +\n 1;\n}\n\n// Bounce\n\n/** Ease out with bounce */\nexport function easeOutBounce(x: number): number {\n const n1 = 7.5625;\n const d1 = 2.75;\n\n if (x < 1 / d1) {\n return n1 * x * x;\n } else if (x < 2 / d1) {\n return n1 * (x -= 1.5 / d1) * x + 0.75;\n } else if (x < 2.5 / d1) {\n return n1 * (x -= 2.25 / d1) * x + 0.9375;\n } else {\n return n1 * (x -= 2.625 / d1) * x + 0.984375;\n }\n}\n\n/** Ease in with bounce */\nexport function easeInBounce(x: number): number {\n return 1 - easeOutBounce(1 - x);\n}\n\n/** Ease in-out with bounce */\nexport function easeInOutBounce(x: number): number {\n return x < 0.5\n ? (1 - easeOutBounce(1 - 2 * x)) / 2\n : (1 + easeOutBounce(2 * x - 1)) / 2;\n}\n"],"mappings":";AAKO,SAAS,MAAM,OAAyB;AAC7C,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,GAAG,CAAC,GAAG,MAAM,IAAI,KAAK;AAC1D;;;ACPA,OAAO,QAAQ;AAWf,IAAM,0BAA0B;AAChC,IAAM,qBAAqB,MACzB,KAAK,IAAI,GAAG,qBAAqB,GAAG,uBAAuB;AAqB7D,gBAAuB,MACrB,OACA,IACA,UAAoC;AAAA,EAClC,aAA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cAAc,IAAI,IAAI,CAAC,KAAK;AACvC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effing/tween",
3
- "version": "0.38.4",
3
+ "version": "0.40.0",
4
4
  "description": "Tweening and easing functions for animations",
5
5
  "repository": {
6
6
  "type": "git",