Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / shuffle.js
Created December 21, 2015 11:13
Randomize arrays
const shuffle = (arr, mixed = [], pool = arr.slice()) => {
const remaining = pool.length;
if (!remaining) return mixed;
const index = Math.floor(Math.random() * remaining);
const el = pool.splice(index, 1).pop();
mixed.push(el);
return shuffle(arr, mixed, pool);
};
@bendc
bendc / scrollRoot.js
Created January 5, 2016 09:20
Determines the scrolling element
const scrollRoot = (() => {
if ("scrollingElement" in document)
return document.scrollingElement;
const initial = document.documentElement.scrollTop;
document.documentElement.scrollTop = initial + 1;
const updated = document.documentElement.scrollTop;
document.documentElement.scrollTop = initial;
return updated > initial
@bendc
bendc / isObject.js
Created January 6, 2016 08:50
Check if something is an object
const isObject = obj => Object(obj) === obj;
@bendc
bendc / immutable.js
Created January 6, 2016 16:56
Deep freeze objects
const immutable = (() => {
const getValue = obj => key => obj[key];
const isObject = obj => Object(obj) === obj;
const isMutable = obj => isObject(obj) && !Object.isFrozen(obj);
return obj => {
Object.keys(obj).map(getValue(obj)).filter(isMutable).forEach(immutable);
return Object.freeze(obj);
};
})();
@bendc
bendc / iterable.js
Created January 26, 2016 09:09
Make objects iterable
const iterable = function* (obj) {
yield* Object.keys(obj).map(key => [key, obj[key]]);
};
@bendc
bendc / holy-grail.html
Last active December 15, 2017 19:50
Holy Grail Layout using CSS Grid
<!doctype html>
<title>Holy Grail Layout</title>
<style>
body {
display: grid;
grid-template-rows: 100px 1fr 100px;
grid-template-columns: 200px 1fr 200px;
min-height: 100vh;
margin: 0
@bendc
bendc / async-css.html
Created May 26, 2016 10:52
CSS delivery optimization
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, width=device-width">
<title>Defer non-essential stylesheets</title>
<style>
/* Critical above-the-fold styles */
* { margin: 0; padding: 0 }
:root { background: red }
@bendc
bendc / getPathBoundingBox.js
Created June 9, 2016 18:24
SVG path bounding box
const getPathBoundingBox = (() => {
const toPolygon = (path, points = 50) => {
const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
const total = path.getTotalLength();
const step = total / points;
const getPoints = (dist = 0, arr = []) => {
const {x, y} = path.getPointAtLength(dist);
arr.push(`${x},${y}`);
const next = step + dist;
@bendc
bendc / duplicate.js
Created June 14, 2016 20:22
Deep object copy
const duplicate = obj => JSON.parse(JSON.stringify(obj));
@bendc
bendc / display-fade.html
Created June 22, 2016 03:01
Transitioning the opacity of a hidden element
<!doctype html>
<meta charset="utf-8">
<title>Example</title>
<style>
div {
width: 100px;
height: 100px;
background: black;
animation-duration: .5s;