-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
330 lines (259 loc) · 8.71 KB
/
index.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LabelLayer Demo</title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0 auto;
overflow: hidden;
}
#wrapper {
overflow: hidden;
position: relative;
margin: 0 auto;
width: 100%;
height: 100%;
/*width: 256px;*/
/*height: 256px;*/
/*border: 1px solid red;*/
user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-webkit-user-select: none;
overflow: hidden;
}
.label {
overflow: hidden;
/*box-shadow: 10px 10px 5px #888888;*/
/*width: 1px;*/
/*height: 1px;*/
color: white;
/*border: 1px solid lightgrey;*/
background-color: dimgrey;
border-radius: 10px;
padding: 10px;
margin: 10px;
}
#docs {
background-color: rgba(240, 240, 240, 0.9);
position: absolute;
height: 100%;
z-index: 10;
right: 0;
top: 0;
padding: 10px;
/*height: 100%;*/
max-width: 30%;
margin: 4px;
display: inline-block;
overflow: auto;
user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-webkit-user-select: none;
}
#template {
width: 90%;
height: 100px;
margin: 2px auto;
}
strong {
/*color: darkblue;*/
font-size: 2em;
}
#instructions {
background-color: rgba(240, 240, 240, 0.9);
position: absolute;
z-index: 10;
left: 0;
top: 0;
padding: 10px;
margin: 4px;
}
</style>
</head>
<body>
<div id="instructions">
<strong>1. Click and drag to spray labels on the screen.</strong>
<br/>
<strong>2. Scroll with mouse to zoom in and out.</strong>
</div>
<div id="docs">
<h1>LabelLayer</h1>
Fast on-the-fly HTML/CSS label placement with conflict detection. Labels will not overlap.
<br/>
Conflict detection is greedy, based on label priority.
<br/>
<h2>Why?</h2>
Style your labels using <em>everything</em> that HTML/CSS has on offer.
Vanilla HTML/CSS is significantly more expressive than most libraries that use svg, HTML5-canvas, or WebGL.
<h2>Demo</h2>
Change label template here, and spray the results on the screen below.
<textarea id="template"></textarea>
<h2>API</h2>
<pre style="font-family: monospace">
//---------------------------------
//BASICS
//create LabelLayer
const layer = new LabelLayer(document.getElementById('nodeToPlaceLabelsIn'));
//creates a label
//returns a handle that can then be used for other operations
const labelHandle = layer.createLabel(
x, //x in pixels
y, //y in pixels
contents, //string of valid HTML contents
priority //higher means priority in placement
);
//add a label
layer.addLabel(labelHandle);
//move label to new position
layer.moveLabel(labelHandle, newX, newY);
//remove label that was added earlier
layer.removeLabel(labelHandle);
//add label that was removed earlier
layer.addLabel(labelHandle);
//ensure the LabelLayer instance fits the surrounding div
//call this on a resize of the surrounding node.
layer.resize();
//get the visible layers
const visibleLayer = layer.getVisibleLabels();
//destroy the layer
layer.destroy();
//---------------------------------
//ADVANCED
//create LabelLayer, but do not automatically paint the result
//use this if you manage your own paintloop
const layer = new LabelLayer(document.getElementById('nodeToPlaceLabelsIn'), {
autoPaint: false
});
//paint the current state of the labels
layer.paint();
</pre>
<h2>Download</h2>
Download <a href="./dist/LabelLayer.js">LabelLayer.js</a>. Include this script to add the <code>LabelLayer</code> constructor to the global object.
Find source code <a href="https://github.com/thomasneirynck/LabelLayer">here</a>.
</div>
<div id="wrapper">
</div>
<script src="dist/LabelLayer.js"></script>
<!--<script src="./LabelLayer.js"></script>-->
<script>
///////////////////////////////////////////////////////////////////////////
const templateTag = document.getElementById('template');
templateTag.value =
`<div class="label">
<b>Priority</b>: {{priority}}
<hr>
<span style="float: right"> كارب ديم</span>
</div>`
const arcImage = document.createElement('canvas');
const radius = 10;
arcImage.width = radius * 2;
arcImage.height = radius * 2;
const arcContext = arcImage.getContext('2d');
arcContext.beginPath();
arcContext.arc(radius, radius, radius, 0, Math.PI * 2);
arcContext.fillStyle = 'rgba(175,105,105,0.1)';
arcContext.fill();
const handlez = [];
window.handlez = handlez;
const wrapper = document.getElementById('wrapper');
const canvas = document.createElement('canvas');
canvas.width = wrapper.clientWidth;
canvas.height = wrapper.clientHeight;
canvas.style.position = 'absolute';
canvas.style.top = 0;
canvas.style.left = 0;
wrapper.appendChild(canvas);
const context = canvas.getContext('2d');
const out = {x: 0, y: 0};
window.drawScene = function () {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = 'rgba(175,105,105,0.1)';
window.handlez.forEach(function (label) {
const x = label.getX();
const y = label.getY();
context.drawImage(arcImage, x - radius, y - radius);
});
context.strokeStyle = 'rgba(105,105,105,1)';
context.beginPath();
labelLayer.paint();
const vis = labelLayer.getVisibleLabels();
vis.forEach(function (label) {
const x = label.getX();
const y = label.getY();
labelLayer.getLabelCenter(label, out);
context.moveTo(out.x, out.y);
context.lineTo(x, y);
});
context.stroke();
};
const div = document.getElementById('wrapper');
const labelLayer = new LabelLayer(div, {
autoPaint: false
});
window.labelLayer = labelLayer;
let priority = 0;
let down = false;
wrapper.addEventListener('mousedown', function () {
down = true;
});
wrapper.addEventListener('mouseup', function () {
down = false;
});
wrapper.addEventListener('mousemove', function (event) {
if (down) {
let count = 0;
let bef = Date.now();
while (count < 4) {
const jitter = 64;
const x = event.clientX + (Math.random() * jitter) - jitter / 2;
const y = event.clientY + (Math.random() * jitter) - jitter / 2;
let contents = templateTag.value;
contents = contents.replace('{{priority}}', priority);
contents = contents.replace('{{now}}', Date.now());
const handle = labelLayer.createLabel(x, y, contents, -priority);
labelLayer.addLabel(handle);
priority++;
handlez.push(handle);
count++;
}
}
});
requestAnimationFrame(function draw() {
drawScene();
requestAnimationFrame(draw);
});
window.addEventListener('resize', function () {
canvas.width = wrapper.clientWidth;
canvas.height = wrapper.clientHeight;
});
window.addEventListener('DOMMouseScroll', onScroll);
window.addEventListener('mousewheel', onScroll);
function onScroll(event) {
event.preventDefault();
let cx = event.clientX;
let cy = event.clientY;
let count = 5;
let size = 1.01;
let zoomIn = true;
if (typeof event.wheelDelta === 'number') {
zoomIn = event.wheelDelta >= 0;
} else {
zoomIn = event.detail <= 0;
}
let thing = zoomIn ? size : 1 / size;
requestAnimationFrame(function zoom() {
labelLayer.scaleOnPoint(thing, thing, cx, cy);
if (count > 0) {
requestAnimationFrame(zoom);
}
count--;
});
}
</script>
</body>
</html>