Skip to content

Commit bb7bbd3

Browse files
Altermoemikezhuang2022
authored andcommitted
fix: 代码优化。类型优化
1 parent cdc8673 commit bb7bbd3

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/pages/pageMap/utils/genshinMarker.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,36 @@ const angleCrds = (map: L.Map, prevLatlng: L.LatLng, latlng: L.LatLng) => {
6767
return Math.atan2(pxStart.y - pxEnd.y, pxStart.x - pxEnd.x) / Math.PI * 180 - 90
6868
}
6969

70+
const getMarkerShape = (layer: AnyObject & { options: GenshinLayerOptions }, img: ImgOptions) => {
71+
const { active, popperOpen } = img
72+
73+
// 根据状态调整视觉尺寸
74+
let [w, h] = img.size
75+
w = active ? w - 2 : popperOpen ? w + 4 : w
76+
h = active ? h - 2 : popperOpen ? h + 4 : h
77+
78+
// 基本参数计算
79+
const [halfW, halfH] = [w / 2, h / 2]
80+
const p = layer._point.round() as { x: number; y: number }
81+
let [x, y] = [p.x + img.offset.x, p.y + img.offset.y]
82+
;[x, y] = img.rotate ? [-halfW, -halfH] : [x - halfH, y - halfH]
83+
const center = [x + halfW, y + halfH] as [number, number]
84+
const radius = Math.max(w, h) / 2
85+
return { w, h, x, y, center, radius }
86+
}
87+
7088
/**
7189
* 点位图标绘制逻辑
7290
* @plugin fork from leaflet-canvas-markers
7391
*/
7492
L.Canvas.include({
75-
_compute(layer: AnyObject & { options: GenshinLayerOptions }, img: ImgOptions) {
76-
const { active, popperOpen } = img
77-
78-
// 根据状态调整视觉尺寸
79-
let [w, h] = img.size
80-
w = active ? w - 2 : popperOpen ? w + 4 : w
81-
h = active ? h - 2 : popperOpen ? h + 4 : h
82-
83-
// 基本参数计算
84-
const [halfW, halfH] = [w / 2, h / 2]
85-
const p = layer._point.round()
86-
let [x, y] = [p.x + img.offset.x, p.y + img.offset.y]
87-
;[x, y] = img.rotate ? [-halfW, -halfH] : [x - halfH, y - halfH]
88-
const center = [x + halfW, y + halfH] as const
89-
const radius = Math.max(w, h) / 2
90-
return { w, h, x, y, center, radius }
91-
},
9293
_updateImg(layer: AnyObject & { options: GenshinLayerOptions }) {
9394
const { img, undergroundImg } = layer.options
9495
const { width: rW, height: rH } = img.el
9596
const { width: urW, height: urH } = undergroundImg.el
9697
const { hover, active, popperOpen } = img
9798

98-
const { w, h, x, y, center, radius } = this._compute(layer, img)
99+
const { w, h, x, y, center, radius } = getMarkerShape(layer, img)
99100

100101
const ctx = this._ctx as CanvasRenderingContext2D
101102

@@ -143,20 +144,19 @@ L.Canvas.include({
143144
})
144145

145146
// 地下点位标识绘制
146-
if (img.isUnderground) {
147-
const { w: uw, h: uh, x: ux, y: uy } = this._compute(layer, undergroundImg)
148-
doDraw(ctx, () => {
149-
if (img.rotate) {
150-
ctx.translate(x, y)
151-
ctx.rotate(img.rotate * Math.PI / 180)
152-
}
153-
const { sx, sy, swidth, sheight, x: dx, y: dy, width, height } = getObjectFitSize('contain', uw, uh, urW, urH)
154-
ctx.drawImage(undergroundImg.el, sx, sy, swidth, sheight, dx + ux, dy + uy, width, height)
155-
})
156-
}
147+
img.isUnderground && doDraw(ctx, () => {
148+
const { w: uw, h: uh, x: ux, y: uy } = getMarkerShape(layer, undergroundImg)
149+
if (img.rotate) {
150+
ctx.translate(x, y)
151+
ctx.rotate(img.rotate * Math.PI / 180)
152+
}
153+
const { sx, sy, swidth, sheight, x: dx, y: dy, width, height } = getObjectFitSize('contain', uw, uh, urW, urH)
154+
ctx.drawImage(undergroundImg.el, sx, sy, swidth, sheight, dx + ux, dy + uy, width, height)
155+
})
157156
},
158157
})
159158

159+
// TODO ES5 类写法不便于调试,后续改成 ES6 class
160160
const CanvasMarker = L.CircleMarker.extend({
161161
_updatePath() {
162162
if (!this.options.img || !this.options.img.url)

0 commit comments

Comments
 (0)