react開發模式_通過開發帶有精靈動畫的游戲來學習高級React模式

react開發模式

by Pavel Vlasov

通過帕維爾·弗拉索夫(Pavel Vlasov)

通過開發帶有精靈動畫的游戲來學習高級React模式 (Learn advanced React patterns by developing a game with sprite animation)

Have you ever wanted to learn some advanced React patterns? Or build your own game engine? If at least one answer is yes, then this article is for you.

您是否曾經想學習一些高級React模式? 還是構建自己的游戲引擎? 如果至少有一個答案是肯定的,那么本文適合您。

In this tutorial, you’ll learn how to build basic sprite animation using React, styles-components, and requestAnimationFrame. At the end you’ll be able to create characters like this:

在本教程中,您將學習如何使用React , styles-componentsrequestAnimationFrame來構建基本的精靈動畫。 最后,您將可以創建如下字符:

You may ask me why can’t I learn it another way? Well… There’re three reasons for that:

您可能會問我, 為什么我不能以其他方式學習它 ? 好吧……這有三個原因:

So, let’s do it! ?

所以,讓我們開始吧! ?

讓我們從一些理論開始 (Let’s start with a bit of a theory)

What is a sprite animation? Wikipedia says that

什么是精靈動畫? 維基百科說

In computer graphics, a sprite is a two-dimensional bitmap that is integrated into a larger scene.

在計算機圖形學中, 子畫面是一個二維位圖,已集成到較大的場景中。

So basically sprite animation is a repeatedly changing two-dimensional bitmap.

因此,基本上,精靈動畫是一個反復變化的二維位圖。

Sprite is usually represented like a png image with different states of the animation:

Sprite通常表示為具有不同動畫狀態的png圖像:

We’ll start by creating a tile component that will show us one frame at a time and allow us to change frames with state property:

我們將首先創建一個tile組件,該組件一次向我們顯示一幀,并允許我們使用state屬性更改幀:

Basically, we’ll need to show one part of the image at a time and hide the rest. Pretty straightforward.

基本上,我們需要一次顯示圖像的一部分,然后隱藏其余部分。 非常簡單。

(Tile)

First of all, we’ll create a container component to create the shape of our frame:

首先,我們將創建一個容器組件以創建框架的形狀:

width and height represent the size of the tale, and scale increases the size of the image. overflow: hidden will hide the unused part of the image and transform-origin will make a container to keep its top and left the same when we scale it.

widthheight代表故事的大小,而scale增加圖像的大小。 overflow: hidden將隱藏圖像的未使用部分,而transform-origin將使容器在縮放時保持頂部和頂部不變。

Now we need to adjust the position of the inner image. We’ll use the transform: translate CSS property for that:

現在我們需要調整內部圖像的位置。 我們將使用transform: translate CSS屬性:

Now let’s combine everything together in the tile component:

現在,讓我們在tile組件中將所有內容組合在一起:

  • src property contains a link to the image

    src屬性包含圖像的鏈接

  • tile is the object with width and height fields, represents the size of the tile

    tile是具有widthheight字段的對象,代表tile的大小

  • state frame index

    state框架索引

  • scale property to increase the size of the image (For example, scale = 2 is 2x image)

    scale屬性以增加圖像的大小(例如, scale = 2是2x圖像)

In the next step, we’ll add some movement to our image.

在下一步中,我們將為圖像添加一些移動。

雪碧 (Sprite)

We’ll use requestAnimationFrame for that. You may ask why we don’t use setTimeout or setInterval. The problem with timeouts is that the callback will fire somewhere in between frames, that may result in clunky animation.

我們將為此使用requestAnimationFrame 。 您可能會問為什么我們不使用setTimeoutsetInterval。 超時的問題在于回調將在幀之間的某個位置觸發,這可能會導致笨拙的動畫。

Also, requestAnimationFrame allows us to synchronize animations of different objects on the screen. In the game you’ll have lots of them!

另外, requestAnimationFrame允許我們同步屏幕上不同對象的動畫。 在游戲中,您將有很多!

Let’s put together a Sprite component:

讓我們把一個Sprite組件放在一起:

In the animate function, we should change the state of the frame and request a new animation frame:

animate功能中,我們應該更改幀的state并請求一個新的動畫幀:

We use the eframesPerStep property to control the number of states per frame, so our animation won’t be too fast.

我們使用framesPerStep屬性來控制每幀的狀態數,因此動畫不會太快。

那槍呢? ? (What about a gun? ?)

Now the only thing we need to do is combine our sprite with the gun image:

現在,我們唯一需要做的就是將我們的精靈與槍的圖像結合起來:

And you should get the following result:

并且您應該得到以下結果:

The best way to learn something it to build it by yourself. So I encourage you to use this codesandbox:

