-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·75 lines (57 loc) · 1.88 KB
/
index.js
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
import HexGrid from "./hexgrid.js";
import { createMap } from "./mapgen.js";
$(function () {
const MAP_ROWS = 20
const MAP_COLUMNS = 20;
const RADIUS = 20;
let backgroundCanvas;
let foregroundCanvas;
// preloadImages(["a.jpg"]);
initCanvas();
bindListeners();
useCanvas();
function initCanvas() {
backgroundCanvas = new HexGrid("#canvas1", { rows: MAP_ROWS, columns: MAP_COLUMNS, radius: RADIUS, fitToGrid: true, startCenterX: false, startCenterY: false });
foregroundCanvas = new HexGrid("#canvas2", { rows: MAP_ROWS, columns: MAP_COLUMNS, radius: RADIUS, fitToGrid: true ,startCenterX:false,startCenterY:false});
}
function bindListeners() {
$('#canvas2').on("click", (e) => {
const clicked = foregroundCanvas.getClickedTile(e)
console.log(clicked)
foregroundCanvas.drawHexes([{ x: clicked.x, y: clicked.y, fill: "green", line: "black" }]);
});
}
function useCanvas() {
// get basic layout from the hexgrid
// const tiles = backgroundCanvas.createGrid("green", "lightgreen","tree");
// get randomized color map
const tiles = createMap(3,7,MAP_ROWS,MAP_COLUMNS);
// images
backgroundCanvas.drawImages(tiles)
//colors
// backgroundCanvas.drawHexes(tiles);
// paint a specific tile
// foregroundCanvas.drawHexes([{ x: 0, y: 2, fill: "green", line: "black" }]);
}
});
/*
function preloadImages(array) {
if (!preloadImages.list) {
preloadImages.list = [];
}
var list = preloadImages.list;
for (var i = 0; i < array.length; i++) {
var img = new Image();
img.onload = function() {
var index = list.indexOf(this);
if (index !== -1) {
// remove image from the array once it's loaded
// for memory consumption reasons
list.splice(index, 1);
}
}
list.push(img);
img.src = array[i];
}
}
*/