-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceWallpaperShapes.js
More file actions
52 lines (45 loc) · 1.35 KB
/
sliceWallpaperShapes.js
File metadata and controls
52 lines (45 loc) · 1.35 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
import {intersect} from './intersect.js'
import {union} from './union.js'
const EPS = 0.000001
const stripeWidth = 60 // 6 cm
export function sliceWallpaperShapes(PARAMS, shapes) {
let newShapes = []
shapes.forEach(shape => {
let newShape = sliceWallpaperShape(PARAMS, shape)
newShapes.push(newShape)
})
return newShapes
}
export function sliceWallpaperShape(PARAMS, shape) {
let slicesNumber = Math.ceil(PARAMS.sizeXRounded / stripeWidth) // 3 cm
let w = PARAMS.sizeXRounded
let h = PARAMS.sizeY
let sliceWidth = stripeWidth // 3 cm
let newShape = {
type: shape.type,
fill: shape.fill,
polys: [],
}
shape.polys.forEach(poly => {
for (let i = 0; i < slicesNumber; i++) {
let rectPoly = []
rectPoly.push([i * sliceWidth - EPS, 0])
rectPoly.push([i * sliceWidth - EPS, h])
rectPoly.push([(i + 1) * sliceWidth + EPS, h])
rectPoly.push([(i + 1) * sliceWidth + EPS, 0])
let newPoly = []
poly.forEach(point => {
let [x, y] = point
let sliceCenterX = sliceWidth / 2 + i * sliceWidth
x -= sliceCenterX
x /= PARAMS.scale
x += sliceCenterX
newPoly.push([x, y])
})
// intersect may return several polys
newShape.polys.push(...intersect(newPoly, rectPoly))
}
})
newShape.polys = union(newShape.polys)
return newShape
}