diff --git a/package.json b/package.json index d571a73..1809c8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "litecanvas", - "version": "0.102.3", + "version": "0.103.0", "description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.", "license": "MIT", "author": "Luiz Bills ", diff --git a/samples/camera-follow/camera-follow.js b/samples/camera-follow/camera-follow.js index 5f5e599..9505a33 100644 --- a/samples/camera-follow/camera-follow.js +++ b/samples/camera-follow/camera-follow.js @@ -6,7 +6,7 @@ function init() { function update(dt) { // animate the actor - actor.x = wave(W / 2 - 200, W / 2 + 200, T) + actor.x = W / 2 + sin(T) * 200 } function draw() { diff --git a/samples/clip/clip.js b/samples/clip/clip.js index 8b5ce3b..8980440 100644 --- a/samples/clip/clip.js +++ b/samples/clip/clip.js @@ -30,14 +30,9 @@ function draw() { cls(0) push() if (state === 0) { - clipcirc(x, y, wave(size - 20, size + 20, T * 2)) + clipcirc(x, y, size + sin(T * 5) * 20) } else if (state === 1) { - cliprect( - x, - y, - wave(size - 100, size + 100, T * 5), - wave(size - 100, size + 100, T * 5, cos) - ) + cliprect(x, y, size + sin(T * 5) * 100, size + cos(T * 5) * 100) } else if (state === 2) { clipheart(x, y, size, size) } diff --git a/samples/shapes/shapes.js b/samples/shapes/shapes.js index 5bef5dd..2e6c3ac 100644 --- a/samples/shapes/shapes.js +++ b/samples/shapes/shapes.js @@ -25,7 +25,6 @@ function update(dt) { function draw() { cls(0) - linewidth(11 - wave(-10, 10, T * 10)) for (let i = 0; i < 100; i++) { push() diff --git a/src/index.js b/src/index.js index 0f022fc..acb8b35 100644 --- a/src/index.js +++ b/src/index.js @@ -217,6 +217,24 @@ export default function litecanvas(settings = {}) { return value }, + /** + * Calculates the distance between a point (x1, y1) to another (x2, y2). + * + * @param {number} x1 + * @param {number} y1 + * @param {number} x2 + * @param {number} y2 + * @returns {number} + */ + dist: (x1, y1, x2, y2) => { + DEV: assert(isNumber(x1), '[litecanvas] dist() 1st param must be a number') + DEV: assert(isNumber(y1), '[litecanvas] dist() 2nd param must be a number') + DEV: assert(isNumber(x2), '[litecanvas] dist() 3rd param must be a number') + DEV: assert(isNumber(y2), '[litecanvas] dist() 4th param must be a number') + + return math.hypot(x2 - x1, y2 - y1) + }, + /** * Wraps a number between `min` (inclusive) and `max` (exclusive). * @@ -286,25 +304,6 @@ export default function litecanvas(settings = {}) { return instance.map(value, start, stop, 0, 1) }, - /** - * Interpolate between 2 values using a periodic function. - * - * @param {number} from - the lower bound - * @param {number} to - the higher bound - * @param {number} t - value passed to the periodic function - * @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`) - */ - wave: (from, to, t, fn = Math.sin) => { - DEV: assert(isNumber(from), '[litecanvas] wave() 1st param must be a number') - DEV: assert(isNumber(to), '[litecanvas] wave() 2nd param must be a number') - DEV: assert(isNumber(t), '[litecanvas] wave() 3rd param must be a number') - DEV: assert( - 'function' === typeof fn, - '[litecanvas] wave() 4rd param must be a function (n: number) => number' - ) - return from + ((fn(t) + 1) / 2) * (to - from) - }, - /** RNG API */ /** * Generates a pseudorandom float between min (inclusive) and max (exclusive) diff --git a/src/version.js b/src/version.js index 42c4d81..a2e80e8 100644 --- a/src/version.js +++ b/src/version.js @@ -1,2 +1,2 @@ // Generated by genversion. -export const version = '0.102.3' +export const version = '0.103.0' diff --git a/tests/math.js b/tests/math.js index bd043ef..365878b 100644 --- a/tests/math.js +++ b/tests/math.js @@ -11,6 +11,8 @@ test.before(() => { local = litecanvas({ global: false, }) + + local.listen('init', () => local.pause()) }) test.after(() => { @@ -83,21 +85,8 @@ test('round', async (t) => { t.is(local.round(n, 5), 9.87654) }) -test('wave', async (t) => { - { - // interpolate from 0 to 100 using Math.sin (default) - const amount = 0 - const expected = 50 - const actual = local.wave(0, 100, amount) - t.is(actual, expected) - } - - { - // interpolate from 0 to 100 using a custom periodic function - const amount = 0 - const fn = (x) => -Math.cos(x) - const expected = 0 - const actual = local.wave(0, 100, amount, fn) - t.is(actual, expected) - } +test('dist', async (t) => { + const expected = 100 + const actual = local.dist(0, 0, 0, 100) + t.is(actual, expected) }) diff --git a/types/global.d.ts b/types/global.d.ts index 1b0d66d..99b5853 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -116,14 +116,15 @@ declare global { */ function norm(value: number, start: number, stop: number): number /** - * Interpolate between 2 values using a periodic function. + * Calculates the distance between a point (x1, y1) to another (x2, y2). * - * @param from - the lower bound - * @param to - the higher bound - * @param t - the value passed to the periodic function - * @param fn= - the periodic function (which default to `Math.sin`) + * @param x1 + * @param y1 + * @param x2 + * @param y2 + * @returns the distance */ - function wave(from: number, to: number, t: number, fn?: (n: number) => number): number + function dist(x1: number, y1: number, x2: number, y2: number): number /** * Returns the sine of a number in radians */ diff --git a/types/types.d.ts b/types/types.d.ts index 9ff6061..4bd3554 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -110,14 +110,15 @@ type LitecanvasInstance = { */ norm(value: number, start: number, stop: number): number /** - * Interpolate between 2 values using a periodic function. + * Calculates the distance between a point (x1, y1) to another (x2, y2). * - * @param from - the lower bound - * @param to - the higher bound - * @param t - the value passed to the periodic function - * @param fn - the periodic function (which default to `Math.sin`) + * @param x1 + * @param y1 + * @param x2 + * @param y2 + * @returns the distance */ - wave(from: number, to: number, t: number, fn?: (n: number) => number): number + dist(x1: number, y1: number, x2: number, y2: number): number /** * Returns the sine of a number in radians */