-
Notifications
You must be signed in to change notification settings - Fork 4
/
hexmap.html
990 lines (858 loc) · 27.8 KB
/
hexmap.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
<!DOCTYPE html>
<html lang="en">
<head>
<!--<link rel="stylesheet" href="css/prettify.css">-->
<link rel="stylesheet" href="css/style.css">
<!--<script type="text/javascript" src="js/prettify.js"></script>
<script type="text/javascript" src="js/lang-css.js"></script>-->
<script type="text/javascript" src="js/highlight.pack.js"></script>
<link rel="stylesheet" href="css/highlight.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>stuQuery hexmap</title>
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@astronomyblog">
<meta name="twitter:url" property="og:url" content="https://slowe.github.io/stuquery/hexmap">
<meta name="twitter:title" property="og:title" content="stuQuery hexmap">
<meta name="twitter:description" property="og:description" content="A simple HTML-based hexmap library that makes use of stuQuery">
<meta name="twitter:image" property="og:image" content="https://slowe.github.io/stuquery/images/hexmap.png">
<meta name="keywords" content="html,javascript,hexmap,hex,hexjson">
<style>
section:hover { background-color: #efefef; }
section:hover pre { background-color: #f9f9fa; }
section pre { margin-bottom: 0px; }
pre { font-family: Courier; }
#main { top: 4em; }
h2 a:hover:before { content: "#"; left: -1em; position: absolute; color: #999; }
</style>
</head>
<body>
<a href="https://github.com/slowe/stuquery" id="forkme_banner">Fork Me on GitHub</a>
<header style="position:absolute;left:0px;top:0px;width:100%;height:8em;overflow:hidden;">
<div style="position:relative;width:105%;min-width:1024px;left:-1.75em;top:-1em;">
<div id="hexmap">
<code>
{
"layout":"odd-r",
"hexes": {
"0-0":{"q":0,"r":0},
"0-1":{"q":1,"r":0},
"0-2":{"q":2,"r":0},
"0-3":{"q":3,"r":0},
"0-4":{"q":4,"r":0},
"0-5":{"q":5,"r":0},
"0-6":{"q":6,"r":0},
"0-7":{"q":7,"r":0},
"0-8":{"q":8,"r":0},
"0-9":{"q":9,"r":0},
"0-10":{"q":10,"r":0},
"0-11":{"q":11,"r":0},
"0-12":{"q":12,"r":0},
"0-13":{"q":13,"r":0},
"0-14":{"q":14,"r":0},
"0-15":{"q":15,"r":0},
"0-16":{"q":16,"r":0},
"0-17":{"q":17,"r":0},
"0-18":{"q":18,"r":0},
"0-19":{"q":19,"r":0},
"0-20":{"q":20,"r":0},
"0-21":{"q":21,"r":0},
"0-22":{"q":22,"r":0},
"0-23":{"q":23,"r":0},
"0-24":{"q":24,"r":0},
"0-25":{"q":25,"r":0},
"0-26":{"q":26,"r":0},
"0-27":{"q":27,"r":0},
"0-28":{"q":28,"r":0},
"0-29":{"q":29,"r":0},
"0-30":{"q":30,"r":0},
"0-31":{"q":31,"r":0},
"0-32":{"q":32,"r":0},
"0-33":{"q":33,"r":0},
"0-34":{"q":34,"r":0},
"1-0":{"q":0,"r":1},
"1-1":{"q":1,"r":1},
"1-2":{"q":2,"r":1},
"1-3":{"q":3,"r":1},
"1-4":{"q":4,"r":1},
"1-5":{"q":5,"r":1},
"1-6":{"q":6,"r":1},
"1-7":{"q":7,"r":1},
"1-8":{"q":8,"r":1},
"1-9":{"q":9,"r":1},
"1-10":{"q":10,"r":1},
"1-11":{"q":11,"r":1},
"1-12":{"q":12,"r":1},
"1-13":{"q":13,"r":1},
"1-14":{"q":14,"r":1},
"1-15":{"q":15,"r":1},
"1-16":{"q":16,"r":1},
"1-17":{"q":17,"r":1},
"1-18":{"q":18,"r":1},
"1-19":{"q":19,"r":1},
"1-20":{"q":20,"r":1},
"1-21":{"q":21,"r":1},
"1-22":{"q":22,"r":1},
"1-23":{"q":23,"r":1},
"1-24":{"q":24,"r":1},
"1-25":{"q":25,"r":1},
"1-26":{"q":26,"r":1},
"1-27":{"q":27,"r":1},
"1-28":{"q":28,"r":1},
"1-29":{"q":29,"r":1},
"1-30":{"q":30,"r":1},
"1-31":{"q":31,"r":1},
"1-32":{"q":32,"r":1},
"1-33":{"q":33,"r":1},
"1-34":{"q":34,"r":1},
"2-0":{"q":0,"r":2},
"2-1":{"q":1,"r":2},
"2-2":{"q":2,"r":2},
"2-3":{"q":3,"r":2},
"2-4":{"q":4,"r":2},
"2-5":{"q":5,"r":2},
"2-6":{"q":6,"r":2},
"2-7":{"q":7,"r":2},
"2-8":{"q":8,"r":2},
"2-9":{"q":9,"r":2},
"2-10":{"q":10,"r":2},
"2-11":{"q":11,"r":2},
"2-12":{"q":12,"r":2},
"2-13":{"q":13,"r":2},
"2-14":{"q":14,"r":2},
"2-15":{"q":15,"r":2},
"2-16":{"q":16,"r":2},
"2-17":{"q":17,"r":2},
"2-18":{"q":18,"r":2},
"2-19":{"q":19,"r":2},
"2-20":{"q":20,"r":2},
"2-21":{"q":21,"r":2},
"2-22":{"q":22,"r":2},
"2-23":{"q":23,"r":2},
"2-24":{"q":24,"r":2},
"2-25":{"q":25,"r":2},
"2-26":{"q":26,"r":2},
"2-27":{"q":27,"r":2},
"2-28":{"q":28,"r":2},
"2-29":{"q":29,"r":2},
"2-30":{"q":30,"r":2},
"2-31":{"q":31,"r":2},
"2-32":{"q":32,"r":2},
"2-33":{"q":33,"r":2},
"2-34":{"q":34,"r":2} }
}
</code>
</div>
</div>
</header>
<div id="main">
<section id="downloads">
<a href="js/stuquery.hexmap.js">stuquery.hexmap.js (13kB)</a>
<a href="css/stuquery.hexmap.css">stuquery.hexmap.css (1kB)</a>
</section>
<h1>stuQuery hexmap</h1>
<p>This is a simple HTML-based hexmapping library that makes use of <a href="index.html">stuQuery</a>. Being created from simple HTML elements, it is easy to style the hexes with CSS. Each hex is accessible via the keyboard too.</p>
<p>As HTML is based on rectangles, we've had to create hexagons using CSS's <code>clip-path</code>. We've simulated a border on each hexagon by nesting two elements and setting the <code>border-color</code> by using <code>background-color</code> on the outer element and <code>border-width</code> by using <code>margin</code> on the inner element</code>.</p>
<p>Make sure to include the relevant CSS and Javascript files in your HTML page:</p>
<div class="example" data-title="false">
<script type="text/javascript" src="js/stuquery.js"></script>
<script type="text/javascript" src="js/stuquery.hexmap.js"></script>
<link rel="stylesheet" href="css/stuquery.hexmap.css">
</div>
<section id="basic">
<h2><a href="#basic">Basic hex map</a></h2>
<p>Let's create a simple hex map. We define the hex map by embedding some <a href="https://odileeds.org/projects/hexmaps/hexjson">HexJSON</a> (that way the raw data is visible if Javascript fails).</p>
<div class="example">
<div id="hexmap-1">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
// Create an object attached to a DOM element with the ID "hexmap-1"
var hexmap = S.hexmap('hexmap-1');
// Position the hexes and scale them to the container
hexmap.positionHexes().resize();
});
</script>
</div>
</section>
<section id="label">
<h2><a href="#label">Custom label on each hex</a></h2>
<p>Let's create a simple hex map but add a visible label to each hex.</p>
<div class="example">
<div id="hexmap-2">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-2',{
'formatLabel': function(id,hex){
// "id" = unique ID of the hex
// "hex" = object containing the properties of the specific hex
return hex.n;
}
});
hexmap.positionHexes().resize();
});
</script>
</div>
</section>
<section id="label-extra">
<h2><a href="#label-extra">Extra labels on select</a></h2>
<p>Now we'll add labels to each hex which have elements that are always visible and elements that only appear when the hex is selected or hovered.</p>
<div class="example">
<div id="hexmap-3">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-3',{
'formatLabel': function(id,hex){
// "id" = unique ID of the hex
// "hex" = object containing the properties of the specific hex
str = hex.n
str += '<div class="default">Default</div>'
str += '<div class="when-selected">Hover</div>';
return str;
}
});
hexmap.positionHexes().resize();
});
</script>
</div>
</section>
<section id="colours">
<h2><a href="#colours">Hex-specific colours</a></h2>
<p>Sometimes you might want to make your hex map into a heat map. To do that we send a callback function that returns an object of the form <code>{ 'background-color': '#ff0000', 'color': 'black' }</code></p>
<div class="example">
<div id="hexmap-4">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-4');
hexmap.positionHexes().resize();
hexmap.setColour(function(hex){
// The "hex" variable contains the properties of the specific hex
// The "this" variable references the hexmap object itself
var colours = ['#00B6FF','#EF3AAB','#F9BC26','#67E767','#08DEF9'];
// We will choose a colour which is the sum of the q and r coordinate
return {'background-color':colours[Math.abs(hex.q+hex.r)],'color':'black'};
});
});
</script>
</div>
</section>
<section id="hover">
<h2><a href="#hover">Hover styling</a></h2>
<p>As the hexes are created as DOM elements, we can style them with CSS</p>
<div class="example">
<!-- HTML -->
<div id="hexmap-5" class="ex5">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-5');
hexmap.positionHexes().resize();
});
</script>
<!-- CSS -->
<style>
/* Attach styles to hover and focus events */
.ex5 .hex:hover {
background-color: black;
}
.ex5 .hex:hover .hexinner,
.ex5 .hex:focus .hexinner {
/* We have created the hexagon via clip-path so we have to simulate
a border using two nested elements. We can set the border colour
using "background" and the border width using "margin" */
background: rgb(249, 188, 38);!important;
color: black;
margin: 10px;
cursor: pointer;
}
</style>
</div>
</section>
<section id="callbacks">
<h2><a href="#callbacks">Adding callbacks</a></h2>
<p>You may want to make your hexes interactive. You can attach callback functions to events such as <code>click</code>, <code>mouseover</code>, <code>mouseout</code>, and <code>focus</code>.</p>
<div class="example">
<!-- HTML -->
<div id="message-6">Click or hover on a hex</div>
<div id="hexmap-6" class="ex6">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-6');
hexmap.positionHexes().resize();
hexmap.on('click',function(e){
S('#message-6').html('You have clicked hex '+e.i+' ('+e.hex.id+')')
}).on('mouseover',function(e){
S('#message-6').html('You have hovered over hex '+e.i+' ('+e.hex.id+')')
}).on('mouseout',function(e){
S('#message-6').html('You have left hex '+e.i+' ('+e.hex.id+')')
}).on('focus',function(e){
S('#message-6').html('You have focussed on hex '+e.i+' ('+e.hex.id+')')
});
});
</script>
<!-- CSS -->
<style>
/* Attach styles to hover and focus events */
#message-6 {
position: relative;
display: inline-block;
left: 50%;
transform: translate3D(-50%,0,0);
background: black;
color: white;
padding: 0.25em 0.5em;
float: left;
z-index: 10;
}
.ex6 .hex {
cursor: pointer;
}
.ex6 .hex:hover .hexinner,
.ex6 .hex:focus .hexinner {
background: black!important;
color: white;
}
</style>
</div>
</section>
<section id="content">
<h2><a href="#content">Update the content</a></h2>
<p>You may want to update the content of your hexes.</p>
<div class="example">
<!-- HTML -->
<div id="hexmap-7">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"Hex a","q":0,"r":0},
"B":{"n":"Hex b","q":0,"r":1},
"C":{"n":"Hex c","q":1,"r":0},
"D":{"n":"Hex d","q":1,"r":1},
"E":{"n":"Hex e","q":2,"r":0},
"F":{"n":"Hex f","q":2,"r":1},
"G":{"n":"Hex g","q":0,"r":2},
"H":{"n":"Hex h","q":1,"r":2},
"I":{"n":"Hex i","q":2,"r":2}
}
}
</code>
</div>
<button id="button-7-id">Show IDs</button>
<button id="button-7-clear">Clear</button>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-7');
hexmap.positionHexes().resize();
// Add event to button click
S('#button-7-id').on('click',function(){
// When the button is pressed we update the content of all the hexes
hexmap.setContent(function(id,hex){
// "id" = unique ID of the hex
// "hex" = object containing the properties of the specific hex
return id;
})
});
// Add event to button click
S('#button-7-clear').on('click',function(){
// When the button is pressed we clear the content of all the hexes
hexmap.setContent(function(id,hex){ return ''; });
});
});
</script>
</div>
</section>
<section id="game">
<h2><a href="#game">Settling hex island</a></h2>
<p>Some hexes styled like the starting map of a famous board game.</p>
<div class="example">
<!-- HTML -->
<div id="hexmap-8" class="ex-8">
<code>
{
"layout":"odd-r",
"hexes": {
"A":{"n":"A","q":0,"r":0,"dice":5,"type":"mountains"},
"B":{"n":"B","q":1,"r":0,"dice":2,"type":"fields"},
"C":{"n":"C","q":2,"r":0,"dice":6,"type":"forest"},
"D":{"n":"D","q":2,"r":1,"dice":3,"type":"mountains"},
"E":{"n":"E","q":3,"r":2,"dice":8,"type":"fields"},
"F":{"n":"F","q":2,"r":3,"dice":10,"type":"pasture"},
"G":{"n":"G","q":2,"r":4,"dice":9,"type":"fields"},
"H":{"n":"H","q":1,"r":4,"dice":12,"type":"pasture"},
"I":{"n":"I","q":0,"r":4,"dice":11,"type":"forest"},
"J":{"n":"J","q":-1,"r":3,"dice":4,"type":"hills"},
"Robber":{"n":"Robber","q":-1,"r":2,"dice":7,"type":"desert"},
"K":{"n":"K","q":-1,"r":1,"dice":8,"type":"hills"},
"L":{"n":"L","q":0,"r":1,"dice":10,"type":"pasture"},
"M":{"n":"M","q":1,"r":1,"dice":9,"type":"pasture"},
"N":{"n":"N","q":2,"r":2,"dice":4,"type":"forest"},
"O":{"n":"O","q":1,"r":3,"dice":5,"type":"hills"},
"P":{"n":"P","q":0,"r":3,"dice":6,"type":"mountains"},
"Q":{"n":"Q","q":0,"r":2,"dice":3,"type":"forest"},
"R":{"n":"R","q":1,"r":2,"dice":11,"type":"fields"}
}
}
</code>
</div>
<div style="text-align:center;margin-top:1em;">
<button id="button-8-terrain">Randomise terrain</button>
</div>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-8');
hexmap.positionHexes().resize();
// Define the content of each hex
hexmap.setContent(function(id,hex){
// Define an abstract odds for outcomes 0-12 from rolling two dice
odds = [0,0,1,2,3,4,5,6,5,4,3,2,1];
ostr = '';
// Display circles to represent the odds
for(var i = 0; i < odds[hex.dice]; i++) ostr += '●'
// Build the circular token that sits on a hex
str = '<div class="token'+((hex.dice==8 || hex.dice == 6) ? ' token-red':'')+'">';
if(hex.n == "Robber"){
str += 'Robber '+hex.q+','+hex.r;
}else{
str += '<div class="id">'+' '+hex.q+','+hex.r+'</div>';
str += '<div class="dice">'+hex.dice+'</div>';
str += '<div class="odds">'+ostr+'</div>';
}
str += '</div>';
return str;
});
// Set the CSS class of each hex to be the hex type
hexmap.setClass(function(id,hex){
return hex.type;
});
// Create button which randomises the terrain
S('#button-8-terrain').on('click',function(){
// Define the terrain types
var terrains = ['hills','pasture','mountains','fields','forest'];
// Create an array of terrains to populate
var t = new Array(hexmap.hexes.length);
// Randomly assign terrains
for(var i = 0; i < hexmap.hexes.length; i++){
t[i] = terrains[Math.floor(Math.random()*terrains.length)];
}
// Update all the classes
for(var i = 0; i < hexmap.hexes.length; i++){
if(hexmap.hexes[i].n != "Robber") hexmap.hexes[i].setClass(t[i]);
}
});
});
</script>
<!-- CSS -->
<style>
.ex-8 {
background-color: #66bbe2;
padding: 6% 10%;
margin: auto;
clip-path: polygon(0% 50%, 25% 100%, 75% 100%, 100% 50%, 75% 0%, 25% 0%);
}
.token {
font-family: serif;
border-radius: 100%;
background-color: wheat;
width: 4em;
height: 4em;
color: black;
display: table-cell;
vertical-align: middle;
}
.token.token-red { color: crimson; }
.token .id, .token .odds { font-size: 0.5em; }
.token .dice { font-size: 2em; }
.hills { background-color: #c8906e; }
.pasture { background-color: #b5c881; }
.mountains { background-color: #778088; }
.fields { background-color: #e9b66c; }
.forest { background-color: #55855f; }
.desert { background-color: #f5dcad; }
</style>
</div>
</section>
<section id="london">
<h2><a href="#london">London 1895</a></h2>
<p>Re-creating "The Unification of London: The Need and the Remedy", John Leighton, page 14, 1895 <a href="https://www.flickr.com/photos/12403504@N02/11233339935/in/photolist-i7DNAK-i7DAdZ-i7B3We-i7yTt8-i7y4Ws-i7xDLm-i7woFp-i7wSYe-i7vCp9-i7uZcH-i7uATJ-i7tUTL-i7sB3W-i7rZ5o-i7rPcP-i7q4y9-i7psYp-i7pAVM-hY7HLd-hY6rx4-hY5Z53-hY3HsB-hY2nrs-hXUzY2-hXMtVh-hXLsns">released by the British Library to the Public Domain</a>.</p>
<div class="example">
<!-- HTML -->
<figure class="ex-9">
<div id="hexmap-9">
<div class="compass compass-n">N</div>
<div class="compass compass-e">E</div>
<div class="compass compass-w">W</div>
<div class="compass compass-s">S</div>
<code>
{
"layout":"odd-q",
"hexes": {
"A":{"n":"1a","q":0,"r":2,"label":"Horn<br />sey","type":"horizontal"},
"B":{"n":"2a","q":1,"r":2,"label":"Hack<br />ney","type":"horizontal"},
"C":{"n":"3a","q":2,"r":1,"label":"Old<br />Ford","type":"horizontal"},
"D":{"n":"4a","q":2,"r":0,"label":"Poplar","type":"horizontal"},
"E":{"n":"5a","q":2,"r":-1,"label":"Dept<br />ford","type":"horizontal"},
"F":{"n":"6a","q":1,"r":-1,"label":"Peck<br />ham","type":"horizontal"},
"G":{"n":"7a","q":0,"r":-2,"label":"Brix<br />ton","type":"horizontal"},
"H":{"n":"8a","q":-1,"r":-1,"label":"Batter<br />sea","type":"horizontal"},
"I":{"n":"9a","q":-2,"r":-1,"label":"Chel<br />sea","type":"horizontal"},
"J":{"n":"10a","q":-2,"r":0,"label":"Mary Le Bone","type":"horizontal"},
"K":{"n":"11a","q":-2,"r":1,"label":"S<sup>t</sup> John'<sup>s</sup> Wood","type":"horizontal"},
"L":{"n":"12a","q":-1,"r":2,"label":"Kentish Town","type":"horizontal"},
"M":{"n":"1","q":0,"r":1,"label":"Isling·<br />ton","type":"vertical"},
"N":{"n":"2","q":1,"r":1,"label":"Bethnal Green","type":"vertical"},
"O":{"n":"3","q":1,"r":0,"label":"South·<br />wark","type":"vertical"},
"P":{"n":"4","q":0,"r":-1,"label":"Kenning·<br />ton","type":"vertical"},
"Q":{"n":"5","q":-1,"r":0,"label":"Westmin-<br />ster","type":"vertical"},
"R":{"n":"6","q":-1,"r":1,"label":"St Pan·<br />cras","type":"vertical"},
"S":{"n":"","q":0,"r":0,"label":"The<br /><div>City</div>"}
}
}
</code>
</div>
<figcaption>London indexed by 2-mile hexagons</figcaption>
</figure>
<!-- Javascript -->
<script>
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-9');
hexmap.positionHexes().resize();
// Define the content of each hex
hexmap.setContent(function(id,hex){
str = '';
// Build the circular token that sits on a hex
str += '<div class="id">'+hex.n+'</div>';
str += '<div class="name">'+hex.label+'</div>';
return str;
})
// Set the CSS class of each hex to be the hex type
hexmap.setClass(function(id,hex){
return hex.type;
});
});
</script>
<!-- CSS -->
<style>
.ex-9 {
font-family: "Times New Roman", serif;
background-color: #fdf4d6;
padding: 2em;
position: relative;
color: #656058;
}
.compass {
position: absolute;
display: block;
padding: 0.06em 0.125em;
background-color: #fdf4d6;
font-size: 2em;
font-weight: 700;
font-family: "Copperplate Gothic Bold", serif;
}
.compass-n {
left: 50%;
top: 0;
transform: translate3D(-50%,-50%,0);
}
.compass-s {
left: 50%;
top: 100%;
transform: translate3D(-50%,-50%,0);
}
.compass-e {
left: 100%;
top: 50%;
transform: translate3D(-50%,-50%,0);
}
.compass-w {
left: 0%;
top: 50%;
transform: translate3D(-50%,-50%,0);
}
.ex-9 figcaption {
margin-top: 2em;
text-align: center;
}
#hexmap-9 {
position: relative;
padding: 6% 8.3333%;
margin: auto;
border-radius: 100%;
border: 2px solid rgba(0,0,0,0.5);
}
#hexmap-9 .hex {
background-color: #656058;
font-family: "Arial Black","Copperplate Gothic Bold","Times New Roman", serif;
}
#hexmap-9 .hex .hexinner {
background-color: #fdf4d6;
color: inherit;
margin: 2px;
}
#hexmap-9 .hex .id, #hexmap-9 .hex .name {
font-weight: 700;
line-height: 1em;
text-shadow: 3px 3px 2px #fdf4d6, -3px 3px 1px #fdf4d6, -3px -3px 1px #fdf4d6, 3px -3px 2px #fdf4d6;
}
#hexmap-9 .hex .id {
font-size: 2em;
font-family: "Times New Roman", serif;
}
#hexmap-9 .hex .name {
font-size: 1.3em;
text-transform: uppercase;
}
#hexmap-9 .hex .name > div {
font-size: 1.6em;
line-height: 1.25em;
}
#hexmap-9 .hex:hover .hexinner {
/* We have created the hexagon via clip-path so we have to simulate
a border using two nested elements. We can set the border colour
using "background" and the border width using "margin" */
background: #656058;
color: #fdf4d6;
}
#hexmap-9 .hex:hover .id, #hexmap-9 .hex:hover .name {
text-shadow: none;
}
.outer-london .name {
font-size: 1.4em;
font-family: "Times New Roman", serif;
}
.vertical {
background-color: #fdf4d6;
background-image: linear-gradient(90deg, transparent 50%, rgba(0,0,0,0.15) 50%);
background-size: 4% 4%;
}
.horizontal {
background-color: #fdf4d6;
background-image: linear-gradient(0deg, transparent 50%, rgba(0,0,0,0.15) 50%);
background-size: 4% 4%;
}
sup { top: -0.2em; }
</style>
</div>
</section>
<section id="html">
<h2><a href="#html">Hex map from HTML elements</a></h2>
<p>Sometimes you'd rather have a pre-generated hex map in the page as raw HTML rather than a JSON blob that gets converted. You can do that and attach a <code>hexmap</code> object to the element.</p>
<div class="example">
<!-- HTML -->
<div id="hexmap-10" class="hexmap pointy" data-layout="odd-r" style="width:336px;height:280px;transform:scale(1)">
<div class="hexmapinner" style="transform:scale(1.0000)">
<div class="hex" tabindex="0" data-id="A" data-r="0" data-q="0" style="left:0px;bottom:0px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="B" data-r="1" data-q="0" style="left:48px;bottom:84px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="C" data-r="0" data-q="1" style="left:96px;bottom:0px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="D" data-r="1" data-q="1" style="left:144px;bottom:84px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="E" data-r="0" data-q="2" style="left:192px;bottom:0px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="F" data-r="1" data-q="2" style="left:240px;bottom:84px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="G" data-r="2" data-q="0" style="left:0px;bottom:168px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="H" data-r="2" data-q="1" style="left:96px;bottom:168px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
<div class="hex" tabindex="0" data-id="I" data-r="2" data-q="2" style="left:192px;bottom:168px;width:96px;height:112px">
<div class="hexinner">
<div class="hexcontent"></div>
</div>
</div>
</div>
</div>
<!-- Javascript -->
<script>
// Note that we only need to call any Javascript if we want to do
// anything interactive with the HexMap. If not, we can skip it.
S(document).ready(function(){
var hexmap = S.hexmap('hexmap-10');
// No need to call hexmap.positionHexes() as we've already set the positions
hexmap.setClass(function(id,hex){
return (id=="D" ? "forest":"");
});
});
</script>
<!-- CSS -->
<style>
</style>
</div>
</section>
</div>
<script>
ex = S('.example');
function tidy(t){ return t.replace(/===NEWLINE===/g,"\n").replace(/\n*$/,"").replace(/^\n*/,""); }
function sanitise(t){ return t.replace(/\</g,"<").replace(/\>/g,">"); }
for(var i = 0; i < ex.length; i++){
html = ex[i].innerHTML;
css = "";
js = "";
temp = html.replace(/\n/g,"===NEWLINE===").replace(/<!-- HTML -->/i,"");
temp = temp.replace(/<!-- Javascript -->/i,"").replace(/<script>(.*)<\/script>/,function(m,p1){
js = tidy(p1);
return "";
});
temp = temp.replace(/<!-- CSS -->/i,"").replace(/<style>(.*)<\/style>/,function(m,p1){
css = tidy(p1);
return "";
});
code = sanitise(tidy(temp));
code = code.replace(/(src|href)=\"([^\"]+)\"/g,function(m,p1,p2){ return p1+'="<a href="'+p2+'">'+p2+'</a>"'; })
showtitle = true;
if(S(ex[i]).attr('data-title')=="false") showtitle = false;
ex[i].innerHTML = ''+html+(showtitle ? '<h3>How to do it</h3><h4>HTML</h4>':'')+'<pre class="prettyprint lang-html">'+code+'</pre>'+(css ? (showtitle ? '<h4>CSS</h4>':'')+'<pre class="prettyprint lang-css">'+sanitise(css)+'</pre>':'')+(js ? (showtitle ? '<h4>Javascript</h4>':'')+'<pre class="prettyprint lang-js">'+sanitise(js)+'</pre>':'');
}
var code = S('.prettyprint');
for(var i = 0; i < code.length; i++){
hljs.highlightBlock(code[i]);
}
var hexmap = S.hexmap();
S('h1').append(' v'+hexmap.version);
S(document).ready(function(){
// Create an object attached to a DOM element with the ID "hexmap-1"
var hexmap = S.hexmap('hexmap',{'width':3});
// Position the hexes and scale them to the container
hexmap.positionHexes().resize();
var colours = ['#00B6FF','#EF3AAB','#F9BC26','#67E767','#08DEF9'];
hexmap.setColour(function(hex){
// We will choose a colour which is the sum of the q and r coordinate
return {'background-color':colours[Math.floor(Math.random()*colours.length)]};
});
});
</script>
</body>
</html>