-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
69 lines (66 loc) · 1.85 KB
/
render.js
File metadata and controls
69 lines (66 loc) · 1.85 KB
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
class GridBox {
static getTopleft(cellIndex) {
let boardHalfway = [0, 1].map(i => this.gridSize[i] / 2);
let indexOffset = [0, 1].map(i => cellIndex[i] - boardHalfway[i]);
let offset = [0, 1].map(i => indexOffset[i] * this.cellSize[i]);
let pos = [0, 1].map(i => this.center[i] + offset[i]);
return pos
}
//shrink half-margin for all sides
static getBox(topleft, margin) {
let [x, y] = topleft
return [
x + margin / 2,
y + margin / 2,
this.cellSize[0] - margin,
this.cellSize[1] - margin
]
}
}
self.GridBox = GridBox
class Tile extends GridBox {
static getBox(pos, margin) {
return super.getBox(this.getTopleft(pos), margin)
}
static getCenter(pos) {
let [x, y, w, h] = this.getBox(pos, 0)
return [x + w / 2, y + h / 2]
}
static draw(tiledata) {
let { pos: pos, infotext: infotext, tiletext: tiletext } = tiledata
let margin = 10;
// let [x, y] = this.getTopleft(pos)
let box = this.getBox(pos, margin)
let ctx = this.ctx
ctx.fillStyle = "lightgreen";
ctx.strokeStyle = "black";
ctx.strokeRect(...box);
ctx.font = "10px D2Coding";
ctx.textBaseline = "bottom";
ctx.textAlign = "left";
ctx.fillText(infotext, ...box.slice(0, 2));
ctx.font = "30px D2Coding";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(tiletext, ...this.getCenter(pos));
}
}
self.Tile = Tile
class Cursor extends GridBox {
static getBox(pos, margin) {
return super.getBox(this.getTopleft(pos), margin)
}
static draw(cursor) {
let pos = cursor.pos
let margin = 20;
// let [x, y] = this.getTopleft(pos)
let box = this.getBox(pos, margin)
let ctx = this.ctx
ctx.save()
ctx.lineWidth = 3
ctx.strokeStyle = cursor.strokeStyle ?? "white";
ctx.strokeRect(...box);
ctx.restore()
}
}
self.Cursor = Cursor