Skip to content

Commit 5b06ac2

Browse files
authored
Merge pull request #264 from litecanvas/next
v0.103
2 parents 3d5efdc + adcd6d1 commit 5b06ac2

File tree

9 files changed

+43
-59
lines changed

9 files changed

+43
-59
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>",

samples/camera-follow/camera-follow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function init() {
66

77
function update(dt) {
88
// animate the actor
9-
actor.x = wave(W / 2 - 200, W / 2 + 200, T)
9+
actor.x = W / 2 + sin(T) * 200
1010
}
1111

1212
function draw() {

samples/clip/clip.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ function draw() {
3030
cls(0)
3131
push()
3232
if (state === 0) {
33-
clipcirc(x, y, wave(size - 20, size + 20, T * 2))
33+
clipcirc(x, y, size + sin(T * 5) * 20)
3434
} else if (state === 1) {
35-
cliprect(
36-
x,
37-
y,
38-
wave(size - 100, size + 100, T * 5),
39-
wave(size - 100, size + 100, T * 5, cos)
40-
)
35+
cliprect(x, y, size + sin(T * 5) * 100, size + cos(T * 5) * 100)
4136
} else if (state === 2) {
4237
clipheart(x, y, size, size)
4338
}

samples/shapes/shapes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function update(dt) {
2525

2626
function draw() {
2727
cls(0)
28-
linewidth(11 - wave(-10, 10, T * 10))
2928

3029
for (let i = 0; i < 100; i++) {
3130
push()

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)