Files
Tessellate/src/utils.js
2016-02-19 14:28:15 -05:00

60 lines
1.1 KiB
JavaScript

export function noop() {};
// hypotenuse factor of isoscelese right triangle
export const sqrt2 = Math.sqrt(2);
// short width factor given the lenght of a hexagon's side
// (2*S gives long width)
export const sqrt3 = Math.sqrt(3);
export function throttleEvent(type, name, obj) {
obj = obj || window;
let running = false;
let throttle = () => {
if (!running) {
running = true;
requestAnimationFrame(() => {
obj.dispatchEvent(new CustomEvent(name));
running = false;
});
}
}
obj.addEventListener(type, throttle);
}
throttleEvent('resize', 'optimizedResize');
export function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
export function extend(obj, ...sources) {
sources.forEach(src => {
for (let key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key];
}
});
return obj;
}
export function hypotenuse(a, b) {
if (b == null) b = a;
return Math.sqrt(a*a + b*b);
}
export function random(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
}