How to create a GIF from images in React
In this article, we will show you how to create a GIF from images in React. If you have a list of images and you would like to convert the images to GIF, here we will explain to you step by step.
Checkout more articles on ReactJS
Demo Application
Steps to create a GIF from images in React
1. Create a React application
Let’s create a react application using create-react-app
. Run the following command to create an application.
1 | npx create-react-app react-images-to-gif |
2. Install NPM package
Here, we’ll use the gifshot NPM package to create a GIF from the list of images. Run the following command to install the NPM package.
1 | npm i gifshot |
3. Create a component
Let’s create a simple component where we will add the button to create a GIF from images.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import React from 'react'; function App() { const handleClick = () => { } return ( <div classname="App"> <h3>Create a GIF from images in React - <a href="https://www.cluemediator.com/" target="_blank" rel="noopener">Clue Mediator</a></h3> <button onclick="{handleClick}">Click to create a GIF</button> </div> ); } export default App; |
Now, let’s import the gifshot
at the top of the page and add the following command to convert the images to GIF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import React, { useState } from 'react'; import { createGIF } from 'gifshot'; function App() { const [progress, setProgress] = useState(0); const handleClick = () => { const images = [ 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg' ]; const options = { images: images, gifWidth: 500, gifHeight: 300, numWorkers: 5, frameDuration: 0.01, sampleInterval: 10, progressCallback: e => setProgress(parseInt(e * 100)) }; createGIF(options, obj => { if (!obj.error) { console.log('GIF Image: ', obj.image); } }); } return ( <div classname="App"> <h3>Create a GIF from images in React - <a href="https://www.cluemediator.com/" target="_blank" rel="noopener">Clue Mediator</a></h3> <button onclick="{handleClick}">Click to create a GIF</button> {progress !== 0 && <label>Creating GIF... {progress}%</label>} </div> ); } export default App; |
Here, we will write the code to download the Base64 GIF image. If you do not know, refer to the following article.
Reference Article: How to download a base64 image in JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import React, { useState } from 'react'; import { createGIF } from 'gifshot'; function App() { const [progress, setProgress] = useState(0); const handleClick = () => { const images = [ 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/07/20/22/flowers-7180947__340.jpg', 'https://cdn.pixabay.com/photo/2021/11/19/11/55/field-6809045__340.jpg', 'https://cdn.pixabay.com/photo/2022/05/08/20/21/flowers-7182930__340.jpg', 'https://cdn.pixabay.com/photo/2021/08/13/21/14/kiteboarding-6543982__340.jpg' ]; const options = { images: images, gifWidth: 500, gifHeight: 300, numWorkers: 5, frameDuration: 0.01, sampleInterval: 10, progressCallback: e => setProgress(parseInt(e * 100)) }; createGIF(options, obj => { if (!obj.error) { const link = document.createElement('a'); link.download = 'sample.gif'; link.href = obj.image; link.click(); setProgress(0); } }); } return ( <div classname="App"> <h3>Create a GIF from images in React - <a href="https://www.cluemediator.com/" target="_blank" rel="noopener">Clue Mediator</a></h3> <button onclick="{handleClick}">Click to create a GIF</button> {progress !== 0 && <label>Creating GIF... {progress}%</label>} </div> ); } export default App; |
4. Output
Let’s run the application and check the output in the browser.
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