Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

faster way to scatter multiple image items html

I'm new to animating and im currently trying to achieve this look below: however because I want to animate these stars to wiggle when you hover over them and I figured out I need to manually insert each star rather than just inserting this whole picture. Is there a faster/more efficient way to go about this? Should I write an insert js function for this? Thanks! enter image description here

like image 394
helloworld Avatar asked Oct 14 '25 15:10

helloworld


1 Answers

You probably need Particles.js

A lightweight JavaScript library for creating particles.


The usage is simple.

  1. Load the library from a CDN or locally. I used

    <script src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>

  2. create a container for the particles with id="particles-js"

  3. style it like so:

    #particles-js { width: 100vw; height: 100vh; overflow: hidden; }

  4. Go to the official page and set whatever settings you want

  5. export settings as json

  6. Add json contents in a <script> tag in your document.

Responsive interactive demo:

particlesJS("particles-js", {
  "particles": {
    "number": {
      "value": 200,
      "density": {
        "enable": true,
        "value_area": 800
      }
    },
    "color": {
      "value": "#ffffff"
    },
    "shape": {
      "type": "star",
      "stroke": {
        "width": 0,
        "color": "#000000"
      },
      "polygon": {
        "nb_sides": 5
      },
      "image": {
        "src": "img/github.svg",
        "width": 35,
        "height": 35
      }
    },
    "opacity": {
      "value": 0.35,
      "random": true,
      "anim": {
        "enable": true,
        "speed": .5,
        "opacity_min": 0.1,
        "sync": false
      }
    },
    "size": {
      "value": 4,
      "random": true,
      "anim": {
        "enable": false,
        "speed": 40,
        "size_min": 0.1,
        "sync": false
      }
    },
    "line_linked": {
      "enable": false,
      "distance": 150,
      "color": "#ffffff",
      "opacity": 0.1,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": .5,
      "direction": "none",
      "random": true,
      "straight": false,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": true,
        "rotateX": 4024.6529723245903,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": true,
        "mode": "bubble"
      },
      "onclick": {
        "enable": true,
        "mode": "repulse"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 400,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 150,
        "size": 5,
        "duration": 8,
        "opacity": .2,
        "speed": 3
      },
      "repulse": {
        "distance": 100,
        "duration": 5
      },
      "push": {
        "particles_nb": 2
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true

});
* {
  margin: 0;
  padding: 0;
}

#particles-js {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.stars,
.twinkle {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: -10;
}

.stars {
  background: #000 url(https://i.sstatic.net/SyeSl.png) repeat top center;
  z-index: -1;
}

.twinkle {
  background: transparent url(https://i.sstatic.net/q9Sz6.png) repeat top center;
  z-index: -1;
  animation: stars 1000s linear infinite;
}

@keyframes stars {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/particles.js/2.0.0/particles.min.js"></script>

<div id="particles-js"></div>
<div class="stars"></div>
<div class="twinkle"></div>

If prefer something non-interactive as that probably has much better overall performance a much simpler version can be achieved Like so:

* {
  margin: 0;
  padding: 0;
}

.stars,
.twinkle {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: -10;
}

.stars {
  background: #000 url(https://i.sstatic.net/SyeSl.png) repeat top center;
  z-index: -1;
}

.twinkle {
  background: transparent url(https://i.sstatic.net/q9Sz6.png) repeat top center;
  z-index: -1;
  animation: stars 1000s linear infinite;
}

@keyframes stars {
  from {
    background-position: 0 0;
  }
  to {
    background-position: -10000px 5000px;
  }
}
<div class="stars"></div>
<div class="twinkle"></div>

Note: Small stars in the background shamelessly stolen from a pen by Annaliza Torres

like image 79
I haz kode Avatar answered Oct 17 '25 05:10

I haz kode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!