Skip to content

Commit daf4bcb

Browse files
committed
v0.103
* add `dist()` * remove `wave()` (moved to @litecanvas/utils)
1 parent 3d5efdc commit daf4bcb

File tree

6 files changed

+40
-50
lines changed

6 files changed

+40
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "litecanvas",
3-
"version": "0.102.3",
3+
"version": "0.103.0",
44
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
55
"license": "MIT",
66
"author": "Luiz Bills <luizbills@pm.me>",

src/index.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ export default function litecanvas(settings = {}) {
217217
return value
218218
},
219219

220+
/**
221+
* Calculates the distance between a point (x1, y1) to another (x2, y2).
222+
*
223+
* @param {number} x1
224+
* @param {number} y1
225+
* @param {number} x2
226+
* @param {number} y2
227+
* @returns {number}
228+
*/
229+
dist: (x1, y1, x2, y2) => {
230+
DEV: assert(isNumber(x1), '[litecanvas] dist() 1st param must be a number')
231+
DEV: assert(isNumber(y1), '[litecanvas] dist() 2nd param must be a number')
232+
DEV: assert(isNumber(x2), '[litecanvas] dist() 3rd param must be a number')
233+
DEV: assert(isNumber(y2), '[litecanvas] dist() 4th param must be a number')
234+
235+
return math.hypot(x2 - x1, y2 - y1)
236+
},
237+
220238
/**
221239
* Wraps a number between `min` (inclusive) and `max` (exclusive).
222240
*
@@ -286,25 +304,6 @@ export default function litecanvas(settings = {}) {
286304
return instance.map(value, start, stop, 0, 1)
287305
},
288306

289-
/**
290-
* Interpolate between 2 values using a periodic function.
291-
*
292-
* @param {number} from - the lower bound
293-
* @param {number} to - the higher bound
294-
* @param {number} t - value passed to the periodic function
295-
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
296-
*/
297-
wave: (from, to, t, fn = Math.sin) => {
298-
DEV: assert(isNumber(from), '[litecanvas] wave() 1st param must be a number')
299-
DEV: assert(isNumber(to), '[litecanvas] wave() 2nd param must be a number')
300-
DEV: assert(isNumber(t), '[litecanvas] wave() 3rd param must be a number')
301-
DEV: assert(
302-
'function' === typeof fn,
303-
'[litecanvas] wave() 4rd param must be a function (n: number) => number'
304-
)
305-
return from + ((fn(t) + 1) / 2) * (to - from)
306-
},
307-
308307
/** RNG API */
309308
/**
310309
* Generates a pseudorandom float between min (inclusive) and max (exclusive)

src/version.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/math.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ test.before(() => {
1111
local = litecanvas({
1212
global: false,
1313
})
14+
15+
local.listen('init', () => local.pause())
1416
})
1517

1618
test.after(() => {
@@ -83,21 +85,8 @@ test('round', async (t) => {
8385
t.is(local.round(n, 5), 9.87654)
8486
})
8587

86-
test('wave', async (t) => {
87-
{
88-
// interpolate from 0 to 100 using Math.sin (default)
89-
const amount = 0
90-
const expected = 50
91-
const actual = local.wave(0, 100, amount)
92-
t.is(actual, expected)
93-
}
94-
95-
{
96-
// interpolate from 0 to 100 using a custom periodic function
97-
const amount = 0
98-
const fn = (x) => -Math.cos(x)
99-
const expected = 0
100-
const actual = local.wave(0, 100, amount, fn)
101-
t.is(actual, expected)
102-
}
88+
test('dist', async (t) => {
89+
const expected = 100
90+
const actual = local.dist(0, 0, 0, 100)
91+
t.is(actual, expected)
10392
})

types/global.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ declare global {
116116
*/
117117
function norm(value: number, start: number, stop: number): number
118118
/**
119-
* Interpolate between 2 values using a periodic function.
119+
* Calculates the distance between a point (x1, y1) to another (x2, y2).
120120
*
121-
* @param from - the lower bound
122-
* @param to - the higher bound
123-
* @param t - the value passed to the periodic function
124-
* @param fn= - the periodic function (which default to `Math.sin`)
121+
* @param x1
122+
* @param y1
123+
* @param x2
124+
* @param y2
125+
* @returns the distance
125126
*/
126-
function wave(from: number, to: number, t: number, fn?: (n: number) => number): number
127+
function dist(x1: number, y1: number, x2: number, y2: number): number
127128
/**
128129
* Returns the sine of a number in radians
129130
*/

types/types.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ type LitecanvasInstance = {
110110
*/
111111
norm(value: number, start: number, stop: number): number
112112
/**
113-
* Interpolate between 2 values using a periodic function.
113+
* Calculates the distance between a point (x1, y1) to another (x2, y2).
114114
*
115-
* @param from - the lower bound
116-
* @param to - the higher bound
117-
* @param t - the value passed to the periodic function
118-
* @param fn - the periodic function (which default to `Math.sin`)
115+
* @param x1
116+
* @param y1
117+
* @param x2
118+
* @param y2
119+
* @returns the distance
119120
*/
120-
wave(from: number, to: number, t: number, fn?: (n: number) => number): number
121+
dist(x1: number, y1: number, x2: number, y2: number): number
121122
/**
122123
* Returns the sine of a number in radians
123124
*/

0 commit comments

Comments
 (0)