Beginner Inquiry: Implementing an Overlay Div with Text on Top of an Animated Element

Looking to create a stacked div layout but facing some challenges due to being new at this.

// Particle Animation

!function(a){var b="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global;"function"==typeof define&&define.amd?define(["exports"],function(c){b.ParticleNetwork=a(b,c)}):"object"==typeof module&&module.exports?module.exports=a(b,{}):b.ParticleNetwork=a(b,{})}(function(a,b){var c=function(a){this.canvas=a.canvas,this.g=a.g,this.particleColor=a.options.particleColor,this.x=Math.random()*this.canvas.width,this.y=Math.random()*this.canvas.height,this.velocity={x:(Math.random()-.5)*a.options.velocity,y:(Math.random()-.5)*a.options.velocity}};return c.prototype.update=function(){(this.x>this.canvas.width+20||this.x<-20)&&(this.velocity.x=-this.velocity.x),(this.y>this.canvas.height+20||this.y<-20)&&(this.velocity.y=-this.velocity.y),this.x+=this.velocity.x,this.y+=this.velocity.y},c.prototype.h=function(){this.g.beginPath(),this.g.fillStyle=this.particleColor,this.g.globalAlpha=.7,this.g.arc(this.x,this.y,1.5,0,2*Math.PI),this.g.fill()},b=function(a,b){this.i=a,this.i.size={width:this.i.offsetWidth,height:this.i.offsetHeight},b=void 0!==b?b:{},this.options={particleColor:void 0!==b.particleColor?b.particleColor:"#fff",background:void 0!==b.background?b.background:"#1a252f",interactive:void 0!==b.interactive?b.interactive:!0,velocity:this.setVelocity(b.speed),density:this.j(b.density)},this.init()},b.prototype.init=function(){if(this.k=document.createElement("div"),this.i.appendChild(this.k),this.l(this.k,{position:"absolute",top:0,left:0,bottom:0,right:0,"z-index":1}),/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(this.options.background))this.l(this.k,{background:this.options.background});else{if(!/\.(gif|jpg|jpeg|tiff|png)$/i.test(this.options.background))return console.error("Please specify a valid background image or hexadecimal color"),!1;this.l(this.k,{background:'url("'+this.options.background+'") no-repeat center',"background-size":"cover"})}if(!/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(this.options.particleColor))return console.error("Please specify a valid particleColor hexadecimal color"),!1;this.canvas=document.createElement("canvas"),this.i.appendChild(this.canvas),this.g=this.canvas.getContext("2d"),this.canvas.width=this.i.size.width,this.canvas.height=this.i.size.height,this.l(this.i,{position:"relative"}),this.l(this.canvas,{"z-index":"20",position:"relative"}),window.addEventListener("resize",function(){return this.i.offsetWidth===this.i.size.width&&this.i.offsetHeight===this.i.size.height?!1:(this.canvas.width=this.i.size.width=this.i.offsetWidth,this.canvas.height=this.i.size.height=this.i.offsetHeight,clearTimeout(this.m),void(this.m=setTimeout(function(){this.o=[];for(var a=0;a<this.canvas.width*this.canvas.height/this.options.density;a++)this.o.push(new c(this));this.options.interactive&&this.o.push(this.p),requestAnimationFrame(this.update.bind(this))}.bind(this),500)))}.bind(this)),this.o=[];for(var a=0;a<this.canvas.width*this.canvas.height/this.options.density;a++)this.o.push(new c(this));this.options.interactive&&(this.p=new c(this),this.p.velocity={x:0,y:0},this.o.push(this.p),this.canvas.addEventListener("mousemove",function(a){this.p.x=a.clientX-this.canvas.offsetLeft,this.p.y=a.clientY-this.canvas.offsetTop}.bind(this)),this.canvas.addEventListener("mouseup",function(a){this.p.velocity={x:(Math.random()-.5)*this.options.velocity,y:(Math.random()-.5)*this.options.velocity},this.p=new c(this),this.p.velocity={x:0,y:0},this.o.push(this.p)}.bind(this))),requestAnimationFrame(this.update.bind(this))},b.prototype.update=function(){this.g.clearRect(0,0,this.canvas.width,this.canvas.height),this.g.globalAlpha=1;for(var a=0;a<this.o.length;a++){this.o[a].update(),this.o[a].h();for(var b=this.o.length-1;b>a;b--){var c=Math.sqrt(Math.pow(this.o[a].x-this.o[b].x,2)+Math.pow(this.o[a].y-this.o[b].y,2));c>120||(this.g.beginPath(),this.g.strokeStyle=this.options.particleColor,this.g.globalAlpha=(120-c)/120,this.g.lineWidth=.7,this.g.moveTo(this.o[a].x,this.o[a].y),this.g.lineTo(this.o[b].x,this.o[b].y),this.g.stroke())}}0!==this.options.velocity&&requestAnimationFrame(this.update.bind(this))},b.prototype.setVelocity=function(a){return"fast"===a?1:"slow"===a?.33:"none"===a?0:.66},b.prototype.j=function(a){return"high"===a?5e3:"low"===a?2e4:isNaN(parseInt(a,10))?1e4:a},b.prototype.l=function(a,b){for(var c in b)a.style[c]=b[c]},b});

// Initialization

var canvasDiv = document.getElementById('particle-canvas');
var options = {
  particleColor: '#888',
  background: 'https://raw.githubusercontent.com/JulianLaval/canvas-particle-network/master/img/demo-bg.jpg',
  interactive: true,
  speed: 'medium',
  density: 'high'
};
var particleCanvas = new ParticleNetwork(canvasDiv, options);
html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
#particle-canvas {
  width: 100%;
  height: 100%;
}
h1 {
    text-align: center;
    position: inherit;
    float: none;
}
#container {
    width: 100px;
    height: 100px;
}
#back,
.overstyle {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 50%;
}
.overstyle h1{
    font-family: 'Open Sans Condensed', sans-serif;
    font-size: 230%;
    font-family: 'Indie Flower', cursive;
}
.overstyle {
    text-align: center;
    color: white;
    z-index: 100;
}
<body>
        <div id="container" style="width: 100%; height: 100%;">
            <div id="particle-canvas" id="back"></div>
            <script src='https://rawgit.com/JulianLaval/canvas-particle-network/master/particle-network.min.js'></script>
            <script  src="js/index.js"></script>
            <!--- Content Div --->
            <div style="width: 100%; color: rgba(255, 255, 255, 0.55);" class="overstyle">
                <h1>TestFont</h1>
            </div>
            <!--- End of Content Div --->
        </div>
    </body>

