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

Commit

Permalink
Replace GSAP MorphSVG with flubber
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rich committed Jul 9, 2023
1 parent 4220128 commit e1c5772
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 9 deletions.
48 changes: 39 additions & 9 deletions components/d/character/Index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import gsap from 'gsap'
// @ts-ignore
import { interpolate } from 'flubber'
import idle from '../../../assets/img/character/idle.webp'
import idleColor from '../../../assets/img/character/idle-color.webp'
Expand Down Expand Up @@ -37,6 +38,8 @@ const props = defineProps({
}
})
const IS_LOW_PERFORMANCE = isLowPerformanceDevice()
const assets = {
idle,
idleColor,
Expand Down Expand Up @@ -87,15 +90,40 @@ watch(
action: shapeAction.value?.$el,
profi: shapeProfi.value?.$el
}
// TODO: Replace with flubber
if (false) {
gsap.fromTo(
shapeToAnimate.value?.$el,
{ morphSVG: poseShapeMap[oldPose] },
{ morphSVG: poseShapeMap[newPose], duration: 0.2 }
)
} else {
if (IS_LOW_PERFORMANCE) {
useHref.value = `#${newPose}-shape`
return
}
const interpolator = interpolate(
poseShapeMap[oldPose]?.getAttribute('d') ?? '',
poseShapeMap[newPose]?.getAttribute('d') ?? '',
{ maxSegmentLength: 100 }
)
console.log(interpolator)
const howFarAlongTheAnimationIsOnAScaleOfZeroToOne =
createTimeInterpolator(150)
requestAnimationFrame(draw)
function draw(time: number) {
const t = howFarAlongTheAnimationIsOnAScaleOfZeroToOne(time)
shapeToAnimate.value?.$el?.setAttribute('d', interpolator(t))
if (t < 1) {
requestAnimationFrame(draw)
}
}
function createTimeInterpolator(
duration: number,
easing: (t: number) => number = (t) => t
) {
const start = performance.now()
return (time: number) => {
const elapsed = time - start
const t = elapsed / duration
return t > 1 ? 1 : easing(t)
}
}
}
)
Expand All @@ -112,11 +140,13 @@ watch(
</defs>
<g v-if="!noShape">
<component
v-show="!IS_LOW_PERFORMANCE"
:is="initialSvgPath"
ref="shapeToAnimate"
:class="shapeClass"
/>
<use
v-show="IS_LOW_PERFORMANCE"
:class="shapeClass"
:href="useHref"
/>
Expand Down
101 changes: 101 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@nuxtjs/google-fonts": "^3.0.0-1",
"@nuxtjs/tailwindcss": "^6.6.0",
"@tailwindcss/typography": "^0.5.8",
"flubber": "^0.4.2",
"gsap": "^3.12.2",
"nuxt-icon": "^0.4.0"
}
Expand Down
4 changes: 4 additions & 0 deletions utils/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function isLowPerformanceDevice() {
if (typeof window === 'undefined') return false
return window.matchMedia('(prefers-reduced-motion: reduce)').matches
}

0 comments on commit e1c5772

Please sign in to comment.