Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Optimize interpolators
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rich committed Jul 9, 2023
1 parent e1c5772 commit edc0151
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 8 additions & 7 deletions components/d/character/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ watch(
useHref.value = `#${newPose}-shape`
return
}
const interpolator = interpolate(
poseShapeMap[oldPose]?.getAttribute('d') ?? '',
poseShapeMap[newPose]?.getAttribute('d') ?? '',
{ maxSegmentLength: 100 }
)
console.log(interpolator)
const interpolator = useCache(`character:morph:${oldPose}-${newPose}`, () => {
return interpolate(
poseShapeMap[oldPose]?.getAttribute('d') ?? '',
poseShapeMap[newPose]?.getAttribute('d') ?? '',
{ maxSegmentLength: 30 }
)
})
const howFarAlongTheAnimationIsOnAScaleOfZeroToOne =
createTimeInterpolator(150)
createTimeInterpolator(200)
requestAnimationFrame(draw)
Expand Down
10 changes: 10 additions & 0 deletions composables/calculations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const cache = new Map<string, any>()

export function useCache<T>(key: string, fn: () => T): T {
if (cache.has(key)) {
return cache.get(key)
}
const value = fn()
cache.set(key, value)
return value
}

0 comments on commit edc0151

Please sign in to comment.