javascript創建類_如何使用JavaScript創建吹氣效果

javascript創建類

Have you ever wondered how you can create a realistic air blowing effect with JavaScript? Like the one shown on the evening TV shows, where multiple balls are being mixed up in a sphere-like object by leveraging air pressure? If you want to find out how it's done, read on.

您是否曾經想過如何使用JavaScript創建逼真的吹氣效果? 就像晚上電視節目中顯示的那樣,利用氣壓將多個球混合在一個球形物體中? 如果您想了解它是如何完成的,請繼續閱讀。

? If you want to skip the reading and jump straight to the code, you will find it here. Also, I have deployed a live demo here.?

?如果您想跳過閱讀并直接跳轉到代碼,您將在這里找到它。 另外,我在這里部署了一個現場演示。

研究 (Research)

Recently I have decided to refurbish something old that I did 4 years ago for a project of mine. Here is how it looked:

最近我決定翻新我4年前為我的一個工程做的舊東西。 這是它的外觀:

At that time I chose to use a library called Paperjs. Back then this library let me build the closest thing to what I wanted to achieve.

當時我選擇使用一個名為Paperjs的庫。 那時,該庫使我可以構建最接近我想要實現的東西。

As it turns out, there are many more JavaScript libraries today that let you do animations with or without physics.

事實證明,今天有更多JavaScript庫可以讓您在有或沒有物理的情況下制作動畫。

Before making my final choice, which you will see below, I played around with Anime.js, Velocity.js, Popmotion, Three.js, GreenSock JS, Mo.js and Matter.js. All of them have pluses and minuses, and as with everything else, your choice between them depends on the specific needs you might have. I chose Matter.js.

在做出最終選擇(您將在下面看到)之前,我使用了Anime.js , Velocity.js , Popmotion , Three.js , GreenSock JS , Mo.js和Matter.js 。 它們都有優點和缺點,與其他所有優點一樣,您在它們之間的選擇取決于您可能有的特定需求。 我選擇了Matter.js。

認識Matter.js (Meet Matter.js)

Matter.js is a 2d rigid body JavaScript physics engine. Sounds complex, but it's not. What this actually means is that this library contains all the stuff we need to create realistic 2d physics animations with JavaScript.

Matter.js是2D剛體JavaScript物理引擎。 聽起來很復雜,但事實并非如此。 這實際上意味著該庫包含用JavaScript創建逼真的2D物理動畫所需的所有內容。

For detailed information on what Matter.js has to offer, you might check their documentation. In our case, we will take advantage mainly of the Body module and the features it has. Let's see how in the next section.

有關Matter.js提供的內容的詳細信息,您可以查看其文檔 。 在我們的案例中,我們將主要利用Body模塊及其具有的功能。 讓我們在下一節中了解如何。

球管 (Balls and Tube)

The "tube" component is easy. It's just a background image I am using to create an illusion that the balls are flying around inside a sphere-like glass object.

“管”組件很容易。 這只是我用來產生一種幻覺的背景圖像 ,這些幻覺是球在球形玻璃物體內部飛來飛去。

The interesting part is the code to create the animations and detect the collisions between the balls and the walls. But let's go step by step.

有趣的部分是用于創建動畫并檢測球與墻之間的碰撞的代碼。 但是,讓我們一步一步走。

As I said, the "tube" is a background image I've added via the simple CSS background property. Let's see the balls themselves. For them I had two choices - try to draw circles in canvas and make them look like balls or use simple images. I chose the latter option, as I wanted to have a more realistic view of the balls.

正如我所說的,“ tube”是我通過簡單CSS background屬性添加的背景圖片。 讓我們看看球本身。 對于他們來說,我有兩種選擇-嘗試在畫布上繪制圓并使它們看起來像球形或使用簡單的圖像。 我選擇了后者,因為我想更真實地觀察球。

So, with the help of a graphic processing program, a friend of mine created 75 images for me, one for each ball.

因此,在一個圖形處理程序的幫助下,我的一個朋友為我創建了75張圖像 ,每個球一個。

Having all the assets we need, we are now ready to go deeper and implement some physics with Matter.js.

擁有了我們需要的所有資產之后,我們現在就可以更深入地利用Matter.js實施一些物理了。

實施,測試,實施,測試 (Implement, test, implement, test)

Before going into the animation itself, we need to mention few Matter.js specific things. When creating animations with this library, we need to define, at a minimum:

