Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <luizbills@pm.me>",
Expand Down
2 changes: 1 addition & 1 deletion samples/camera-follow/camera-follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 2 additions & 7 deletions samples/clip/clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion samples/shapes/shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
37 changes: 18 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 6 additions & 17 deletions tests/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test.before(() => {
local = litecanvas({
global: false,
})

local.listen('init', () => local.pause())
})

test.after(() => {
Expand Down Expand Up @@ -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)
})
13 changes: 7 additions & 6 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
13 changes: 7 additions & 6 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down