Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / assign.js
Created June 25, 2018 12:33
Unified object assignment
const assign = (target, key, value) =>
Object.assign(target, Object(key) === key ? key : {[key]: value});
@bendc
bendc / sets.js
Created January 31, 2018 10:16
Sets: Union, Intersection and Difference
const union = (...sets) =>
sets.reduce((acc, set) => {
set.forEach(val => acc.add(val));
return acc;
}, new Set);
const intersection = (...sets) =>
sets.reduce((acc, set) => {
set.forEach(val => {
if (sets.every(set => set.has(val)))
@bendc
bendc / list.js
Last active January 15, 2018 08:51
Array of arbitrary number of items
const list = (length, callback) =>
Array.from({length}, (hole, index) => callback(index));
@bendc
bendc / simulate-typing.js
Created September 1, 2017 08:57
Fake typing animation
const trackTime = timing => {
const now = performance.now();
if (!timing.startTime) timing.startTime = now;
const elapsed = now - timing.startTime;
const {duration} = timing;
if (duration != null && duration <= elapsed) timing.startTime = null;
return elapsed;
};
const delay = (callback, duration) => {
@bendc
bendc / personal-raf-boilerplate.js
Last active January 21, 2018 17:19
requestAnimationFrame boilerplate code
"use strict";
// animation utils
// ===============
const trackTime = timing => {
const now = performance.now();
if (!timing.startTime) timing.startTime = now;
const elapsed = now - timing.startTime;
@bendc
bendc / raf-boilerplate.js
Created August 28, 2017 13:52
rAF tutorial: boilerplate code
"use strict";
// animation utils
// ===============
const trackTime = id => {
const [entry] = performance.getEntriesByName(id);
if (!entry) {
performance.mark(id);
@bendc
bendc / raf-delay.js
Created August 28, 2017 13:19
rAF tutorial: better setTimeout
const delay = (callback, duration) => {
const tick = () =>
getProgress(time) < 1 ? requestAnimationFrame(tick) : callback();
const time = {
duration,
id: requestAnimationFrame(tick)
};
};
@bendc
bendc / raf-performance-mark.js
Created August 28, 2017 13:02
rAF tutorial: advanced time tracking
const trackTime = id => {
const [entry] = performance.getEntriesByName(id);
if (!entry) {
performance.mark(id);
return 0;
}
return performance.now() - entry.startTime;
};
const getProgress = ({duration, id}) => {
@bendc
bendc / raf-svg-morphing.js
Created August 25, 2017 10:28
rAF tutorial: SVG morphing
// polygon's points
const shapes = {
play: [85, 70, 180, 125, 180, 125, 85, 180],
stop: [85, 85, 165, 85, 165, 165, 85, 165]
};
const tick = now => {
// calculate the current position of each point
const points = shapes.play.map((start, index) => {
const end = shapes.stop[index];
@bendc
bendc / raf-ease-out.js
Created August 24, 2017 18:14
rAF tutorial: ease-out equation
const easeOut = progress =>
Math.pow(--progress, 5) + 1;