/
surface-minimap.ts
148 lines (121 loc) · 4.17 KB
/
surface-minimap.ts
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
import $ from 'sigl'
import { filterMap } from 'everyday-utils'
import { Point, Rect } from 'sigl'
import { SurfaceElement } from './surface'
const style = /*css*/ `
[part=canvas] {
contain: size layout style paint;
position: fixed;
opacity: 0.6;
width: 150px;
height: 112.5px;
right: 30px;
bottom: 30px;
z-index: 2;
border-radius: 3px;
/* this is cool but glitches on mobile */
/* ${$.isMobile ? '' : 'transition: opacity 0.08s ease-out;'} */
}
[part=canvas].hide {
opacity: 0;
pointer-events: none;
}
[part=canvas]:hover {
opacity: 0.9;
}`
export interface SurfaceMinimapElement extends $.Element<SurfaceMinimapElement> {}
@$.element()
export class SurfaceMinimapElement extends HTMLElement {
root = $(this).shadow(/*html*/ `<style>${style}</style><canvas part="canvas" class="hide"></canvas>`)
@$.attr() pixelRatio = window.devicePixelRatio
@$.attr() scale = 0.2
@$.attr() ratio = 4 / 3
// from surface
matrix?: DOMMatrix
rect?: Rect
items?: SurfaceElement['items']
canvas = $(this).query<HTMLCanvasElement>(/*css*/ `[part=canvas]`)
ctx = $(this).reduce(({ canvas }) => canvas.getContext('2d', { alpha: false, desynchronized: true })!) //?: CanvasRenderingContext2D
isOverMinimap = $(this).fulfill(({ canvas }) => (fulfill =>
$.chain(
$.on(canvas).pointermove(() => fulfill(true)),
$.on(canvas).pointerleave(() => fulfill(false))
)), false)
allVisible = true
mounted($: SurfaceMinimapElement['$']) {
$.effect(({ rect, canvas, scale, ratio, pixelRatio }) => {
const [w, h] = rect.size.scaleSelf(pixelRatio)
const g = Math.max(850, Math.min(1150 + (w * 0.1), w * 0.15 + h * 0.85))
const size = new Point(
g * scale * ratio,
g * scale
)
if (!size.round().equals(Point.fromObject(canvas))) {
Object.assign(canvas, size.round().toSizeObject())
Object.assign(canvas.style, size.normalize(pixelRatio).toStyleSize())
}
})
$.effect.raf(({ ctx: ctx, canvas: { width: w, height: h }, matrix, items: surfaceItems }) =>
$.use(({ isOverMinimap, rect }) => {
if (!rect) return
const frame = rect.clone()
const items = filterMap(surfaceItems, el => !el.points && el.rect.transform(matrix).translateSelf(frame))
const all = [frame, ...items]
const screen = Rect.combine(all)
all.forEach(b => b.normalizeSelf(screen).multiplySelf(w, h))
if (!isOverMinimap) {
$.allVisible = items.every(b => b.withinRect(frame))
if ($.allVisible) return
}
ctx.clearRect(0, 0, w, h)
ctx.fillStyle = '#282828'
ctx.fillRect(0, 0, w, h)
const lw = ctx.lineWidth
const lw2 = lw * 2
// draw frame
ctx.fillStyle = 'rgba(255,255,255,.045)'
ctx.strokeStyle = 'rgba(255,255,255,.5)'
ctx.beginPath()
// @ts-ignore
ctx.roundRect(frame.x + lw, frame.y + lw, frame.width - lw2, frame.height - lw2, [3])
ctx.fill()
ctx.stroke()
// draw items
ctx.fillStyle = 'rgba(255,255,255,.16)'
ctx.strokeStyle = '#aaa'
for (const item of items) {
const { x, y, width, height } = item
ctx.beginPath()
// @ts-ignore
ctx.roundRect(x + lw, y + lw, width - lw2, height - lw2, [0.75])
ctx.fill()
ctx.stroke()
}
// draw points/paths
const paths = surfaceItems
.filter(x => x.points)
.map(p =>
p.points!.map(
x =>
x
.transform(matrix)
.normalizeSelf(screen.size)
.multiplySelf(w, h)
.translateSelf(frame)
)
) as Point[][]
ctx.strokeStyle = '#666'
ctx.lineWidth = 1.75
for (const points of paths) {
ctx.beginPath()
for (const p of points) ctx.lineTo(p.x, p.y)
ctx.stroke()
}
})
)
$.effect.raf(({ allVisible, canvas }) => {
if (allVisible && !canvas.classList.contains('hide')) canvas.classList.add('hide')
if (!allVisible && canvas.classList.contains('hide')) canvas.classList.remove('hide')
})
}
}