Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / raf-basic-animation.js
Created August 24, 2017 16:45
rAF tutorial: basic animation
const element = document.querySelector("span");
const finalPosition = 600;
const time = {
start: performance.now(),
total: 2000
};
const tick = now => {
time.elapsed = now - time.start;
@bendc
bendc / raf-performance-time-tracking.js
Created August 24, 2017 15:31
rAF tutorial: performance time tracking
const time = {
start: performance.now(),
total: 2000
};
const tick = now => {
time.elapsed = now - time.start;
if (time.elapsed < time.total) requestAnimationFrame(tick);
};
@bendc
bendc / raf-basic-time-tracking.js
Created August 24, 2017 15:07
rAF tutorial: basic time tracking
const time = {
start: null,
total: 2000
};
const tick = now => {
if (!time.start) time.start = now;
time.elapsed = now - time.start;
if (time.elapsed < time.total) requestAnimationFrame(tick);
};
@bendc
bendc / raf-basic-loop.js
Created August 24, 2017 14:23
rAF tutorial: basic loop
const tick = now => {
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
@bendc
bendc / easing.js
Created March 24, 2017 13:05
Tween.js ES6 easing functions
// k = progress between 0 and 1
const easing = {
quadratic: {
in(k) {
return k * k;
},
out(k) {
return k * (2 - k);
@bendc
bendc / randomInterval.js
Created March 9, 2017 21:55
rAF-based random interval
const randomInterval = (() => {
const random = (min, max) => Math.random() * (max - min) + min;
return (callback, min, max) => {
const time = {
start: performance.now(),
total: random(min, max)
};
const tick = now => {
if (time.total <= now - time.start) {
time.start = now;
@bendc
bendc / todo-list-events.js
Created January 6, 2017 11:23
Web Components: todo-list-events
const store = (() => {
let state;
return todos => {
if (todos) {
state = todos;
render("todo-list");
}
return state;
};
})();
@bendc
bendc / todo-list-basic.js
Created January 6, 2017 10:57
Web Components: todo-list-basic
const state = ["Buy milk", "Call Sarah", "Pay bills"];
customElements.define("todo-item", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = `
<label>
<input type=checkbox>
<slot></slot>
@bendc
bendc / hello-message-attribute.js
Created January 5, 2017 13:11
Web Components: hello-message-attribute
customElements.define("hello-message", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = "Hello <slot></slot>";
}
static get observedAttributes() {
return ["name"];
}
@bendc
bendc / hello-message-slot.js
Created January 5, 2017 13:07
Web Components: hello-message-slot
customElements.define("hello-message", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = "Hello <slot></slot>";
}
});