Skip to content

Instantly share code, notes, and snippets.

@LucasIron
LucasIron / game__loop.js
Last active October 1, 2018 16:28
requestAnimationFrame Game Loop with delta time calculation
export default callback => requestAnimationFrame((function loop(then) {
return now => {
const delta = now - then;
callback(delta);
return requestAnimationFrame(loop(now));
}
})(performance.now()));
@LucasIron
LucasIron / overlap.js
Created August 1, 2018 21:49
Overlap for N dimensions function
const overlap = baseLines => comparisonLines => baseLines.reduce((bool, baseLine, index) => {
const comparisonLine = comparisonLines[index];
if (bool === true) return Math.max(baseLine[0], comparisonLine[0]) < Math.min(baseLine[1], comparisonLine[1]);
else return false;
}, true);
@LucasIron
LucasIron / fetch_form.js
Last active August 22, 2018 19:20
fetch a form passing its input values as querystring parameters;
form => fetch(form.getAttribute('action') + [].reduce.call(form.querySelectorAll('[name]'), (params, input) => params + encodeURI(input.getAttribute('name') + '=' + input.value + '&'), '?')
@LucasIron
LucasIron / debounce_throttle.js
Last active January 21, 2021 19:04
Debounce & Throttle
debounce = (delay, cancel) => (listener, wait) => {
let delayId
return function(...args) {
cancel(delayId)
return delayId = delay(() => listener.apply(this, args), wait)
}
}
debounceTimeout = debounce(setTimeout, clearTimeout)
debounceAnimationFrame = debounce(requestAnimationFrame, cancelAnimationFrame)