Code example for implementing parabolic animation in JS
In a small program project, you need to add a parabolic ball animation when adding a shopping cart.
Analysis
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.
Implementation
(1) Implementation of small program
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>
translate optimization
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
(2) Implementation of H5
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.
