Skip to content

animation

动画属性。

注意事项

需安装 动画插件 才能使用。

关键属性

animation: IAnimation

动画 / 入场动画,详细了解 动画选项

ts
type IAnimation = IStyleAnimation | IKeyframesAnimation

// 关键帧动画
interface IKeyframesAnimation extends IAnimateOptions {
  keyframes: IKeyframe[] // 关键帧列表
}

// 样式过渡动画
interface IStyleAnimation extends IAnimateOptions {
  style: IUIInputData // 元素样式
}

// 关键帧
type IKeyframe = IUIInputData | IAnimateKeyframe

interface IAnimateKeyframe {
  style: IUIInputData // 元素样式

  easing?: IAnimateEasing // 单独设置关键帧缓动方式
  delay?: number // 单独设置关键帧延迟播放时长。
  duration?: number // 单独设置关键帧的固定时长,设置后将忽略 autoDuration

  // 分配剩余时间:(总时长 - 总关键帧固定时长)/ 总权重 * 当前权重
  autoDelay?: number // 自动 delay 的权重, 默认为 0
  autoDuration?: number // 自动 duration 的权重, 默认为 1
}

// 动画选项
interface IAnimateOptions {
  easing?: IAnimateEasing // 缓动方式,默认为 ease

  delay?: number // 延迟时间,以秒为单位, 默认为 0
  duration?: number // 动画时长,以秒为单位,默认为 0.2
  ending?: IAnimateEnding // 动画结束时的状态,可设置from、to,默认auto

  reverse?: boolean // 是否反向动画 to -> from,默认为 false
  swing?: boolean // 是否摇摆循环播放 from -> to,to -> from ... ,默认 false

  loop?: boolean | number // 是否循环播放,可设置循环次数,默认为 false
  loopDelay?: number // 进入下一次循环播放的延迟时间,默认为0

  speed?: number // 动画播放的倍速,值越大播放越快,默认为 1 倍速

  join?: boolean //  是否加入动画前的元素状态作为 from 关键帧
  autoplay?: boolean // 是否自动播放

  attrs?: string[] // 参与动画过渡效果的元素属性列表, 默认为所有
  event?: IAnimateEvents // 监听事件
}

animationOut: IAnimation

出场动画, 元素被移除 或 visible 为 0 时执行。

归属

UI

示例

入场和出场动画

可以用来直接开发页面过渡效果,以及元素的移入移出效果。

ts
import { Group, Leafer, Frame } from 'leafer-ui'
import '@leafer-in/animate'

const leafer = new Leafer({ view: window })

const page1 = new Frame({
    x: 300,
    y: 100,
    width: 150,
    height: 100,
    fill: '#FEB027',
    animation: { // 入场动画
        keyframes: [{ opacity: 0, offsetX: -150 }, { opacity: 1, offsetX: 0 }],
        duration: 0.8
    },
    animationOut: { // 出场动画
        style: { opacity: 0, offsetX: 150 },
        duration: 0.8
    }
})

const page2 = page1.clone({ fill: '#32cd79' }) // 克隆 page 并重新设置fill

const group = new Group({ children: [page1] })

leafer.add(group)

// 切换页面, 自动执行入场、出场动画
setInterval(() => {

    if (page1.parent) {
        group.add(page2)
        page1.remove()
    } else {
        group.add(page1)
        page2.remove()
    }

}, 2000)

摇摆循环动画

ts
import { Leafer, Rect } from 'leafer-ui'
import '@leafer-in/animate'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    y: 100,
    cornerRadius: 50,
    fill: '#32cd79',
    animation: {
        style: { x: 500, cornerRadius: 0 }, // style keyframe
        // options
        duration: 1,
        swing: true
    }
})

leafer.add(rect)

颜色过渡动画

ts
import { Leafer, Rect } from 'leafer-ui'
import '@leafer-in/animate'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    y: 100,
    cornerRadius: 50,
    fill: '#32cd79',
    animation: {
        style: { x: 500, cornerRadius: 0, fill: '#ffcd00' }, // style keyframe
        duration: 1,
        swing: true // 摇摆循环播放
    }
})

leafer.add(rect)

关键帧动画

ts
import { Leafer, Rect } from 'leafer-ui'
import '@leafer-in/animate'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 50,
    y: 100,
    cornerRadius: 50,
    fill: '#32cd79',
    around: 'center',
    animation: {
        keyframes: [
            { style: { x: 150, scaleX: 2, fill: '#ffcd00' }, duration: 0.5 },  // animate keyframe
            { style: { x: 50, scaleX: 1, fill: '#ffcd00' }, duration: 0.2 },
            { style: { x: 550, cornerRadius: 0, fill: '#ffcd00' }, delay: 0.1, easing: 'bounce-out' },
            { x: 50, rotation: -720, cornerRadius: 50 } // style keyframe
        ],
        duration: 3, // 自动分配剩余的时长给未设置 duration 的关键帧: (3 - 0.5 - 0.2 - 0.1) / 2 
        loop: true,
        join: true //  加入动画前的元素状态作为 from 关键帧
    }
})

leafer.add(rect)

Released under the MIT License.