In a small program project, you need to add a parabolic ball animation when adding a shopping cart.
This kind of animation with no fixed starting position naturally cannot use gif images, so it can only be implemented with native code
So what tools do we have to implement animation? Woolen cloth?
The applet provides JS API createAnimation to create animations
CSS transition
Tools include Now, let’s take a look at what a parabola is.
Here we only discuss the horizontal parabola. From the mathematical principle, the horizontal parabola is [movement with constant horizontal speed and vertical acceleration]. The conversion to the code level is in the animation effect timingFunction. The horizontal animation uses linear and the vertical animation uses ease-in
So we need to decompose this parabolic animation into two animations that are performed at the same time but with different animation effects.
JS:
cartAnimation(x, y) { // x y 为手指点击的坐标,即球的起始坐标 let self = this, cartY = app.globalData.winHeight - 50, // 结束位置(购物车图片)纵坐标 cartX = 50, // 结束位置(购物车图片)的横坐标 animationX = flyX(cartX, x), // 创建球的横向动画 animationY = flyY(cartY, y) // 创建球的纵向动画 this.setData({ ballX: x, ballY: y, showBall: true }) setTimeoutES6(100).then(() => { // 100 ms 延时,确保球已经到位并显示 self.setData({ animationX: animationX.export(), animationY: animationY.export(), }) return setTimeoutES6(400) // 400 ms 是球的抛物线动画时长 }).then(() => { // 400 ms 延时后隐藏球 this.setData({ showBall: false, }) }) }function setTimeoutES6(sec) { // Promise 化 setTimeout return new Promise((resolve, reject) => { setTimeout(() => {resolve()}, sec) }) }function flyX(cartX, oriX) { // 水平动画 let animation = wx.createAnimation({ duration: 400, timingFunction: 'linear', }) animation.left(cartX).step() return animation }function flyY(cartY, oriY) { // 垂直动画 let animation = wx.createAnimation({ duration: 400, timingFunction: 'ease-in', }) animation.top(cartY).step() return animation }
HTML:
<view animation="{{animationY}}" style="position:fixed;top:{{ballY}}px;" hidden="{{!showBall}}"> <view class="ball" animation="{{animationX}}" style="position:fixed;left:{{ballX}}px;"></view></view>
As far as I know, the animation implemented using transform: translate() will have better performance than top & left, but it is not that easy to implement.
After much research, I found that the method of translate is one step more than the method of top & left, that is, the translate displacement of the ball needs to be restored (otherwise translate will always have a value) to ensure that the next displacement will change from click The position of the beginning
cartAnimation(x, y) { let self = this, cartY = app.globalData.winHeight - 50, cartX = 50, animationX = flyX(cartX, x), animationY = flyY(cartY, y) this.setData({ leftNum: x, topNum: y, showBall: true }) setTimeoutES6(100).then(() => { self.setData({ animationDataX: animationX.export(), animationDataY: animationY.export(), }) return setTimeoutES6(400) }).then(() => { this.setData({ showBall: false, animationX: flyX(0, 0, 0).export(), // 还原小球位置,即 translate 恢复默认值 animationY: flyY(0, 0, 0).export(), }) }) }function flyX(cartX,oriX,duration) { let animation = wx.createAnimation({ duration: duration||400, timingFunction: 'linear', }) animation.translateX(cartX-oriX).step() return animation }function flyY(cartY,oriY,duration) { let animation = wx.createAnimation({ duration: duration||400, timingFunction: 'ease-in', }) animation.translateY(cartY-oriY).step() return animation }
HTML part remains unchanged
In addition to small programs, of course, H5 is more commonly used in daily front-end development. Below I Will use CSS3 transition method to achieve
<!DOCTYPE html><html lang="en" style="width:100%;height:100%;"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> <style> * { padding: 0; margin: 0; } #ball { width:12px; height:12px; background: #5EA345; border-radius: 50%; position: fixed; transition: left 1s linear, top 1s ease-in; } </style> <title>CSS3 水平抛物线动画</title></head><body style="width:100%;height:100%;"> <p id="ball"></p></body><script> var $ball = document.getElementById('ball'); document.body.onclick = function (evt) { console.log(evt.pageX,evt.pageY) $ball.style.top = evt.pageY+'px'; $ball.style.left = evt.pageX+'px'; $ball.style.transition = 'left 0s, top 0s'; setTimeout(()=>{ $ball.style.top = window.innerHeight+'px'; $ball.style.left = '0px'; $ball.style.transition = 'left 1s linear, top 1s ease-in'; }, 20) }</script></html>
Related recommendations:
JS parabolic animation operation example sharing
About html5 Sharing of Parabolic Movement Skills in the Medium Picture
Code Case for Parabolic Movement of Elastic Potential Energy Animation in JavaScript
The above is the detailed content of Code example for implementing parabolic animation in JS. For more information, please follow other related articles on the PHP Chinese website!