forked from onedayitwillmake/CirclePackingJS
-
Notifications
You must be signed in to change notification settings - Fork 14
/
continuous.html
311 lines (251 loc) · 8.18 KB
/
continuous.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>circlepacker continuous rendering demo</title>
<link rel="stylesheet" href="./example.css" />
</head>
<body>
<article class="text">
<h1>continuous drawing</h1>
<p>the circle positions are continuously updated. you can use the buttons below to update the circles and
restart the animation. you can also drag circles around.</p>
<p>this example uses the es module version of the circlepacker script.</p>
<p><a href="index.html">back to index</a></p>
</article>
<div class="container container--is-interactive"></div>
<div class="buttons">
<button id="add-circle">add circle</button>
<button id="delete-circle">delete circle</button>
<button id="random-size">random size</button>
<button id="random-target">random target</button>
<button id="pin-random">toggle pin</button>
</div>
<div class="range-container">
<label for="damping">damping</label>
<input id="damping" type="range" min="0" max="0.05" step="0.001" value="0.025" />
<span id="damping-value">0.025</span>
</div>
<script type="module">
import { CirclePacker } from '../dist/circlepacker.esm.js';
const DRAG_THRESOLD = 10;
const containerEl = document.querySelector('.container');
const addButtonEl = document.querySelector('#add-circle');
const deleteButtonEl = document.querySelector('#delete-circle');
const randomButtonEl = document.querySelector('#random-size');
const targetButtonEl = document.querySelector('#random-target');
const pinRandomButtonEl = document.querySelector('#pin-random');
const dampingInputEl = document.querySelector('#damping');
const dampingValueEl = document.querySelector('#damping-value');
// references to all circle elements
const circleEls = {};
// dimenstions of container
const rect = containerEl.getBoundingClientRect();
let bounds = { width: rect.width, height: rect.height };
let target = { x: bounds.width / 2, y: bounds.height / 2 };
let isDragging = false;
let pinnedCircleId = null;
const circles = [
createCircle(),
createCircle(),
createCircle(),
createCircle(),
createCircle()
];
const targetEl = createTargetEl();
const packer = new CirclePacker({
bounds,
target,
circles,
onMove: render,
collisionPasses: 3,
centeringPasses: 2,
correctionPasses: 5,
animationLoop: true,
});
addButtonEl.addEventListener('click', addRandomCircle);
deleteButtonEl.addEventListener('click', removeRandomCircle);
randomButtonEl.addEventListener('click', setRandomBounds);
targetButtonEl.addEventListener('click', setRandomTarget);
pinRandomButtonEl.addEventListener('click', toggleRandomCirclePin);
dampingInputEl.addEventListener('input', setDamping);
function addRandomCircle() {
packer.addCircle(createCircle());
}
// create circle dom object, return circle data
function createCircle(x, y, radius) {
radius = radius || random(10, 40);
x = x || random(radius, bounds.width - radius);
y = y || random(radius, bounds.height - radius);
const diameter = radius * 2;
const circleEl = document.createElement('div');
// need some sort of unique id...
const id = 'circle-' + random(0, 1000, true) + '-' + Date.now();
const circle = {
id: id,
radius: radius,
position: {
x: random(radius, bounds.width - radius),
y: random(radius, bounds.height - radius)
}
};
// create circle el
circleEl.id = id;
circleEl.style.width = diameter + 'px';
circleEl.style.height = diameter + 'px';
circleEl.style.borderRadius = diameter + 'px';
circleEl.classList.add('circle');
// store position for dragging
circleEl.setAttribute('data-x', x);
circleEl.setAttribute('data-y', y);
circleEl.setAttribute('data-radius', radius);
// start dragging
circleEl.addEventListener('mousedown', function (event) {
circlePressed(circleEl, circle, event);
});
containerEl.appendChild(circleEl);
circleEls[id] = circleEl;
return circle;
}
function createTargetEl() {
const targetEl = document.createElement('div');
targetEl.classList.add('target');
containerEl.appendChild(targetEl);
return targetEl;
}
function removeRandomCircle() {
const ids = Object.keys(circleEls);
const idToDelete = ids[random(0, ids.length, true)];
removeCircle(idToDelete);
}
function setRandomBounds() {
bounds = {
width: random(200, 500, true),
height: random(200, 500, true)
};
containerEl.style.width = bounds.width + 'px';
containerEl.style.height = bounds.height + 'px';
packer.setBounds(bounds);
// set attraction target to center of new bounds
target = { x: bounds.width / 2, y: bounds.height / 2 };
packer.setTarget(target);
packer.update();
}
function removeCircle(id) {
packer.removeCircle(id);
requestAnimationFrame(function () {
containerEl.removeChild(circleEls[id]);
delete circleEls[id];
});
}
function toggleRandomCirclePin() {
if (pinnedCircleId) {
packer.unpinCircle(pinnedCircleId);
circleEls[pinnedCircleId].classList.remove('is-pinned');
pinnedCircleId = null;
} else {
if (circles.length) {
const randomCircleIndex = Math.floor(Math.random() * circles.length);
const randomCircle = circles[randomCircleIndex];
pinnedCircleId = randomCircle.id;
circleEls[pinnedCircleId].classList.add('is-pinned');
packer.pinCircle(randomCircle);
}
}
}
function setRandomTarget() {
target = {
x: random(0, bounds.width, true),
y: random(0, bounds.height, true)
};
packer.setTarget(target);
packer.update();
}
function setDamping() {
const damping = parseFloat(dampingInputEl.value);
dampingValueEl.textContent = damping.toFixed(4);
packer.setDamping(damping);
}
function render(circles, target) {
requestAnimationFrame(function () {
for (let id in circles) {
const circleEl = circleEls[id];
if (circleEl) {
const circle = circles[id];
const x = circle.position.x - circle.radius;
const y = circle.position.y - circle.radius;
// store position for dragging
circleEl.setAttribute('data-x', x);
circleEl.setAttribute('data-y', y);
// actually move the circles around
circleEl.style.transform = `translateX(${x}px) translateY(${y}px)`;
circleEl.classList.add('is-visible');
}
}
targetEl.style.transform = `translateX(${target.x}px) translateY(${target.y}px)`
});
}
// start and stop dragging
function circlePressed(circleEl, circle, event) {
const circleStartPos = {
x: parseFloat(circleEl.getAttribute('data-x')) + circle.radius,
y: parseFloat(circleEl.getAttribute('data-y')) + circle.radius
};
const eventStartPos = { x: event.clientX, y: event.clientY };
function dragStart() {
document.addEventListener('mousemove', dragged);
document.addEventListener('mouseup', dragEnd);
}
function dragged(event) {
const currentPos = { x: event.clientX, y: event.clientY };
const delta = {
x: currentPos.x - eventStartPos.x,
y: currentPos.y - eventStartPos.y
};
// start dragging if mouse moved DRAG_THRESOLD px
if (!isDragging &&
(Math.abs(delta.x) > DRAG_THRESOLD || Math.abs(delta.y) > DRAG_THRESOLD)
) {
isDragging = true;
packer.dragStart(circle.id);
}
const newPos = { x: circleStartPos.x + delta.x, y: circleStartPos.y + delta.y };
if (isDragging) {
// end dragging if circle is outside the bounds
if (
newPos.x < circle.radius || newPos.x > bounds.width - circle.radius ||
newPos.y < circle.radius || newPos.y > bounds.height - circle.radius
) {
dragEnd();
} else {
packer.drag(circle.id, newPos);
}
}
}
function dragEnd() {
isDragging = false;
document.removeEventListener('mousemove', dragged);
packer.dragEnd(circle.id);
}
if (!isDragging) {
dragStart();
}
}
function random(min, max, intResult) {
if (typeof min !== 'number' && typeof max !== 'number') {
min = 0;
max = 1;
}
if (typeof max !== 'number') {
max = min;
min = 0;
}
let result = min + Math.random() * (max - min);
if (intResult) {
result = parseInt(result, 10);
}
return result;
}
</script>
</body>
</html>