Skip to content

Commit

Permalink
fix: 轨迹动画延迟执行状态更新不正确 #55
Browse files Browse the repository at this point in the history
  • Loading branch information
yue1123 committed Nov 10, 2023
1 parent 4d969be commit 2a3ab9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/hooks/useTrackAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { onUnmounted, ref, watch } from 'vue'
import { type Point } from '../utils'
import { type Point, proxyValue } from '../utils'

export type UseTrackAnimationOptions = {
/**
Expand Down Expand Up @@ -46,7 +46,8 @@ export function useTrackAnimation(map: any, options?: UseTrackAnimationOptions)
let mapComponentInstance: any
let mapInstance: BMapGL.Map
const status = ref<AnimationStatus>('INITIAL')
options = options || {}
const _options = options || {}
let prevStartTimeStamp: number | void = undefined
watch(
() => map.value,
(n) => {
Expand All @@ -56,49 +57,46 @@ export function useTrackAnimation(map: any, options?: UseTrackAnimationOptions)
const init = () => {
if (!instance) {
mapInstance = mapComponentInstance.getMapInstance()
instance = new BMapGLLib.TrackAnimation(mapInstance, pl!, options)
instance = new BMapGLLib.TrackAnimation(mapInstance, pl!, _options)
proxyValue(instance, '_status', instance._status, syncState)
}
}
const setPath = (path: Point[]) => {
const point = path.map((pathItem) => new BMapGL.Point(pathItem.lng, pathItem.lat))
pl = new BMapGL.Polyline(point)
init()
}

const start = () => {
if (instance && status.value === 'INITIAL') {
const now = performance.now()
const _prevStartTimeStamp = prevStartTimeStamp || 0
const _delay = _options.delay || 0
if (instance && now - _prevStartTimeStamp > _delay && status.value === 'INITIAL') {
instance.start()
syncState()
setTimeout(() => {
instance._viewAni.addEventListener('animationend', syncState)
})
}
prevStartTimeStamp = performance.now()
}

const cancel = () => {
if (instance) {
instance.cancel()
syncState()
}
}
const stop = () => {
if (instance) {
instance.pause()
syncState()
}
}
const proceed = () => {
if (instance) {
instance.continue()
syncState()
}
}

const syncState = () => {
setTimeout(() => {
if (instance) {
status.value = statusMap[instance._status]
}
})
if (instance) {
status.value = statusMap[instance._status]
}
}
onUnmounted(() => {
if (instance && status.value !== 'INITIAL') {
Expand Down
1 change: 1 addition & 0 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './pluginLoader'
export * from './types'
export * from './pathPointsToMapPoints'
export * from './nanoid'
export * from './proxyValue'
11 changes: 11 additions & 0 deletions packages/utils/proxyValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function proxyValue<T>(target: T, key: string, defaultValue: any, callback?: (defaultValue: any) => any) {
Object.defineProperty(target, '_status', {
get() {
return defaultValue
},
set(value) {
defaultValue = value
callback && callback(defaultValue)
}
})
}

1 comment on commit 2a3ab9a

@Hi-Alan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

神速

Please sign in to comment.