在進入動畫本身之前,我們需要提及一些Matter.js特定的東西。 使用此庫創建動畫時,我們至少需要定義:

  • Matter.Engine - this is the controller that manages updates to the simulation of the world.

    Matter.Engine-這是管理世界模擬更新的控制器。

  • Matter.World - contains methods for creating and manipulating the world composite.

    Matter.World-包含用于創建和操縱世界復合材料的方法。

  • Matter.Render - this module is a simple HTML5 canvas-based renderer for visualizing instances of Matter.Engine.

    Matter.Render-此模塊是一個簡單的基于HTML5畫布的渲染器,用于可視化Matter.Engine實例。

    Matter.Render - this module is a simple HTML5 canvas-based renderer for visualizing instances of Matter.Engine.

    Matter.Render-此模塊是一個簡單的基于HTML5畫布的渲染器,用于可視化Matter.Engine實例。

    In our example we are also going to use:

    在我們的示例中,我們還將使用:

  • Matter.Bodies for creating the different parts of the scene (the balls, the invisible boundary circle).

    Matter.Body用于創建場景的不同部分(球,不可見的邊界圓)的實體 。

  • Matter.Body for applying forces to the bodies and thus creating a nice physics-based simulation of a blower.

    Matter.Body,用于將力施加到主體上,從而創建基于物理的鼓風機模擬。

  • Matter.Runner to run the whole thing.

    Matter.Runner負責整個過程。

  • Matter.Events gives us ability to have listeners for different events that could happen during the animation. In this specific case we use it for listening for the 'tick' event, which occurs on every render tick.

    Matter.Events使我們能夠讓偵聽器了解動畫過程中可能發生的不同事件。 在此特定情況下,我們使用它來監聽“ tick”事件,該事件在每個渲染滴答中發生。

    In the event handler function we do our checking for when the balls collide with the walls and we apply the relevant forces to create a bounce effect.

    在事件處理程序功能中,我們檢查球何時與壁碰撞,并施加相關力以產生反彈效果。

    We postpone the listening for this event with a 3 second timeout, so we can have a more lotto-like effect. Imagine a sphere where the balls are starting to move when, let's say, a button is pressed.

    我們將此事件的監聽時間延遲了3秒鐘,因此我們可以獲得更像樂透的效果。 假設有一個球體,當按下按鈕時,球開始移動。

試玩 (Try and Play)

In the beginning of this article I posted the link to the GitHub repository with the code and the assets in it. If you want to play more, you can easily check it out and try different modifications. You might want to play with the forces being applied, or the size of the balls, and so on.

在本文的開頭,我發布了到GitHub存儲庫的鏈接,其中包含代碼和資產。 如果您想玩更多游戲,可以輕松查看并嘗試其他修改。 您可能想玩一下所施加的力或球的大小等等。

There is plenty of room for experimenting when we talk about Physics. And it's always fun, especially when we add animations to the picture.

當我們談論物理時,有足夠的實驗空間。 它總是很有趣,尤其是當我們在圖片中添加動畫時。

結論 (Conclusion)

As it turns out, Matter.js is a great library for doing 2d realistic animations backed up by the laws of Physics. Of course, there are other options you might choose from, but as I said, this is a matter of choice and project needs.

事實證明, Matter.js是一個出色的庫,用于制作由物理定律支持的2D逼真的動畫。 當然,您可以選擇其他選項,但是正如我所說,這是選擇和項目需求的問題。

I personally would recommend at least giving it a try and see for yourself. For someone with Flash experience or similar, Matter.js is definitely easy to start with. And if you are stubborn enough to keep trying different settings, you might achieve incredible results.

我個人建議至少嘗試一下,自己看看。 對于具有Flash經驗或類似經驗的人來說,Matter.js絕對很容易上手。 而且,如果您足夠頑固地繼續嘗試其他設置,則可能會獲得令人難以置信的結果。

資源資源 (Resources)

