Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API animation


May 19, 2021 WeChat Mini Program Development Document


Table of contents


wx.createAnimation(OBJECT)


Create an animation instance animation. T he method that calls the instance describes the animation. Finally, the animation data is export animation property through the export method animation animation instance.

Note: export animation operation is cleared after each call by the export method

OBJECT parameter description:

Parameters Type Required The default Description
duration Integer Whether 400 Animation duration, in ms
timingFunction String Whether "linear" Defines the effect of the animation
delay Integer Whether 0 Animation delay time, in ms
transformOrigin String Whether "50% 50% 0" Set transform-origin

TimingFunction Valid Value:

Value Description
linear The speed of the animation is the same from start to finish
ease The animation starts at a low speed, then accelerates, and slows down before it ends
ease-in The animation starts at a low speed
ease-in-out The animation starts and ends at a low speed
ease-out The animation ends at a low speed
step-start The first frame of the animation jumps to the end state until the end
step-end The animation remains at the beginning and the last frame jumps to the end state

var animation = wx.createAnimation({
  transformOrigin:"50% 50%",
  duration:1000,
  timingFunction:"ease",
  delay:0
})

animation


Animation instances can call the following methods to describe the animation, return themselves after the call ends, and support the writing of chain calls.

Method list of animation objects:

Style:

Method Parameters Description
opacity value Transparency, parameters range from 0 to 1
backgroundColor color The color value
width length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in
height length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in
top length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in
left length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in
bottom length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in
right length The length value, which defaults to px if number is passed in, and the length value of other custom units can be passed in

Rotating:

Method Parameters Description
rotate deg Deg range -180 to 180, rotate a deg angle clockwise from the origin
rotateX deg Deg ranges from 180 to 180, rotating a deg angle on the X-axis
rotateY deg Deg ranges from -180 to 180, rotating a deg angle on the Y axis
rotateZ deg Deg ranges from 180 to 180, rotating a deg angle on the Z axis
rotate3d (x,y,z,deg) With transform-function rotate3d

Scaling:

Method Parameters Description
scale sx,[sy] At one parameter, it means that the sx multiple is scaled at the same time on the X-axis and the Y-axis, and the two parameters are indicated that the sx multiple is scaled on the X-axis and the sy multiple is scaled on the Y-axis
scaleX Sx Scale the sx multiply on the X-axis
scaleY Sy Scale the sy multiply on the Y axis
scaleZ sz Scale the sy multiply on the Z-axis
scale3d (sx,sy,sz) Scale the sx multiply on the X-axis, the sy-factor on the Y-axis, and the sz-factor on the Z-axis

Offset:

Method Parameters Description
translate tx,[ty] One parameter represents the X-axis offset tx, unit px, and the two parameters represent the X-axis offset tx, and the Y-axis offset ty, unit px.
translateX tx Offset tx in X-axis, unit px
translateY ty Offset tx in Y-axis, unit px
translateZ tz Offset tx in Z-axis, unit px
translate3d (tx,ty,tz) Offset tx on the X-axis, offset ty on the Y-axis, offset tz on the Z-axis, unit px

Tilt:

Method Parameters Description
skew ax,[ay] Parameter range -180 to 180;
skewX Ax Parameter range -180 to 180; Y-axis coordinates remain unchanged, X-axis coordinates extend clockwise tilt ax degree
skewY ay Parameter range -180 to 180; X-axis coordinates remain unchanged, and Y-axis coordinates are tilted clockwise

Matrix deformation:

Method Parameters Description
matrix (a,b,c,d,tx,ty) With transform-function matrix
matrix3d With transform-function matrix3d

Animated queue


After calling the animation operation step() that a set of animations is complete, you can call any number of animation methods in a set of animations, all animations in a set of animations will start at the same time, and the next set of animations will not be performed until a set of animations is complete. Step can pass in a configuration parameter that is the same as wx.createAnimation() to specify the configuration of the current group animation.

Example:

<view animation="{{animationData}}" style="background:red,height:100rpx,width:100rpx"></view>
Page({
  data:{
    animationData:{}
  },
  onShow:function(){
    var animation = wx.createAnimation({
      duration:1000,
        timingFunction:"ease",
    })

    this.animation = animation

    animation.scale(2,2).rotate(45).step();

    this.setData({
      animationData:animation.export()
    })

    setTimeout(function(){
      animation.translate(30).step();
      this.setData({
        animationData:animation.export()
      })
    }.bind(this),1000)
  },
  rotateAndScale: function () {
    // 旋转同时放大
    this.animation.rotate(45).scale(2, 2).step()
    this.setData({
      animationData:animation.export()
    })
  },
  rotateThenScale: function () {
    // 先旋转后放大
    this.animation.rotate(45).step()
    this.animation.scale(2, 2).step()
    this.setData({
      animationData:animation.export()
    })
  },
  rotateAndScaleThenTranslate: function () {
    // 先旋转同时放大,然后平移
    this.animation.rotate(45).scale(2, 2).step()
    this.animation.translate(100, 100).step({ duration: 1000 })
    this.setData({
      animationData:animation.export()
    })
  }
})

bug & tip

  1. bug iOS/Android 6.3.30 separates animations by step(), and only the first animation takes effect