Animated Website Background with HTML5

Animated Website Background with HTML5
Code Snippet:Canvas Animation
Author: Asadabbas
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 5,939
License: MIT
Edit Code online: View on CodePen
Read More

This lightweight JavaScript code snippet helps you to create an animated background for website with HTML5 canvas. It uses requestAnimationFrame function to generate random bubbles with bounce animation to make an attractive background.

How to Create Animated Website Background with HTML5

1. First of all, create the HTML structure as follows:

 <div class="container py-5 text-white">
 <div class="row justify-content-center text-center">
  <div class="col-12">
   <h1 class="pb-3 h5">Number counter animation <br> in pure JavaScript <br> (requestAnimationFrame) </h1>
  </div>
  <div class="col-md-3">
   <span id="count1" class="display-4"></span>
  </div>
  <div class="col-md-3">
   <span id="count2" class="display-4"></span>
  </div>
  <div class="col-md-3">
   <span id="count3" class="display-4"></span>
  </div>
  <div class="col-md-3">
   <span id="count4" class="display-4"></span>
  </div>
 </div>
</div>

2. After that, add the following CSS styles to your project:

/* I'm too lazy to write CSS 🐌 ... other than that Bootstrap has nothing to do with this example.  */
body {
 background-color: #000;
}

main{
   background: #8E2DE2;  /* fallback for old browsers */
   background: -webkit-linear-gradient(to right, #4A00E0, #8E2DE2);  /* Chrome 10-25, Safari 5.1-6 */
   background: linear-gradient(to right, #4A00E0, #8E2DE2); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

3. Finally, add the following JavaScript code and done.

//#region - start of - number counter animation
const counterAnim = (qSelector, start = 0, end, duration = 1000) => {
 const target = document.querySelector(qSelector);
 let startTimestamp = null;
 const step = (timestamp) => {
  if (!startTimestamp) startTimestamp = timestamp;
  const progress = Math.min((timestamp - startTimestamp) / duration, 1);
  target.innerText = Math.floor(progress * (end - start) + start);
  if (progress < 1) {
   window.requestAnimationFrame(step);
  }
 };
 window.requestAnimationFrame(step);
};
//#endregion - end of - number counter animation

document.addEventListener("DOMContentLoaded", () => {
 counterAnim("#count1", 10, 300, 1000);
 counterAnim("#count2", 5000, 250, 1500);
 counterAnim("#count3", -1000, -150, 2000);
 counterAnim("#count4", 500, -100, 2500);
});

That’s all! hopefully, you have successfully integrated this animated website background code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

2 thoughts on “Animated Website Background with HTML5”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X