-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
44 lines (39 loc) · 1.2 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
export function el(name, attributes, children) {
const response = document.createElement(name)
if (attributes)
for (const attribute in attributes)
response.setAttribute(attribute, attributes[attribute])
if (children)
response.append(...children)
return response
}
export function updateSize(canvas) {
if (canvas.width !== canvas.clientWidth)
canvas.width = canvas.clientWidth
if (canvas.height !== canvas.clientHeight)
canvas.height = canvas.clientHeight
}
export function replicate(size, callback) {
const response = new Array(size)
for (let i = 0; i < size; i ++) {
response[i] = callback(i)
}
return response
}
export function lSubtract(vector1, vector2) {
return vector1.map((v1, i) => v1 - vector2[i])
}
export function lMagnitude2(vector) {
return vector.reduce((mag, v) => mag + v * v, 0)
}
export function deepClone(data) {
const response = data
if (Array.isArray(data)) {
return data.map((d) => deepClone(d))
} else if (data && typeof data === 'object') {
const response = { }
for (const key in data)
response[key] = deepClone(data[key])
}
return response
}