in packages/layers/label/src/LabelLayer.ts [177:277]
private _init(projection, timeline, polaris) {
const p = polaris as PolarisGSI
let viewChangedFrames = 0
let visNeedsCheck = true
let lastZoom = -1
// Canvas init
this.listenProps(['zIndex'], () => {
this._createCanvas(p)
})
this.listenProps(
[
'data',
'baseAlt',
'inverseByBgColor',
'getBgColor',
'text_family',
'text_weight',
'text_size',
'text_color',
'text_background',
'text_marginLeft',
'text_marginTop',
'text_translate_x',
'text_translate_y',
'text_shadow_px',
'text_shadow_color',
'customTextStyle',
],
() => {
this._createLocators()
viewChangedFrames = 0
visNeedsCheck = true
this._currInvisibles = []
this._currVisibles = []
}
)
this.listenProps(['markerComparator', 'markerCompareEnlargePx'], () => {
const comparator = this.getProps('markerComparator')
if (!comparator) return
this.markers.sort((a, b) => {
return comparator(a ? a.feature : undefined, b ? b.feature : undefined)
})
viewChangedFrames = 0
visNeedsCheck = true
this._currInvisibles = []
this._currVisibles = []
})
this.onViewChange = (cam, p) => {
const polaris = p as PolarisGSI
// viewChange固定几帧之后再进行label抽样测试
viewChangedFrames = 0
// 只有zoom改变时才测试
if (cam.zoom !== lastZoom) {
visNeedsCheck = true
}
// update width/height
if (this.canvas) {
if (
this.canvas.clientWidth !== polaris.width ||
this.canvas.clientHeight !== polaris.height
) {
this.canvas.style.width = polaris.width + 'px'
this.canvas.style.height = polaris.height + 'px'
this.canvas.width = polaris.width * this.ratio
this.canvas.height = polaris.height * this.ratio
}
}
}
const framesBeforeStable = 3
this.onBeforeRender = () => {
const cam = p.cameraProxy
if (viewChangedFrames < framesBeforeStable) {
this._calcMarkersBBox()
}
// only when static frames > N && view changed, do marker visibility tests
// const frames = Math.round(1000 / this.getProps('showHideDuration'))
if (++viewChangedFrames > framesBeforeStable && visNeedsCheck) {
this._setMarkersVisibility(timeline)
visNeedsCheck = false
if (cam.zoom !== lastZoom) {
lastZoom = cam.zoom
}
}
}
// Draw labels
this.onAfterRender = () => {
this._clearCanvas()
this._drawLabels()
}
}