forked from onedayitwillmake/CirclePackingJS
-
Notifications
You must be signed in to change notification settings - Fork 14
/
typescript.ts
67 lines (59 loc) · 1.52 KB
/
typescript.ts
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
import { CirclePacker, pack } from '../dist/circlepacker.node.mjs';
// CirclePacker Node ES Module Example
// run node node-es-module.mjs to run this example
const circles = generateRandomCircles();
const bounds = { width: 300, height: 300 };
const target = { x: bounds.width / 2, y: bounds.height / 2 };
// example using CirclePacker instance
new CirclePacker({
animationLoop: false,
noWorker: true,
circles,
bounds,
target,
correctionPasses: 20,
calculateOverlap: true,
onMove(circles, target, overlappingCircles) {
console.log('calculated by circlepacker instance', { circles, overlappingCircles });
},
});
// example using pack function
const { updatedCircles, overlappingCircles } = await pack({
circles,
bounds,
target,
calculateOverlap: true,
correctionPasses: 20,
});
console.log('pack() result:', { updatedCircles, overlappingCircles });
// generate random circle data
function generateRandomCircles(
params = {
circleCount: 10,
containerHeight: 300,
containerWidth: 300,
minRadius: 10,
maxRadius: 50,
}
) {
const {
circleCount = 10,
containerHeight = 300,
containerWidth = 300,
minRadius = 10,
maxRadius = 50,
} = params;
return [...new Array(circleCount).keys()].map(index => {
const radius = Math.round(minRadius + Math.random() * (maxRadius - minRadius));
const x = Math.round(Math.random() * (containerWidth - radius));
const y = Math.round(Math.random() * (containerHeight - radius));
return {
id: `circle-${index + 1}`,
position: {
x,
y,
},
radius,
};
});
}