-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
114 lines (97 loc) · 3 KB
/
Copy pathhelpers.js
File metadata and controls
114 lines (97 loc) · 3 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
export let F = (n, f) => [...Array(n | 0)].map((_, i) => f(i))
export let rot = (x, y, angle) => {
let c = Math.cos(angle),
s = Math.sin(angle)
return [x * c - y * s, x * s + y * c]
}
export let vmul = (a, s) => a.map(d => d * s)
export let sscale = (s, m) => s.map(c => c.map(p => p.map(x => x * m)))
export let strans = (s, v) => s.map(c => c.map(p => p.map((x, i) => x + v[i])))
export let srot = (s, a) => s.map(c => c.map(p => rot(...p, a)))
export let vdot = (a, b) => a[0] * b[0] + a[1] * b[1]
export let PI = Math.PI
export let vadd = (a, b) => a.map((d, i) => d + b[i])
export let vsub = (a, b) => a.map((d, i) => d - b[i])
export let round = x => Math.round(x * 100) / 100
export let vnorm = a => {
let l = Math.sqrt(vdot(a, a))
return a.map(d => d / l)
}
export let mod = (x, l) => {
return ((x % l) + l) % l
}
export function smoothstep(min, max, x) {
let flip = false
if (min > max) {
;[min, max] = [max, min]
flip = true
}
if (x <= min) return 0
if (x >= max) return 1
let t = (x - min) / (max - min)
let s = t * t * (3 - 2 * t)
if (flip) s = 1 - s
return s
}
function generateFileName(seedString) {
const now = new Date()
const day = String(now.getDate()).padStart(2, '0')
const month = String(now.getMonth() + 1).padStart(2, '0') // Месяцы начинаются с 0
const year = now.getFullYear()
const hours = String(now.getHours()).padStart(2, '0')
const minutes = String(now.getMinutes()).padStart(2, '0')
const fileName = `${seedString}_${day}_${month}_${year}_${hours}_${minutes}`
return fileName
}
export function saveSVG(PARAMS) {
const zip = new JSZip()
const wallSvg = document.querySelector('#wallpaper svg').outerHTML
const panelSvg = document.querySelector('#panel svg').outerHTML
zip.file('wallpaper.svg', wallSvg)
zip.file('panel.svg', panelSvg)
zip.generateAsync({type: 'blob'}).then(function (content) {
saveAs(content, generateFileName(PARAMS.seedString) + '.zip')
})
}
export function splitmix32(a) {
let counter = 0
return function () {
counter += 1
a |= 0
a = (a + 0x9e3779b9) | 0
var t = a ^ (a >>> 16)
t = Math.imul(t, 0x21f0aaad)
t = t ^ (t >>> 15)
t = Math.imul(t, 0x735a2d97)
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296
}
}
export let R = splitmix32(0)
export let setSeed = (seed = 0) => {
R = splitmix32(seed)
}
export function stringHash(str) {
return str
.split('')
.reduce(
(prevHash, currVal) =>
((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0,
0,
)
}
export function parseColors(colors) {
const regex = /#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6,8})\b/g
return colors.match(regex) || []
}
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(R() * (i + 1))
const temp = array[i]
array[i] = array[j]
array[j] = temp
}
return array
}
export function map(x, in_min, in_max, out_min, out_max) {
return ((x - in_min) / (in_max - in_min)) * (out_max - out_min) + out_min
}