-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashPosition.js
More file actions
212 lines (212 loc) · 6.33 KB
/
hashPosition.js
File metadata and controls
212 lines (212 loc) · 6.33 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function hashPosition(box, params = {}) {
box.style.position = "relative"
if (typeof params === "number") {
params = {
minWidth: params,
maxWidth: params,
}
} else if (params.width) {
params.minWidth = params.minWidth || params.width
params.maxWidth = params.maxWidth || params.width
}
let { minWidth, maxWidth, space } = params
const zeroSpace = minWidth === maxWidth
space = space || maxWidth * 0.2
maxWidth = zeroSpace ? maxWidth + space : maxWidth
let boxWidth = box.scrollWidth
let boxHeight = box.scrollHeight
// 获取圆心
let centerX = boxWidth / 2
let centerY = boxHeight / 2
// 在box区域获取可用区域
let maxBoxWidth = boxWidth > boxHeight ? boxWidth : boxHeight
let minBoxWidth = boxWidth < boxHeight ? boxWidth : boxHeight
// 设置圆环数量
let sumI = Math.round(maxBoxWidth / 2 / maxWidth) - 1
// 环初始数据 保存可用弧度
// 环占用数据 一个环的数据[[a,b],[a,b]] a位占用弧度位置 b为弧度宽度 根据a排序push
let ringData = []
let ringInitData = []
for (let i = 0; i < sumI; i++) {
let radius = (i + 1) * maxWidth
let b = minBoxWidth / 2
if (b >= radius) {
let c = Math.PI / 2
ringInitData[i] = [
[-c, c],
[-c + Math.PI, c + Math.PI],
]
} else {
let a = Math.atan(b / Math.sqrt(radius * radius - b * b))
ringInitData[i] = [
[-a, a],
[-a + Math.PI, a + Math.PI],
]
}
ringData[i] = []
}
// 寻找可用范围 maxData环整体范围 useData环已使用范围(已排序) needWidth需求宽度
let findAvailableData = (maxData, useData = [], needWidth) => {
if (useData.length === 0) {
return maxData
}
let result = []
let maxDataA = maxData[0]
let maxDataB = maxData[1]
for (let i = 0; i < useData.length; i++) {
let start = useData[i][0] - useData[i][1] / 2
let end = useData[i][0] + useData[i][1] / 2
// 0时前后都要比较
if (i === 0) {
if (maxDataA[1] > useData[i][0]) {
// 表明最大区间前区间已用
if (Math.abs(maxDataA[0] - start) >= needWidth) {
result.push([maxDataA[0], start])
}
} else {
// 表明最大区间前区间未用
result.push(maxDataA)
// 那肯定时在后区间了
if (Math.abs(maxDataB[0] - start) >= needWidth) {
result.push([maxDataB[0], start])
}
}
}
// 非零只向后比较
// 拿到后比较值
if (i < useData.length - 1) {
// 非最后一个块时拿后一个块的开始
// 判断后一个块是否和当前在同一区间
let spaceWidth
let needEnd
if (
(useData[i][0] < maxDataA[1] && useData[i + 1][0] < maxDataA[1]) ||
(useData[i][0] > maxDataB[0] && useData[i + 1][0] > maxDataB[0])
) {
// 两个块在同一区间
spaceWidth = useData[i + 1][0] - useData[i + 1][1] / 2 - end
needEnd = useData[i + 1][0] - useData[i + 1][1] / 2
} else {
spaceWidth = maxDataA[1] - end
needEnd = maxDataA[1]
}
if (spaceWidth >= needWidth) {
result.push([end, needEnd])
}
} else {
let comEnd
// 最后一个块拿区间的最后
if (start > maxDataB[0]) {
// 最后一个在最大区间后区间
comEnd = maxDataB[1]
} else {
comEnd = maxDataA[1]
result.push(maxDataB)
}
if (comEnd - end >= needWidth) {
result.push([end, comEnd])
}
}
}
return result
}
let pushUsePoint = (index, location, width) => {
let ringUserData = ringData[index]
let status = false
let bak = [location, width]
for (let i = 0; i < ringUserData.length; i++) {
let [a] = ringUserData[i]
if (a > location && !status) {
status = true
}
if (status) {
let bak1 = ringUserData[i]
ringUserData[i] = bak
bak = bak1
}
}
ringUserData.push(bak)
return location
}
// 在换上寻找位置 找到返回角度,无返回null
let findLocation = (index, width) => {
let radius = (index + 1) * maxWidth
// 当前需求的角度
let needRadius = Math.atan(width / radius) * 2
let ringUserData = ringData[index]
let canNeedData = findAvailableData(
ringInitData[index],
ringUserData,
needRadius
)
if (canNeedData.length === 0) {
// 未找到可用位置
return null
}
let a3 = parseInt(Math.random() * canNeedData.length)
let [start, end] = canNeedData[a3]
start += needRadius / 2
end -= needRadius / 2
return pushUsePoint(
index,
start + Math.random() * (end - start),
needRadius
)
}
let getXY = () => {
const a = maxWidth - minWidth
// 随机到某个环上
let index
// 随机宽度
let count = 0
const itemWidthSet = () => {
return minWidth + Math.random() * a
}
let itemWidth = itemWidthSet()
// 去环上找空位
let userIndex = []
// 拿到放置角度
let resultRadius = null
for (; resultRadius === null; ) {
if (userIndex.length >= sumI) {
count++
if (count > 5) {
return null
}
itemWidth = itemWidthSet()
userIndex = []
}
index = parseInt(Math.random() * sumI)
if (userIndex.includes(index)) {
continue
}
userIndex.push(index)
resultRadius = findLocation(index, itemWidth)
}
let ab = a / 2 - 5
ab = ab < 0 ? 0 : ab
let randomWidth = (index + 1) * maxWidth - ab + Math.random() * 2 * ab
let x = randomWidth * Math.cos(resultRadius)
let y = randomWidth * Math.sin(resultRadius)
return [centerX + x, centerY + y, itemWidth]
}
let id = 0
const children = box.getElementsByClassName("hash-item")
for (let child of children) {
let result = getXY()
if (result) {
let [x, y, width] = result
width = zeroSpace ? minWidth : width
child.style.position = "absolute"
child.style.left = x + "px"
child.style.top = y + "px"
if (!zeroSpace) {
child.style.width = width + "px"
child.style.height = width + "px"
}
} else {
child.style.display = "none"
console.log("未找到位置")
}
}
}