drawing shapes on a canvas when the user clicks
This commit is contained in:
48
src/utils.js
Normal file
48
src/utils.js
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
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, 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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user