https://brm.io/matter-js/ - The website of the libraryhttps://burakkanber.com/blog/modeling-physics-in-javascript-introduction/ - interesting and well explained articles related to physics in JavaScripthttps://spicyyoghurt.com/tutorials/html5-javascript-game-development/collision-detection-physics/ - collisions detection tutorialhttps://codepen.io/AlexRA96/full/egaxVV - bouncing ball examplehttps://codepen.io/Shokeen/pen/WjKmMG?editors=1010 - codepen example with applying forceshttps://code.tutsplus.com/tutorials/getting-started-with-matterjs-body-module--cms-28835 - beginner tutorial to get you started with Matter.jshttps://codepen.io/jh3y/pen/gOPmMyO?editors=0110 - another cool example with falling bearshttps://codepen.io/danielgivens/pen/geKrRx - even cooler example with a circle clock and particles insidehttps://codepen.io/dotcli/pen/NEXrQe - another example of circle bounds and particles (socks!) inside

https://brm.io/matter-js/-圖書館的網站https://burakkanber.com/blog/modeling-physics-in-javascript-introduction/-與JavaScript相關的有趣有趣的文章https ://spicyyoghurt.com/tutorials/html5-javascript-game-development/collision-detection-physics/-碰撞檢測教程https://codepen.io/AlexRA96/full/egaxVV-彈跳球示例https:// codepen。 io / Shokeen / pen / WjKmMG?editors = 1010-施加力的Codepen示例https://code.tutsplus.com/tutorials/getting-started-with-matterjs-body-module--cms-28835-入門教程您從Matter.js開始https://codepen.io/jh3y/pen/gOPmMyO?editors=0110-另一個酷跌熊的例子https://codepen.io/danielgivens/pen/geKrRx-甚至帶圓圈的更酷的例子https://codepen.io/dotcli/pen/NEXrQe內的時鐘和粒子-里面的圓邊界和粒子(襪子!)的另一個示例

翻譯自: https://www.freecodecamp.org/news/how-to-create-a-lotto-balls-blowing-effect/

javascript創建類

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390322.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390322.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390322.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Bootstrap 4:如何使頂部固定的Navbar保持在容器中而不拉伸?

There are many ways to make a fixed navbar stay inside a parents div container. Well go over the most straightforward one here.有很多方法可以使固定的導航欄停留在父級的div容器中。 我們將在這里介紹最簡單的方法。 Imagine you have the following code, modified…

基于SpringBoot+Mybatis+Thymeleaf商品信息管理系統

github地址:github.com/zaiyunduan1…,如果對你有幫助,歡迎Star 主要用到的技術: 使用maven進行項目構建使用SpringbootMybatis搭建整個系統使用Thymeleaf模板技術實現頁面靜態化使用框架Bootstrap、JQuery開發前端界面使用MySQL和MongoDB分別…

在Mac上為自己手動編譯安裝一套PHP7的開發環境

首先你得去官網下載php7 beta1的版本 這里由于我是在mac上安裝,所以就去下載linux相關的版本,地址也直接附上了php7 beta1windows版的官方也有發布詳情猛戳:這里 解壓安裝包,進入源代碼目錄 tar -zxvf php-7.0.0beta1.tar.gz cd p…

卡特蘭數 HDU2067 HDU4165 HDU1134

題目鏈接:https://vjudge.net/problem/HDU-2067 小兔的棋盤 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11800 Accepted Submission(s): 5952 Problem Description小兔的叔叔從外面旅游回來給她…

Python的用途是什么? Python編程語言有10多種編碼用途。

👋歡迎 (👋 Welcome) Hi! Please take a moment to think about this question: 嗨! 請花一點時間考慮這個問題: How is Python applied in real-world scenarios? Python如何在實際場景中應用? If you are learnin…

Publish/Subscribe

Publish/Subscribe 我們將會投遞一個消息給多個消費者,這種模式被稱為“publish/subscribe” 通俗的講,前面的是點對點隊列模型,現在講的是發布訂閱模型。 Exchanges producer:一個發送消息的用戶應用程序 queue:一個存…

[轉]在ROS下使用zeroconf配置多機通信

原文地址:http://www.corvin.cn/635.html,轉載主要方便隨時查閱,如有版權要求,請及時聯系。 0x00 為何需要配置ROS多機通信 眾所周知ROS是分布式系統,因此可以將機器人需要處理的復雜、計算量大的任務分解在多臺機器上…

python中斐波那契數列_斐波那契數列–在Python,JavaScript,C ++,Java和Swift中進行了解釋...

python中斐波那契數列by Pau Pavn通過保羅帕文(PauPavn) The Fibonacci sequence is, by definition, the integer sequence in which every number after the first two is the sum of the two preceding numbers. To simplify:根據定義,斐波那契數列是整數序列&a…

1583. 統計不開心的朋友

