-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlabellayout.html
249 lines (226 loc) · 7.68 KB
/
labellayout.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
<html>
<head>
<title>Map Label Overlap Reduction</title>
<script type="text/javascript" src="min/jquery.min.js"></script>
<link href="wikiminiatlas.css" rel="stylesheet" type="text/css" />
<style>
#canvas {
border: 1px solid black;
width: 512px;
height: 512px;
position: relative;
}
div.tile {
width: 128px;
height: 128px;
position: absolute;
}
span.label {
/*border: 1px solid red;*/
background-color: rgba(100%,0%,0%,0.1);
position: absolute;
font-size:7pt;
-webkit-transition: left 0.5s ease-in-out, top 0.5s ease-in-out;
-moz-transition: left 0.5s ease-in-out, top 0.5s ease-in-out;
line-height: 100%;
}
</style>
</head>
<body>
<h1>Reducing map label overlap</h1>
<div id="canvas"></div>
<button id="opt">Optimize Labels</button>
<button id="pop">New Labels</button>
<p>Testing a simple heuristics to layout the WikiMiniAtlas map labels client-side. Press the 'Optimize Labels' button (try more than once). The code also tries to compatify the labels into a rectangular bounding box with minimal area and pleasant aspect ratio.</p>
<script>
var x,y,n,i,s,t=[],l=[],opt=[]
, llist = [
'Town', 'New York', 'Wreck of the Hindenburg', 'Central park', 'A small thing',
'Riverside Park and drive', 'American Museum of Natural History','Ford Motor Company Edgewater Assembly Plant',
'Studio 54','Church of St. Vincent Ferrer','CBS Broadcast Center','USS Intrepid (CV-11)','Burr-Hamilton duel',
'Nungessers','New York Tunnel Extension','Weehawken','Roosevelt Island'
]
, hslist = [ [0,0], [1,0], [1,1], [0,1] ] // potential label hotspot locations
, sprite = [
{ r: 6 }, // red dot
{ r: 6 }, // blue dot
{ r: 2 }, // UL
{ r: 2 }, // UR
{ r: 2 }, // DR
{ r: 2 }, // DL
{ r: 2 }, // city 3
{ r: 3 }, // city 5
{ r: 4 }, // city 7
{ r: 5 }, // city 9
{ r: 6 }, // city 11
{ r: 5 }, // summit
{ r: 6 } // event
]
;
// position label
function positionLabel(l,hs) {
hs = hs || 0;
l.sprite = (l.style==0)?(hs+2):(l.style+5);
l.x = l.tx-hslist[hs][0]*l.w - (hslist[hs][0]-0.5)*2*sprite[l.sprite].r;
l.y = l.ty-hslist[hs][1]*l.h - (hslist[hs][1]-0.5)*2*sprite[l.sprite].r;
}
function updateSprite(l) {
l.sym.css('background-position', (-12*l.sprite) + 'px' );
}
function insertLabel( l, t ) {
var A = null, wopt, wopt2, w, h, lh;
// insert symbol
l.sym = $('<div class="sprite"></div>')
.css( {
left: (l.tx-5) + 'px',
top: (l.ty-5) + 'px'
} )
.appendTo(t);
// insert label text
l.span = $('<span class="label"></span>').text(l.name).appendTo(t);
// obtain max width
l.w = l.span.width();
// find best label shape
w = wopt = l.w;
lh = l.span.height();
for( ; w>l.w/4; w-=4 ) {
l.span.css( { width: w+'px' } );
// did the height change with this resize? (word wrap occured)
h = l.span.height();
if( h != lh ) {
wopt = wopt2;
lh = h;
}
// calculate new weighted area
h = Math.pow( h, 2 );
if( A === null || w*w*h < A ) {
wopt2 = w;
A = w*w*h;
}
}
l.w = wopt;
l.span.css( { width: l.w+'px' } );
l.h = l.span.height();
positionLabel(l);
l.span.css( { left: l.x, top: l.y } );
updateSprite(l);
}
function overlap(l1,l2,n,m) {
var n=n||0,m=m||0,
l2x=l2.x+128*n,
l2y=l2.y+128*m;
if( l1.x < (l2x+l2.w) && l2x < (l1.x+l1.w) &&
l1.y < (l2y+l2.h) && l2y < (l1.y+l1.h) ) {
return ( Math.min(l1.x+l1.w,l2x+l2.w) - Math.max(l1.x,l2x) ) *
( Math.min(l1.y+l1.h,l2y+l2.h) - Math.max(l1.y,l2y) );
} else {
return 0.0;
}
}
function optimizeLabels( x,y,debug ) {
var o,omin = null,nc,c,cmin,tc,i,j,n,m,ll=l[x][y],l2,hsl=hslist.length, nn=[];
if( ll.length > 4 ) return;
// compile list of initialized neighbors
for( n = Math.max(x-1,0); n<=Math.min(x+1,3); n++ ) {
for( m = Math.max(y-1,0); m<=Math.min(y+1,3); m++ ) {
if( opt[n][m] && ( n!=x || m!=y ) ) {
nn.push( { l: l[n][m], dx: n-x, dy: m-y });
}
}
}
// loop over all combinations
nc = Math.pow( hsl, ll.length );
for( c = 0; c < nc; ++c ) {
// apply configuration
tc = c;
for( i=0; i< ll.length; ++i ) {
positionLabel(ll[i],tc%hsl);
tc = Math.floor(tc/hsl);
}
// check within tile
o = 0;
for( i=0; i< ll.length; ++i ) {
for( j=i+1; j< ll.length; ++j ) {
o += overlap(ll[i],ll[j]);
}
}
// check initialized neighbors
for( n = 0; n < nn.length; ++n ) {
l2=nn[n].l;
for( i=0; i< ll.length; ++i ) {
for( j=0; j< l2.length; ++j ) {
o += overlap(ll[i],l2[j], nn[n].dx, nn[n].dy );
}
}
}
// overlap minimum?
if( omin === null || o < omin ) {
omin = o;
cmin = c;
}
if(debug) { console.log(c+' -> '+o); }
}
// apply minimum
tc = cmin;
for( i=0; i< ll.length; ++i ) {
positionLabel(ll[i],tc%hsl);
tc = Math.floor(tc/hsl);
ll[i].span.css( { left: ll[i].x, top: ll[i].y } );
updateSprite(ll[i]);
}
if(debug) { console.log("min: "+cmin+' -> '+omin); }
opt[x][y] = true;
}
function populate() {
var x2,y2;
$('#canvas').html('');
for( x=0; x<4; x++ ) {
t[x]=[];
l[x]=[];
opt[x]=[];
for( y=0; y<4; y++ ) {
opt[x][y]=false;
l[x][y]=[];
t[x][y]=$('<div class="tile"></div>').css( { top: y*128, left: x*128 } );
$('#canvas').append(t[x][y]);
n = 0;
for( x2=0; x2<2; x2++ ) {
for( y2=0; y2<2; y2++ ) {
l[x][y][n] = {
w: 0, h: 0, hs: 0, x:0, y:0,
tx: Math.floor(Math.random()*64)+64*x2,
ty: Math.floor(Math.random()*64)+64*y2,
name: llist[Math.floor(Math.random()*llist.length)],
style: 0,//Math.floor(Math.random()*8),
span: null, sym: null, sprite: null
};
insertLabel( l[x][y][n], t[x][y] );
positionLabel( l[x][y][n] );
//t[x][y].append($('<div class="dot"></div>').css( { left: l[x][y][n].x-2, top: l[x][y][n].y-2 }));
n++;
}
}
}
}
}
$(function(){
populate();
$('#pop').click(populate);
$('#opt').click(function(){
var i = [],x,y,n;
n = 0;
for( x=0; x<4; x++ ) {
for( y=0; y<4; y++ ) {
i[n++] = { r: Math.random(), x:x, y:y };
}
}
i.sort(function(a,b){return a.r-b.r});
while( n>0 ) {
n--;
optimizeLabels(i[n].x,i[n].y);
}
});
});
</script>
</body>
</html>