Skip to content

Commit

Permalink
✨ feat: add a scale prop to the lotties
Browse files Browse the repository at this point in the history
  • Loading branch information
megasanjay committed Aug 25, 2023
1 parent b18005e commit 19764e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 6 additions & 6 deletions packages/playground/vite-project/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<template>
<div>
<Vue3Lottie :animationData="DogJSON" :width="300" :height="300" />
</div>
<Vue3Lottie :animationData="DogJSON" :width="300" :height="300" />

<Vue3Lottie
animationLink="https://assets1.lottiefiles.com/packages/lf20_soCRuE.json"
:height="300"
:width="300"
:pauseOnHover="true"
:scale="1.1"
/>

<Vue3Lottie
animationLink="https://assets1.lottiefiles.com/packages/lf20_xvz0dpbn.json"
assetsPath="https://source.unsplash.com/user/wsanter"
:height="300"
:width="300"
:height="200"
:width="200"
/>

<HelloWorld msg="Hello Lottie! Using Vue 3 + TypeScript + Vite" />
</template>

<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import DogJSON from './assets/dog.json'
</script>

Expand Down
1 change: 1 addition & 0 deletions packages/vue3-lottie/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface LottieProps {
pauseOnHover: boolean
playOnHover: boolean
noMargin: boolean
scale: number
backgroundColor: string
pauseAnimation: boolean
assetsPath: string
Expand Down
23 changes: 22 additions & 1 deletion packages/vue3-lottie/src/vue3-lottie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ export default defineComponent({
type: Boolean as PropType<LottieProps['noMargin']>,
default: false,
},
scale: {
type: Number as PropType<LottieProps['scale']>,
default: 1,
},
renderer: {
type: String as PropType<LottieProps['rendererSettings']>,
type: String as PropType<LottieProps['renderer']>,
default: 'svg',
},
rendererSettings: {
Expand Down Expand Up @@ -165,6 +169,18 @@ export default defineComponent({
lottieAnimationConfig.rendererSettings = props.rendererSettings
}
/**
* If the scale is not 1, we need to set `viewBoxOnly` to true
* This will remove the translate3d transform from the svg and
* will allow us to scale the svg using css transform scale
*/
if (props.scale !== 1) {
lottieAnimationConfig.rendererSettings = {
...lottieAnimationConfig.rendererSettings,
viewBoxOnly: true,
}
}
// actually load the animation
lottieAnimation = Lottie.loadAnimation(lottieAnimationConfig)
Expand Down Expand Up @@ -248,6 +264,7 @@ export default defineComponent({
'--lottie-animation-container-height': height,
'--lottie-animation-container-background-color': props.backgroundColor,
'--lottie-animation-margin': props.noMargin ? '0' : '0 auto',
'--lottie-animation-scale': props.scale != 1 ? props.scale : '',
}
return cssVariables
Expand Down Expand Up @@ -473,4 +490,8 @@ export default defineComponent({
overflow: hidden;
margin: var(--lottie-animation-margin);
}
.lottie-animation-container svg {
transform: scale(var(--lottie-animation-scale));
}
</style>

1 comment on commit 19764e7

@vercel
Copy link

@vercel vercel bot commented on 19764e7 Aug 25, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

vue3-lottie – ./

vue3-lottie.vercel.app
vue3-lottie-git-main-megasanjay.vercel.app
vue3-lottie-megasanjay.vercel.app

Please sign in to comment.