The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.
The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.
Below is the html code for this video.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fireworks Typing Effect</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<input type="text" id="textField" autocomplete="off" placeholder="Type Something..." spellcheck="false">
<script src="index.js"></script>
</body>
</html>
Below is the css code for this video.
style.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
border: none;
}
:root {
font-size: calc(20px + (60 - 20) * (100vw - 320px) / (1280 - 320));
}
body, input {
color: #eef9ff;
font: 1em/1.5 monospace;
}
body {
display: grid;
height: 100vh;
width: 100%;
background: #003153;
place-items: center;
}
input {
width: 24ch;
background: transparent;
box-shadow: 0 -1px 0 #eef9ff40 inset;
transition: box-shadow 0.51s linear;
appearance: none;
}
input:focus {
box-shadow: 0 -1px 0 #eef9ff inset;
outline: none;
}
input::placeholder {
color: #eef9ff40;
}
.particle {
position: fixed;
height: 1.5ch;
width: 1.5ch;
border-radius: 50%;
pointer-events: none;
}
.particles-ring {
box-shadow: 0 0 0 0.125em inset;
}
Below is the javascript code for this video.
index.js
window.addEventListener("DOMContentLoaded", () => new FireworkTyping("#textField"));
class FireworkTyping {
particles = 24;
value = "";
constructor(qs) {
this.input = document.querySelector(qs);
this.input?.addEventListener("input", this.fireworks.bind(this));
}
fireworks() {
const { selectionStart, value } = this.input;
const { top, left, height } = this.input.getBoundingClientRect();
const charDifference = value.length - this.value.length;
const charDifferenceAbs = Math.abs(charDifference);
let currentPosition = selectionStart || 0;
if (charDifference < 0) currentPosition += charDifferenceAbs;
for (let i = 0; i < charDifferenceAbs; ++i) {
const hue = Utils.random(0, 359, true);
for (let j = 0; j < this.particles; ++j) {
const el = document.createElement("div");
const color = `hsl(${hue}, 90%, 50%)`;
const x = `calc(${left}px + ${(currentPosition <= 24 ? currentPosition : 24) - i - 0.5}ch)`;
const y = `${top + height / 2}px`;
const angle = Utils.random(0, 359, true);
const isRing = j === 0;
const d = isRing ? Utils.random(3, 5) : Utils.random(2, 4);
el.classList.add("particle");
if (isRing) {
el.classList.add("particles-ring");
el.style.color = color;
el.style.height = `${d}em`;
el.style.width = `${d}em`;
} else { el.style.backgroundColor = color; }
el.style.top = y;
el.style.left = x;
document.body.appendChild(el);
const center = "translate(-50%, -50%)";
const ringKeyFrames = [
{ opacity: 1, transform: `${center} scale(0)` },
{ opacity: 0, transform: `${center} scale(1)` }
];
const particleKeyFrames = [
{ transform: `${center} rotate(${angle}deg) translateY(0) scale(1)` },
{ transform: `${center} rotate(${angle}deg) translateY(${d}em) scale(0)` }
];
const movement = el.animate(isRing ? ringKeyFrames : particleKeyFrames, {
duration: isRing ? 600 : 960, easing: "cubic-bezier(0, 0, 0.15, 1)"
});
movement.onfinish = () => el.remove();
}
}
this.value = value;
}
}
class Utils {
static random(min, max, round = false) {
const percent = crypto.getRandomValues(new Uint32Array(1))[0] / 2 ** 32;
const relativeValue = (max - min) * percent;
return (min + (round === true ? Math.round(relativeValue) : +relativeValue.toFixed(2)));
}
}
Thanks for visiting