1583. 統計不開心的朋友 給你一份 n 位朋友的親近程度列表,其中 n 總是 偶數 。 對每位朋友 i,preferences[i] 包含一份 按親近程度從高到低排列 的朋友列表。換句話說,排在列表前面的朋友與 i 的親近程度比排在列表后面的朋友更高。每個列…

uva 247(floyd傳遞閉包)

為什么&#xff0c;逗號后面&#xff0c;還有空格........ #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> #include <map> using namespace std; const int maxn50; int d[maxn][max…

VS Code 的常用快捷鍵和插件

注:文章摘自 風行天下一萬號 - 博客園 vs code 的常用快捷鍵 1、注釋&#xff1a; 單行注釋&#xff1a;[ctrlk,ctrlc] 或 ctrl/取消單行注釋&#xff1a;[ctrlk,ctrlu] (按下ctrl不放&#xff0c;再按k u)多行注釋&#xff1a;[altshiftA]多行注釋&#xff1a;/**2、移動行&a…

python包numpy_NumPy Python科學計算軟件包的終極指南

python包numpyNumPy (pronounced "numb pie") is one of the most important packages to grasp when you’re starting to learn Python.NumPy(讀作“麻木派”)是您開始學習Python時要掌握的最重要的軟件包之一。 The package is known for a very useful data str…

NGINX原理 之 SLAB分配機制(轉)

1 引言 眾所周知&#xff0c;操作系統使用伙伴系統管理內存&#xff0c;不僅會造成大量的內存碎片&#xff0c;同時處理效率也較低下。SLAB是一種內存管理機制&#xff0c;其擁有較高的處理效率&#xff0c;同時也有效的避免內存碎片的產生&#xff0c;其核心思想是預分配。其按…

apk之間數據共享的方式

1、四大組件之ContentProvider大法2、shareUserId3、apk均去遠端獲取配置文件&#xff08;或接口&#xff09;4、AIDL&#xff08;bindService&#xff09;5、SharePreference設置為MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE模式&#xff0c;由于存在安全問題&#xff0c;已被…

藍橋杯java 基礎練習 十六進制轉十進制

問題描述從鍵盤輸入一個不超過8位的正的十六進制數字符串&#xff0c;將它轉換為正的十進制數后輸出。注&#xff1a;十六進制數中的10~15分別用大寫的英文字母A、B、C、D、E、F表示。樣例輸入FFFF樣例輸出65535import java.math.BigInteger; import java.util.Scanner;public …

dynamic web module消失不見

2019獨角獸企業重金招聘Python工程師標準>>> 方法1&#xff1a;在project Facets選項中勾選Dynamic Web Module即可 方法2&#xff1a; 我用eclipse對項目進行修改名稱&#xff0c;修改成功后。項目就沒有Deployment Descriptor&#xff08;如下圖紅色框中&#xff…

576. 出界的路徑數

576. 出界的路徑數 給你一個大小為 m x n 的網格和一個球。球的起始坐標為 [startRow, startColumn] 。你可以將球移到在四個方向上相鄰的單元格內&#xff08;可以穿過網格邊界到達網格之外&#xff09;。你 最多 可以移動 maxMove 次球。 給你五個整數 m、n、maxMove、star…

telnet命令發送郵件

下面的例子是用qq的smtp服務器。 set localecho 本地回顯啟用 telnet smtp.qq.com 25 220 smtp.qq.com Esmtp QQ Mail Server helo sis 250 smtp.qq.com//服務器返回250 smtp.qq.com STARTTLS 220 Ready to start TLS//服務器返回 220 準備開啟TLS通訊 auth login 334 VXNlcm5h…

myelcipse和maven搭建項目

偷懶一下&#xff0c;完了補充 轉載&#xff1a;https://www.cnblogs.com/jr1260/p/6438811.html https://www.cnblogs.com/yangmingyu/p/6908519.html https://www.cnblogs.com/henuyuxiang/p/6288476.html 轉載于:https://www.cnblogs.com/0914lx/p/8342343.html

551. 學生出勤記錄

551. 學生出勤記錄 I 給你一個字符串 s 表示一個學生的出勤記錄&#xff0c;其中的每個字符用來標記當天的出勤情況&#xff08;缺勤、遲到、到場&#xff09;。記錄中只含下面三種字符&#xff1a; ‘A’&#xff1a;Absent&#xff0c;缺勤 ‘L’&#xff1a;Late&#xff…