{"id":20405,"date":"2022-10-04T07:14:09","date_gmt":"2022-10-04T14:14:09","guid":{"rendered":"https:\/\/coderpad.io\/?p=20405"},"modified":"2023-06-07T13:57:09","modified_gmt":"2023-06-07T20:57:09","slug":"interactive-animated-backgrounds-react-tsparticles","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/interactive-animated-backgrounds-react-tsparticles\/","title":{"rendered":"How to Create Interactive Animated Backgrounds in React with tsParticles"},"content":{"rendered":"\n<p>Adding animations to a site is a great way to make it stand out on the web, even better when it&#8217;s interactive. However, depending on the complexity, achieving these effects can require a lot of time and experience.&nbsp;<\/p>\n\n\n\n<p>Unless, of course, you utilize <a href=\"https:\/\/particles.js.org\/\" target=\"_blank\" rel=\"noopener\">tsParticles<\/a>.&nbsp;<\/p>\n\n\n\n<p>This lightweight library helps you build stunning interactive animations with a few lines of code. It includes customization features that help you achieve animations far more advanced than just floating particles on the screen:<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"750\" style=\"aspect-ratio: 1600 \/ 750;\" width=\"1600\" autoplay controls loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/tsParticleintrovid.mp4\"><\/video><figcaption class=\"wp-element-caption\">Source: <a href=\"https:\/\/codepen.io\/collection\/DPOage?cursor=eyJjb2xsZWN0aW9uX2lkIjoiRFBPYWdlIiwiY29sbGVjdGlvbl90b2tlbiI6bnVsbCwibGltaXQiOjQsIm1heF9pdGVtcyI6MTYxLCJvZmZzZXQiOjAsInBhZ2UiOjEsInNvcnRfYnkiOiJwb3NpdGlvbiIsInNvcnRfb3JkZXIiOiJBc2MifQ==\" target=\"_blank\" rel=\"noreferrer noopener\">Matteo Bruni<\/a><\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this tutorial, we will show you how to use tsParticles in React to create interactive animated backgrounds. To follow along, you should be familiar with React and have Node installed on your system.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2705You can use the CoderPad sandboxes&nbsp;located throughout this page or&nbsp;<a href=\"https:\/\/app.coderpad.io\/sandbox?question_id=229542\" target=\"_blank\" rel=\"noreferrer noopener\">as a new browser window<\/a>&nbsp;to run the code in this tutorial.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to tsParticles<\/h2>\n\n\n\n<p>tsParticles is a successor of <a href=\"https:\/\/vincentgarreau.com\/particles.js\/\" target=\"_blank\" rel=\"noreferrer noopener\">particles.js<\/a>, an older library with similar functionality whose development is no longer active. It is backward-compatible with particles.js and has similar configuration options, so you can easily migrate from one to the other.&nbsp;<\/p>\n\n\n\n<p>tsParticles has lots of new features, like first-party integrations with leading frameworks, a frame per second (fps) limiter so as not to overwork the CPU, and the ability to use Font Awesome icons and images as particles.<\/p>\n\n\n\n<p>tsParticles might take some time to get used to due to the numerous <a href=\"https:\/\/particles.js.org\/docs\/classes\/Options_Classes_Options.Options.html#options\" target=\"_blank\" rel=\"noreferrer noopener\">configuration options<\/a> available for creating your desired particle animation. To help with this we will be exploring a few particle animations you can create with this library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get started by setting up React and tsParticles&nbsp;<\/h2>\n\n\n\n<p>For some of the popular frameworks there are ready-to-use tsParticle components available. In our case, we will be using the <a href=\"https:\/\/github.com\/matteobruni\/tsparticles\/tree\/main\/components\/react\" target=\"_blank\" rel=\"noreferrer noopener\">official tsParticles React component<\/a>. We will also use <a href=\"https:\/\/www.npmjs.com\/package\/tsparticles\" target=\"_blank\" rel=\"noreferrer noopener\">tsParticles Full Bundle<\/a>, which has more features available than the <a href=\"https:\/\/www.npmjs.com\/package\/tsparticles-slim\" target=\"_blank\" rel=\"noreferrer noopener\">tsParticles Slim Bundle<\/a>.<\/p>\n\n\n\n<p>Type in the following command in the terminal to set up a React app, install the needed dependencies, and start up the development server.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">npx create-react-app particle-background\ncd particle-background\nnpm i react-particles tsparticles\nnpm start<\/code><\/span><\/pre>\n\n\n<p>Now let\u2019s initialize tsParticles and render the Particles component in the <strong>src\/App.js<\/strong> file. Here is what the file will look like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> Particles <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-tsparticles\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { loadFull } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"tsparticles\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { useCallback } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 <span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 \u00a0 <span class=\"hljs-comment\">\/\/...<\/span>\n\u00a0 };\n\u00a0\n\u00a0 <span class=\"hljs-keyword\">const<\/span> particlesInit = useCallback(<span class=\"hljs-keyword\">async<\/span> (engine) =&gt; {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">await<\/span> loadFull(engine);\n\u00a0 }, &#91;]);\n\n\u00a0 <span class=\"hljs-keyword\">return<\/span> (\n\u00a0 \u00a0 <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Particles<\/span> <span class=\"hljs-attr\">options<\/span>=<span class=\"hljs-string\">{options}<\/span> <span class=\"hljs-attr\">init<\/span>=<span class=\"hljs-string\">{particlesInit}<\/span> \/&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n\u00a0 );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The Particles component accepts <a href=\"https:\/\/github.com\/matteobruni\/tsparticles\/tree\/main\/components\/react#props\" target=\"_blank\" rel=\"noreferrer noopener\">several properties<\/a> among which is the options prop. This is where we will be passing the configurations for creating the particle animation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understand the configuration options<\/h2>\n\n\n\n<p>To demonstrate how the configuration options work, we\u2019ll start with <a href=\"https:\/\/codepen.io\/matteobruni\/pen\/ZExLYyg\" target=\"_blank\" rel=\"noreferrer noopener\">this example <\/a>created by <a href=\"https:\/\/codepen.io\/matteobruni\" target=\"_blank\" rel=\"noreferrer noopener\">Matteo Bruni<\/a>. It features a particle animation with floating circles in the brand colors of the popular communication software <a href=\"http:\/\/www.slack.com\" target=\"_blank\" rel=\"noreferrer noopener\">Slack<\/a>. The circles link together when they get close to each other and link with a nearby mouse hover. A click creates an extra four particles.&nbsp;<\/p>\n\n\n\n<p>Here are the configuration options:&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 \u00a0 <span class=\"hljs-attr\">particles<\/span>: {\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">number<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-number\">80<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">density<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">area<\/span>: <span class=\"hljs-number\">800<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">color<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: &#91;<span class=\"hljs-string\">\"#2EB67D\"<\/span>, <span class=\"hljs-string\">\"#ECB22E\"<\/span>, <span class=\"hljs-string\">\"#E01E5B\"<\/span>, <span class=\"hljs-string\">\"#36C5F0\"<\/span>]\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">shape<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"circle\"<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">opacity<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-number\">1<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">size<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: { <span class=\"hljs-attr\">min<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-attr\">max<\/span>: <span class=\"hljs-number\">8<\/span> }\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">links<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">distance<\/span>: <span class=\"hljs-number\">150<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">\"#808080\"<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">0.4<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">width<\/span>: <span class=\"hljs-number\">1<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">move<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">speed<\/span>: <span class=\"hljs-number\">5<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">direction<\/span>: <span class=\"hljs-string\">\"none\"<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">random<\/span>: <span class=\"hljs-literal\">false<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">straight<\/span>: <span class=\"hljs-literal\">false<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">outModes<\/span>: <span class=\"hljs-string\">\"out\"<\/span>\n\u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 },\n\u00a0 \u00a0 <span class=\"hljs-attr\">interactivity<\/span>: {\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">events<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">onHover<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">mode<\/span>: <span class=\"hljs-string\">\"grab\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">onClick<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">mode<\/span>: <span class=\"hljs-string\">\"push\"<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">modes<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">grab<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">distance<\/span>: <span class=\"hljs-number\">140<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">links<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">1<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">push<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">quantity<\/span>: <span class=\"hljs-number\">4<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 }\n\u00a0 };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the result of the above configs:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=229832&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>There are lots of different options that go into the animation\u2013most of them are self-explanatory from their names. But if you\u2019re unfamiliar they might still seem a bit perplexing. Here are two in particular that often cause confusion:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/particles.js.org\/docs\/classes\/Options_Classes_Particles_ParticlesOptions.ParticlesOptions.html\" target=\"_blank\" rel=\"noreferrer noopener\">particles<\/a>: This option handles everything that has to do with the display and behavior of the particles. In the above config, we set the number of particles, their colors, the shape (\u201ccircle\u201d), as well as the particles\u2019 opacity, size range, and movement type. We also set the details of the links, which are the lines connecting the particles when they are close.<\/li>\n\n\n\n<li><a href=\"https:\/\/particles.js.org\/docs\/classes\/Options_Classes_Interactivity_Interactivity.Interactivity.html\" target=\"_blank\" rel=\"noreferrer noopener\">interactivity<\/a>: This option controls what happens when click and hover events are triggered, and when a particle intersects with a div element. In the above config, when a hover occurs we are calling the grab mode which links nearby particles. When a click occurs we call the push mode which adds four more particles.<\/li>\n<\/ul>\n\n\n\n<p>There are even more options and properties than we covered above, all of which can be explored in the <a href=\"https:\/\/particles.js.org\/docs\/interfaces\/Options_Interfaces_IOptions.IOptions.html\" target=\"_blank\" rel=\"noreferrer noopener\">official documentation<\/a> and by playing around with the <a href=\"https:\/\/codepen.io\/collection\/DPOage?cursor=eyJjb2xsZWN0aW9uX2lkIjoiRFBPYWdlIiwiY29sbGVjdGlvbl90b2tlbiI6bnVsbCwibGltaXQiOjQsIm1heF9pdGVtcyI6MTYxLCJvZmZzZXQiOjAsInBhZ2UiOjEsInNvcnRfYnkiOiJwb3NpdGlvbiIsInNvcnRfb3JkZXIiOiJBc2MifQ==\" target=\"_blank\" rel=\"noreferrer noopener\">available demos.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create more advanced effects using the Emitters and Absorbers plugins<\/h2>\n\n\n\n<p>As the name implies, emitters are for releasing\/giving off particles while absorbers are for absorbing particles. These are advanced options that can be used to create particle animation like confetti, fireworks, black hole effects, and more.&nbsp;<\/p>\n\n\n\n<p>The docs don\u2019t currently include helpful information regarding these options \u2013 but you can figure out how they work from the available demos that I\u2019ve outlined below.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s create a black hole animation where particles are released using emitters and sucked up using absorbers. Here is a GIF showing what we want to create.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"356\" style=\"aspect-ratio: 624 \/ 356;\" width=\"624\" autoplay controls loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/blackhole-1.mp4\"><\/video><figcaption class=\"wp-element-caption\">Black hole effect<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Start by adding the configurations using the <code>particles<\/code> option. We will make the particles:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Be a circle<\/li>\n\n\n\n<li>Be semi-transparent (<code>opacity<\/code>)&nbsp;<\/li>\n\n\n\n<li>Have random colors and sizes within a set range<\/li>\n\n\n\n<li>Bounce when they hit the side of the page&nbsp;<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 \u00a0 <span class=\"hljs-attr\">background<\/span>: {\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">\"#fff\"<\/span>,\n\u00a0 \u00a0 },\n\u00a0 \u00a0 <span class=\"hljs-attr\">particles<\/span>: {\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">shape<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">\"circle\"<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">number<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-number\">0<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">color<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">\"random\"<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">opacity<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-number\">0.3<\/span>\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">size<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: {<span class=\"hljs-attr\">min<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-attr\">max<\/span>: <span class=\"hljs-number\">20<\/span>}\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">move<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">enable<\/span>: <span class=\"hljs-literal\">true<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">speed<\/span>: <span class=\"hljs-number\">6<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">random<\/span>: <span class=\"hljs-literal\">false<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">outModes<\/span>: <span class=\"hljs-string\">\"bounce\"<\/span>,\n\u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 },\n\u00a0 };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since we have set <code>number.value<\/code> to 0, no particle will be displayed on the screen. This is exactly what we want since we want all particles to come from the emitter. Now let\u2019s add the <code>emitters<\/code> option.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 <span class=\"hljs-comment\">\/\/ \u2026<\/span>\n\u00a0 \u00a0 <span class=\"hljs-attr\">emitters<\/span>: &#91;\n\u00a0 \u00a0 \u00a0 {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">direction<\/span>: <span class=\"hljs-string\">\"top-right\"<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">position<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-number\">0<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">100<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">rate<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">delay<\/span>: <span class=\"hljs-number\">0.3<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">quantity<\/span>: <span class=\"hljs-number\">4<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 ]\n\u00a0 };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The above will create an emitter position at the bottom left of the screen which releases particles to the top-right direction at the rate of 4 particles every 0.4 seconds.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"324\" style=\"aspect-ratio: 554 \/ 324;\" width=\"554\" autoplay controls loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/emitter.mp4\"><\/video><figcaption class=\"wp-element-caption\">Emitter in action<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>If we wanted, we could easily create another emitter by adding another object within the <code>emitters<\/code> array property. Particles emitted by the added emitters will be the same as the first since they are both working with the same <code>particles<\/code> option. To change the emitted particles, we can define a <code>particles<\/code> property within the new emitter object.<\/p>\n\n\n\n<p>Now to add an absorber at the middle of the screen with a size that changes as particles are absorbed, add the following configuration within the options object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n   <span class=\"hljs-comment\">\/\/ \u2026<\/span>\n    <span class=\"hljs-attr\">absorbers<\/span>: &#91;\n      {\n        <span class=\"hljs-attr\">position<\/span>: { <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">50<\/span> },\n        <span class=\"hljs-attr\">size<\/span>: {\n          <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-number\">50<\/span>,\n          <span class=\"hljs-attr\">limit<\/span>: <span class=\"hljs-number\">100<\/span>,\n        }\n      }\n    ],\n  };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With this the intended particle animation has been achieved. We can view it and its configurations below:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=229896&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Make the most of presets<\/h2>\n\n\n\n<p>Rather than creating a particle animation from scratch, we can use the official presets or use the configurations from the available demos and then do some fine-tuning to achieve our desired effect.<\/p>\n\n\n\n<p>To show how we can use the <a href=\"https:\/\/github.com\/matteobruni\/tsparticles#presets\" target=\"_blank\" rel=\"noreferrer noopener\">official presets<\/a>, we\u2019ll be using the confetti preset.&nbsp;<\/p>\n\n\n\n<p>First, let&#8217;s install the <a href=\"https:\/\/www.npmjs.com\/package\/tsparticles-preset-confetti\" target=\"_blank\" rel=\"noreferrer noopener\">preset package<\/a> using the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">npm i tsparticles-preset-confetti<\/code><\/span><\/pre>\n\n\n<p>Now to load the preset, we need to modify the <strong>src\/App.js<\/strong> file to look like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> Particles <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-tsparticles\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { loadFull } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"tsparticles\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { useCallback } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { loadConfettiPreset } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"tsparticles-preset-confetti\"<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\u00a0 <span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 \u00a0 <span class=\"hljs-attr\">preset<\/span>: <span class=\"hljs-string\">\"confetti\"<\/span>,\n\u00a0 };\n\u00a0\n\u00a0 <span class=\"hljs-keyword\">const<\/span> particlesInit = useCallback(<span class=\"hljs-keyword\">async<\/span> (engine) =&gt; {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">await<\/span> loadConfettiPreset(engine)\n\u00a0 \u00a0 <span class=\"hljs-keyword\">await<\/span> loadFull(engine)\n\u00a0 }, &#91;]);\n\n\u00a0 <span class=\"hljs-keyword\">return<\/span> (\n\u00a0 \u00a0 <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"App\"<\/span>&gt;<\/span>\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Particles<\/span> <span class=\"hljs-attr\">options<\/span>=<span class=\"hljs-string\">{options}<\/span> <span class=\"hljs-attr\">init<\/span>=<span class=\"hljs-string\">{particlesInit}<\/span> \/&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n\u00a0 );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Above in <code>options<\/code> , we specified the preset by its name using the <code>preset<\/code> option. Then we loaded in the callback function passed to the <code>init<\/code> prop. Below is the result:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=229900&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>To customize the preset like changing the background color, increasing the number of particles emitted, changing the particle shape, etc, we can just add other options to the options object.&nbsp;<\/p>\n\n\n\n<p>For example, to change the color of the confetti, here is what the configuration options look like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 \u00a0 <span class=\"hljs-attr\">particles<\/span>: {\n\u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">color<\/span>: {\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-attr\">value<\/span>: &#91;<span class=\"hljs-string\">\"#0000ff\"<\/span>, <span class=\"hljs-string\">\"#00ff00\"<\/span>],\n\u00a0 \u00a0 \u00a0 },\n\u00a0 \u00a0 },\n\u00a0 \u00a0 <span class=\"hljs-attr\">preset<\/span>: <span class=\"hljs-string\">\"confetti\"<\/span>,\n\u00a0 };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Explore other customization plugins&nbsp;<\/h2>\n\n\n\n<p>tsParticles allows even more advanced customization through plugins, which can be used to <a href=\"https:\/\/particles.js.org\/docs\/modules\/Core_Interfaces_IPlugin.html#creating-a-custom-shape\" target=\"_blank\" rel=\"noreferrer noopener\">create custom shapes<\/a> and <a href=\"https:\/\/particles.js.org\/docs\/modules\/Core_Interfaces_IPlugin.html#creating-a-custom-preset\" target=\"_blank\" rel=\"noreferrer noopener\">presets<\/a> using the <code>addShape<\/code> and <code>addPreset<\/code> method respectively.<\/p>\n\n\n\n<p>Let\u2019s look at how we can use the <code>addShape<\/code> method to create a pentagon and use it to replace the circles emitted in the black hole example above.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> loadPentagonShape = <span class=\"hljs-function\">(<span class=\"hljs-params\">engine<\/span>) =&gt;<\/span> {\n\u00a0 engine.addShape(<span class=\"hljs-string\">\"pentagon\"<\/span>, (context, particle, radius) =&gt; {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">const<\/span> angle = (<span class=\"hljs-number\">2<\/span> * <span class=\"hljs-built_in\">Math<\/span>.PI) \/ <span class=\"hljs-number\">6<\/span>;\n\u00a0 \u00a0 context.beginPath();\n\u00a0 \u00a0 <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">let<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; <span class=\"hljs-number\">5<\/span>; i++) {\n\u00a0 \u00a0 \u00a0 context.lineTo(\n\u00a0 \u00a0 \u00a0 \u00a0 radius * <span class=\"hljs-built_in\">Math<\/span>.cos(angle * i),\n\u00a0 \u00a0 \u00a0 \u00a0 radius * <span class=\"hljs-built_in\">Math<\/span>.sin(angle * i)\n\u00a0 \u00a0 \u00a0 );\n\u00a0 \u00a0 }\n\u00a0 \u00a0 context.closePath();\n\u00a0 \u00a0 context.stroke();\n\u00a0 });\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>addShape<\/code> method accepts multiple arguments, but the most important is the shape name and the draw function callback..&nbsp;<\/p>\n\n\n\n<p>The draw function callback helps you create your desired shape, and we\u2019ve passed multiple arguments to it to help us tweak the shape that is being drawn.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>context<\/code> is the HTML canvas context&nbsp;<\/li>\n\n\n\n<li><code>particle<\/code> contains the value of the properties passed in the <code>particles<\/code> options, discussed above&nbsp;<\/li>\n\n\n\n<li><code>radius<\/code> , as the name implies, is the radius of the shape.<\/li>\n<\/ul>\n\n\n\n<p>Now, to make the pentagon shape available for use, you need to call it before the <code>loadFull<\/code> function is called and our tsParticles are initialized, like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> particlesInit = useCallback(<span class=\"hljs-keyword\">async<\/span> (engine) =&gt; {\n\u00a0 \u00a0 loadPentagonShape(engine)\n\u00a0 \u00a0 <span class=\"hljs-keyword\">await<\/span> loadFull(engine)\n\u00a0 }, &#91;]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With this, you can start using this shape by passing its name as a value to <code>shape.type<\/code> in the <code>particles<\/code> option. Below is the result of using this shape in the black hole example:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=229907&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>As mentioned earlier, tsParticles also allows us to create our own presets using the <code>addPreset<\/code> method, which takes in the preset name and configuration options as arguments. In the same way we created a custom shape, we also need to create a load which will be called to make the preset available for use.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> options = {\n\u00a0 <span class=\"hljs-comment\">\/\/ preset options<\/span>\n};\n\n<span class=\"hljs-keyword\">const<\/span> loadCustomPreset = <span class=\"hljs-function\">(<span class=\"hljs-params\">engine<\/span>) =&gt;<\/span> {\n\u00a0 engine.addPreset(<span class=\"hljs-string\">\"customPreset\"<\/span>, options);\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Using a custom preset is exactly the same as using an official one, where you need to call the preset before initializing tsParticles and specify the preset name in the <code>preset<\/code> option of the particle configurations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Play with tsParticles in the CoderPad sandbox<\/h2>\n\n\n\n<p>tsParticles makes it easier than ever before to create and customize particle animations for your website. In my view, it\u2019s pretty great!&nbsp; Try implementing some of the tricks you learned today in the <a href=\"https:\/\/app.coderpad.io\/sandbox\" target=\"_blank\" rel=\"noreferrer noopener\">CoderPad sandbox<\/a>.&nbsp;<\/p>\n\n\n\n<p><em>Taminoturoko Briggs is an enthusiastic software developer and technical writer. Core languages include JavaScript and Python.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interactive animations made with the tsParticles library add more spice to a UI application. In this article, you will learn how you can create interactive animated backgrounds in React with tsParticles.<\/p>\n<p>Prepare to have your mind blown with this fantastic animation library.<\/p>\n","protected":false},"author":12,"featured_media":20452,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[61,41],"keyword-cluster":[],"class_list":["post-20405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=20405"}],"version-history":[{"count":68,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20405\/revisions"}],"predecessor-version":[{"id":40876,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/20405\/revisions\/40876"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/20452"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=20405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=20405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=20405"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=20405"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=20405"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=20405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}