自己學習構建東西的最好方法。 因此,我鼓勵您使用以下codeandbox :

The TypeScript version is available here as well.

這里也提供 TypeScript版本。

As a bonus, you can implement different animations using files from the assets folder.

另外,您可以使用資產文件夾中的文件來實現不同的動畫。

You can find the source code here. I used game assets made by finalbossblues.

您可以在此處找到源代碼。 我使用的是finalbossblues制造的游戲資產。

Hope you enjoyed the article! ?

希望您喜歡這篇文章! ?

Follow me on Medium and Twitter to get more updates on new articles. Also, share this article to help others know about it. Sharing is caring ?

在Medium和Twitter上關注我,以獲取有關新文章的更多更新。 另外,分享這篇文章以幫助其他人了解它。 分享在乎嗎?

Destroy this clap button if you want more.

如果您想要更多,請破壞此拍手按鈕。

You can clap up to 50 times! ?

您最多可以拍手50次!

Some more resources about the topic:

有關該主題的更多資源:

Understanding JavaScript's requestAnimationFrame() method for smooth animationsrequestAnimationFrame() is a JavaScript method for creating smoother, less resource intensive JavaScript animations…www.javascriptkit.com

了解JavaScript的requestAnimationFrame()方法以實現平滑的動畫 requestAnimationFrame()是一種JavaScript方法,可用于創建更平滑,資源占用更少JavaScript動畫…… www.javascriptkit.com

Originally published at react.camp.

最初發表于react.camp 。

翻譯自: https://www.freecodecamp.org/news/learn-advanced-react-patterns-by-developing-a-game-with-sprite-animation-5dc072886975/

react開發模式

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

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

相關文章

js腳本鎖計算機軟件,JS使用插件cryptojs進行加密解密數據實例

本文實例講述了JS使用插件cryptojs進行加密解密數據。分享給大家供大家參考,具體如下:Testing websocketsvar key BOTWAVEE;//CBC模式加密function encryptByDESModeCBC(message) {var keyHex CryptoJS.enc.Utf8.parse(key);var ivHex CryptoJS.enc.U…

nginx、Apache、IIS服務器解決 413 Request Entity Too Large問題方法匯總

一、nginx服務器 nginx出現這個問題的原因是請求實體太長了。一般出現種情況是Post請求時Body內容Post的數據太大了,如上傳大文件過大、POST數據比較多。處理方法 在nginx.conf增加 client_max_body_size的相關設置, 這個值默認是1m,可以增加到8m以增加提…

python代理池好難啊_新人不會自己搭建代理池?快來引用大佬的

新人不會自己搭建代理池?快來引用大佬的對于新人學習爬蟲來說,雖然不會爬取太難的網站,但是有時候爬取的數據量過大的時候,也會遇到返回不了數據的問題,這時候打開網頁一看.可能會看到"你的ip訪問頻率太高"這樣的提示,出現這種問題的原因可能是,你被封ip啦.但是爬蟲…

pat 1123(AVL)

題意:給n個數,按照順序插入AVL,輸出層次遍歷,如果這顆AVL是完全二叉樹,輸出YES 否則輸出NO 當時考試的時候。忘記AVL什么時候旋轉了,數據結構不會寫,感覺藥丸 判斷完全二叉樹,這個簡…

leetcode435. 無重疊區間(貪心算法)

給定一個區間的集合,找到需要移除區間的最小數量,使剩余區間互不重疊。 注意: 可以認為區間的終點總是大于它的起點。 區間 [1,2] 和 [2,3] 的邊界相互“接觸”,但沒有相互重疊。 示例 1: 輸入: [ [1,2], [2,3], [3,4], [1,3] ] 輸出: 1 …

AMD規范(RequireJS)、CMD規范(SeaJS)、CommonJS(BravoJS)規范的辨析

首先,AMD,CMD,CommonJS都實現了文件模塊化。 對于依賴的模塊:AMD是提前執行;CMD是延遲執行; AMD是依賴前置,CMD是依賴就近; AMD官方解釋:https://github.com/amdjs/amdjs…

客戶旅程_我們進入微服務世界的旅程-以及從中學到的東西。

客戶旅程by Ignacio Salazar Williams通過伊格納西奧薩拉薩爾威廉姆斯(Ignacio Salazar Williams) 我們進入微服務世界的旅程-以及從中學到的東西。 (Our journey into the world of Microservices — and what we learned from it.) I know, I know everyone is talking abou…

英才計劃計算機潛質測評試題,湖北省2020年“英才計劃”潛質測試的通知