The div has an id="particle-canavas". For more details, you can check it out here.

You can find my CSS file here. Any suggestions on how I can enhance the "overstyle" overlay div?

Your assistance is greatly appreciated.

Answer №1

This centers as you need. When in need of centering something, I always utilize the following CSS:

width:100%;
margin-left:auto;
margin-right:auto;
// Code for particle.min.js hosted on GitHub
// Scroll down for initialization code

! function(a) {
    var b = "object" == typeof self && self.self === self && self || "object" == typeof global && global.global === global && global;
    "function" == typeof define && define.amd ? define(["exports"], function(c) {
        b.ParticleNetwork = a(b, c)
    }) : "object" == typeof module && module.exports ? module.exports = a(b, {}) : b.ParticleNetwork = a(b, {})
}(function(a, b) {
    // Function to create particles
    var c = function(a) {
        this.canvas = a.canvas, this.g = a.g, this.particleColor = a.options.particleColor, this.x = Math.random() * this.canvas.width, this.y = Math.random() * this.canvas.height, this.velocity = {
            x: (Math.random() - .5) * a.options.velocity,
            y: (Math.random() - .5) * a.options.velocity
        }
    };
    return c.prototype.update = function() {
        (this.x > this.canvas.width + 20 || this.x < -20) && (this.velocity.x = -this.velocity.x), (this.y > this.canvas.height + 20 || this.y < -20) && (this.velocity.y = -this.velocity.y), this.x += this.velocity.x, this.y += this.velocity.y
    }, c.prototype.h = function() {
        this.g.beginPath(), this.g.fillStyle = this.particleColor, this.g.globalAlpha = .7, this.g.arc(this.x, this.y, 1.5, 0, 2 * Math.PI), this.g.fill()
    }, b = function(a, b) {
        this.i = a, this.i.size = {
            width: this.i.offsetWidth,
            height: this.i.offsetHeight
        }, b = void 0 !== b ? b : {}, this.options = {
            particleColor: void 0 !== b.particleColor ? b.particleColor : "#fff",
            background: void 0 !== b.background ? b.background : "#1a252f",
            interactive: void 0 !== b.interactive ? b.interactive : !0,
            velocity: this.setVelocity(b.speed),
            density: this.j(b.density)
        }, this.init()
    }, b.prototype.init = function() {
        // Initialization logic here
    }, b.prototype.update = function() {
        // Update function logic here
    }, b.prototype.setVelocity = function(a) {
        // Set velocity logic here
    }, b.prototype.j = function(a) {
        // Density calculation logic here
    }, b.prototype.l = function(a, b) {
        // Apply styles logic here
    }

    // Initialisation logic
});

// Initialisation

var canvasDiv = document.getElementById('particle-canvas');
var options = {
    particleColor: '#888',
    background: 'https://raw.githubusercontent.com/JulianLaval/canvas-particle-network/master/img/demo-bg.jpg',
    interactive: true,
    speed: 'medium',
    density: 'high'
};
var particleCanvas = new ParticleNetwork(canvasDiv, options);
CSS Style code goes here
Your customized HTML code snippet here

Answer №2

After implementing the solution on the sample you shared, I found success:

.overstyle h1 {
  z-index:2;
  color: #fff;
  position: absolute;
  top: 20px;
  left: 20px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you please guide me on determining the type of the root element using jQuery?

I have an ajax request that can return either an error code or a user ID <?xml version="1.0" encoding="UTF-8"?> <error>1000</error> <?xml version="1.0" encoding="UTF-8"?> <userID>8</userID> Is there a way to determine ...

Establish map boundaries using the longitude and latitude of multiple markers

Utilizing Vue, I have integrated the vue-mapbox component from this location: I've made sure to update the js and css to the latest versions and added them to the index.html: <!-- Mapbox GL CSS --> <link href="https://api.tiles.mapbox.com/m ...

Tips for aligning an element in the center in relation to its sibling above

Help Needed with Centering Form Button https://i.sstatic.net/QhWqi.png I am struggling to center the "Send" button below the textarea in the form shown in the image. Despite trying various methods like using position absolute, relative, and custom margin ...

Picture leading to the expansion of a table cell's height

I despise CSS more than anything. My table header with multiple inline table cells was working flawlessly until I attempted to add an image to one of the cells, causing the div's height to inexplicably expand. Here's an example of the blank tab ...

Show individual JSON values from a Python response on a webpage using HTML

I have created a webapp using Flask for OCR on documents with layoutlm. The website features an upload system that accepts images and documents, which are then processed by a Python script for OCR. This script returns a JSON response containing field names ...

The form validation feature is not functioning as expected when integrating mui-places-autocomplete with MUI React

I'm currently working on implementing an autocomplete feature using Google Places API in my project with Material UI React, Redux-Form, Revalidate, and MUI-Places-Autocomplete. Although I've successfully integrated the place lookup functionality, ...

What steps can I take to modify this script in order to display one image on top of another?

I am working on a script that creates cross fades for images. However, I have some text images that I would like to appear as fading in on each image transition. This means that when the first image appears, it will pause, then the text image will fade i ...

What is the reason behind capitalizing all words within <p> elements?

Why are all the words in my sentences capitalized randomly? Below you can see snippets of my HTML and CSS code. Any help would be greatly appreciated. Thank you in advance! HTML <!DOCTYPE html> <html lang="en"> <head> & ...

Discovering the width of desktop and mobile browsers to ensure maximum compatibility

While working on media queries, I encountered this code: //Checking various widths as different browsers report them differently if ($(window).width()!==0) { width = $(window).width(); } else if (window.innerWidth!==0) { width = window.inner ...

Unable to get the active class to work in Bootstrap 4 navbar

I'm struggling to make the active class in this specific navbar work on my website. I want the link to turn blue when I click on the current page. Being new to web development, I would really appreciate any help. Thank you in advance. This is the nav ...

Combining and consolidating JSON attributes

I am working with a JSON object that has multiple instances of the email property, like so: accounts": [ { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="61120e0c04030e051821050e0c">[email protected]</a& ...

What causes the fading effect on my navigation bar when the slideshow is in use?

I recently created a navigation bar at the top of my webpage, placed above a picture slideshow. However, I noticed that as the slideshow transitions between images, the background color of the navigation bar fades to transparent. How can I prevent this and ...

Unlocking nested JSON data in React applications

Encountering an issue while retrieving a nested JSON object from a local API endpoint /user/{id}. Here is an example of the JSON object: {"userId":122,"name":"kissa","email":"<a href="/cdn-cgi/l/email-protect ...

Using JavaFX to modify properties with multiple selectors

I've set up a stylesheet with the following styles: * { -fx-font-size: 15px; } .title { -fx-font-size: 20px; } I assumed that since * is a more general selector than .title, the font size specified in .title would take precedence over it. Ho ...

Using an array as the source for your Mui Autocomplete instead of JSON

I'm attempting to recreate the autocomplete MUI example shown here, but with a slight variation. Instead of utilizing a JSON data structure, I am simply passing an Array. Here is what I have tried: export default function SearchTutorialArray() { con ...

Customize your Bootstrap 4 navbar to break and float on the right side for a

Is there a way to achieve a similar layout using Bootstrap 4, with centered menu items in the second row like this: https://i.sstatic.net/l1EOx.gif I successfully created this design with Bootstrap 3, but I'm struggling to replicate it with Bootstra ...

What is causing my Next.js server action to be mistaken for a client-side function?

I'm currently working on implementing infinite scrolling for the NextJS gallery template project by following this specific tutorial. The server action script I am using is as follows: // /actions/getImages.ts 'use server' import { promise ...

JavaScript's forEach function is utilized for handling button events

Just getting started with JavaScript and I've run into a problem. Can anyone help me spot where I went wrong below? The button doesn't seem to be functioning as expected. <!DOCTYPE html> <html> <body> <button onclick =" ...

Ways to modify the end result using callback information

In my scenario, I am working with an object that has keys id and name as shown below. The goal is to retrieve the customer name based on the id and then update the object accordingly. Const arr=[{id:1,name:''},{id:2,name:''}]; ...

How to keep text always locked to the front layer in fabric.js without constantly bringing it to the front

Is it possible to achieve this functionality without using the following methods? canvas.sendBackwards(myObject) canvas.sendToBack(myObject) I am looking to upload multiple images while allowing them to be arranged forward and backward relative to each o ...