12月3日,湖北省青少年科技中心發布湖北省2020年“英才計劃”潛質測試的通知,潛質測試分為筆試和機試兩部分測試時間為2019年12月7日。各相關單位:根據《中國科協辦公廳 教育部辦公廳關于開展2020年“英才計劃”工作的通知》(科協辦發青字〔20…

leetcode1253. 重構 2 行二進制矩陣(貪心算法)

給你一個 2 行 n 列的二進制數組: 矩陣是一個二進制矩陣,這意味著矩陣中的每個元素不是 0 就是 1。 第 0 行的元素之和為 upper。 第 1 行的元素之和為 lower。 第 i 列(從 0 開始編號)的元素之和為 colsum[i],colsum…

Spring Cloud Config服務端配置細節(一)

上篇文章我們看了Spring Cloud中分布式配置中心的一個基本使用,這里邊還涉及到許多細節,本文我們就來看看服務端配置中的一些細節。 本文是Spring Cloud系列的第二十三篇文章,了解前二十二篇文章內容有助于更好的理解本文: 1.使用…

POJ 1797 Heavy Transportation

傳送門&#xff1a;http://poj.org/problem?id1797 不想吐槽了&#xff0c;弄了好久才AC 實現代碼&#xff1a; #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cstdio> #include <iostream> u…

java8中方法區的內存大小如何設置_從Java8升級到Java11

奇技 指南為什么選擇Java11?容器環境支持&#xff0c;GC等領域的增強&#xff0c;僅通過切換到 Java 11 就有 16&#xff05; 的改進。進行了瘦身&#xff0c;更輕量級&#xff0c;安裝包體積小。JDK11 是一個長期支持版。1Java11相對于Java8的一些新特性1.變量類型推斷Var關…

TCP建立連接

TCP的連接建立過程被稱為三次握手:第一次握手&#xff1a;客戶A的TCP向服務器B發出連接請求報文段,其首部中的同步位SYN 1 ,并選擇序號seq x,表明傳送| 數據時的第一 個數據字節的序號是X。第二次握手:B的TCP收到連接請求報文段后,如果同意,則發回確認。ACK1,其確認號ackx1。同…

webgl 著色器_如何使用AI,AR和WebGL著色器來幫助視障人士

webgl 著色器by Dan Ruta通過Dan Ruta 如何使用AI&#xff0c;AR和WebGL著色器來幫助視障人士 (How you can use AI, AR, and WebGL shaders to assist the visually impaired) Today, about 4% of the world’s population is visually impaired. Tasks like simple navigati…

計算機語言乍么設置,電腦如何設置語言

設置語言欄其實語言欄是用來進行輸入法的切換的。當你需要在Windows中進行文字輸入的時候,就需要用語言欄了,因為Windows的默認輸入語言是英文,在這種情況下,你用鍵盤在文本里輸入的文字會是英文字母,所以作為中國人的我們要想在Windows里輸入中文的話,就需要語言欄的幫助了。試…

hive 初認識

結構Hive 是建立在hadoop上的數據倉庫架構,它提供了一系列的工具,可以進行數據提取轉換加載(這個過程叫做ETL),這是一種可以存儲,查詢和分析存儲在hadoop中的大規模數據的機制.Hive定義了簡單的類SQL查詢語句 成為hql,他允許數據SQL的用戶查詢數據.同時 這個語言也允許數據mapr…

git使用(2)

1.遠程倉庫 a SSHKEY 第1步&#xff1a;創建SSH Key。在用戶主目錄下&#xff0c;看看有沒有.ssh目錄&#xff0c;如果有&#xff0c;再看看這個目錄下有沒有id_rsa和id_rsa.pub這兩個文件&#xff0c;如果已經有了&#xff0c;可直接跳到下一步。如果沒有&#xff0c;打開Shel…

郵件中的商務英語

一、常見縮寫 CC carbon copy&#xff1a;抄送 FYI for your information&#xff1a;供你參考 EOD end of the day BTW By the way&#xff1a;順便提一下 COB close of the business 這兩個詞都是指下班前。需要催促某人在下班前給到回復的時候可以用用它們。 eg: Ple…

vue 橫向菜單滾動定位_使用vue組件+iscroll實現一個橫向菜單,不能正確滑動

使用vue組件iscroll實現一個橫向菜單&#xff0c;可是卻不能滑動&#xff0c;給父元素ul寫死一個寬度可以滑動。但是&#xff0c;我在computed里計算寬度&#xff0c;直接路由進去不能滑動&#xff0c;當我進入別的組件(切換路由)回來又可以滑動了示例地址&#xff1a;http://o…

leetcode1353. 最多可以參加的會議數目(貪心算法)

給你一個數組 events&#xff0c;其中 events[i] [startDayi, endDayi] &#xff0c;表示會議 i 開始于 startDayi &#xff0c;結束于 endDayi 。 你可以在滿足 startDayi < d < endDayi 中的任意一天 d 參加會議 i 。注意&#xff0c;一天只能參加一個會議。 請你返…