HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="container">
<!-- <img src="https://1-notes.com/images/ishidatami.jpg" /> -->
<!-- <p>JavaScript | Falling Letters Animation</p> -->
</div>
<p><a href="https://1-notes.com/javascript-falling-letters-animation/" target="_blank">JavaScript | Falling Letters Animation</a></p>
body {
margin: 0;
}
p {
margin: 20px;
}
#container {
width: 100%;
aspect-ratio: 3 / 1;
background-color: #000;
}
#container img {
width: 100%;
object-fit: cover;
aspect-ratio: 3 / 1;
}
#container p {
position: absolute;
left: 20px;
top: 20px;
font-size: 40px;
color: #ffb6c1;
font-weight: bold;
}
// drop letters animation function
const fallingLettersAnim = (option) => {
// default option
let default_option = {
target_element: 'body', // taget HTML element
where_to_insert: 'img', // Insert after the specified element in target_element. If unspecified, insert at the end of the target_element. ('img' 'p' '#id' etc)
letters_amount: 5, // Amount of letters (min 1 max 10)
letters_speed: 5, // Speed of letters (min 1 max 10)
letters_size: 1.0, // Size of letters (min 0.1)
letters_type: 'Aऄก', // select letter (aA1ऄกあア)
letters_angle: true, // Random default angle / affect rotation mode
letters_rotate_mode: 1, //Rotation mode (0=false 1=X 2=Y 3=XY)
letters_rotate_deg: 180, // (0 = false 360 = 360deg)
letters_rotate_reverse: true, // reverse rotation
letters_color: 'random', // Color of letters (hex, rgba, name, random)
letters_neon_light: true, // Emission of light from letters.
letters_neon_color: 'random', // Color of letters (hex, rgba, name, random)
animation_time: 600 // Animation time (s)
};
// merge option
let op = Object.assign(default_option, option);
// whether the target element exists
if (!document.querySelector(op.target_element)) {
console.log('no target element.');
return;
}
// target element
let target_element = document.querySelector(op.target_element);
target_element.style.position = 'relative';
target_element.style.overflow = 'hidden';
// Insert after the specified element
let insert_after_element = '';
if (op.where_to_insert != '') {
insert_after_element = target_element.querySelector(op.where_to_insert);
}
// main container
let container = document.createElement('div');
if (!insert_after_element) {
target_element.appendChild(container);
} else {
insert_after_element.after(container);
}
container.style.position = 'absolute';
container.style.top = 0;
container.style.left = 0;
container.style.width = '100%';
container.style.height = '100%';
container.style.overflow = 'hidden';
// letters container
let letters_container = document.createElement('div');
container.appendChild(letters_container);
letters_container.style.position = 'absolute';
letters_container.style.width = '100%';
letters_container.style.height = '100%';
// letter clone
let letter = document.createElement('div');
letter.style.position = 'absolute';
letter.style.opacity = 1;
letter.style.fontFamily = '';
// letters
let chars = '';
if (op.letters_type.includes('a')) {
chars += 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
}
if (op.letters_type.includes('A')) {
chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
if (op.letters_type.includes('1')) {
chars += '012345678901234567890123456789012345678901234567890123456789';
}
if (op.letters_type.includes('ऄ')) {
chars += 'ऄअआइईउऊऋऌऍऎएऐऑऒओऔकखगघङचछजझञटठडढणतथदधनऩपफबभमयरऱलळऴवशषसह';
}
if (op.letters_type.includes('ก')) {
chars += 'กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ';
}
if (op.letters_type.includes('あ')) {
for (let i = 12353; i < 12435; i++) {
chars += String.fromCharCode(i);
}
}
if (op.letters_type.includes('ア')) {
for (let i = 12439; i < 12538; i++) {
chars += String.fromCharCode(i);
}
}
let colors = [];
if (op.letters_color == 'random' || op.letters_neon_color == 'random') {
for (let i = 0; i < 110; i++) {
colors.push('#' + Math.floor(Math.random() * 16777215).toString(16));
}
}
;
let letters = '';
for (var i = 0; i < 500; i++) {
letters += chars.charAt(Math.floor(Math.random() * chars.length));
}
let no = 0;
let count = 0;
op.animation_time *= 60;
const update = () => {
let rand1 = Math.floor(Math.random() * 110);
let rand2 = Math.floor(Math.random() * 100);
// font color
let letters_color = op.letters_color;
if (op.letters_color == 'random') {
letters_color = colors[rand1];
}
// neon color
let letters_neon_color = op.letters_neon_color;
if (op.letters_neon_color == 'random') {
letters_neon_color = colors[rand2];
}
// angle
let angle = 0;
if (op.letters_angle == true) {
angle = Math.floor(Math.random() * 360);
}
// rotate set reverse
if (rand2 % 2 == 0 && op.letters_rotate_reverse == true) {
op.letters_rotate_deg = -(op.letters_rotate_deg);
}
// rotate set
let angle_set = 0;
let rotate_angle_set = '';
if (op.letters_rotate_mode == 0) {
rotate_angle_set = `rotateX(0deg)`;
rotate_angle_set = `rotateX(0deg)`;
} else if (op.letters_rotate_mode == 1) {
angle_set = `rotate(${angle}deg)`;
rotate_angle_set = `rotateX(${angle + op.letters_rotate_deg}deg)`;
} else if (op.letters_rotate_mode == 2) {
angle_set = `rotate(${angle}deg)`;
rotate_angle_set = `rotateY(${angle + op.letters_rotate_deg}deg)`;
} else if (op.letters_rotate_mode == 3) {
angle_set = `rotate(${angle}deg)`;
rotate_angle_set = `rotate(${angle + op.letters_rotate_deg}deg)`;
}
// letter drop
if (count % (11 - op.letters_amount) == 0) {
let letter_clone = letter.cloneNode();
letter_clone.innerText = letters[no];
letter_clone.style.fontSize = `${0.1 * rand2 * op.letters_size}vw`;
letter_clone.style.left = `${rand1 - 10}%`;
letter_clone.style.color = letters_color;
letter_clone.style.transform = `rotate(${angle}deg)`;
if (op.letters_neon_light == true) {
letter_clone.style.textShadow = `
0 0 1.0em ${letters_neon_color},
0 0 0.5em ${letters_neon_color},
0 0 0.1em ${letters_neon_color}
`;
}
letters_container.appendChild(letter_clone);
let letters_anim = letter_clone.animate(
[
{ top: `-20%`, transform: angle_set },
{ top: `100%`, transform: rotate_angle_set }
],
{
fill: 'forwards',
duration: 6000 / op.letters_speed
}
);
letters_anim.onfinish = (event) => {
letter_clone.remove();
};
if (no < 499) {
no++;
} else {
no = 1;
}
}
// stop or run animation
count++;
if (op.animation_time >= count) {
requestAnimationFrame(update);
} else {
cancelAnimationFrame(update);
}
};
update();
};
//////////////////////////// call function
let option = {
target_element: '#container', // taget HTML element
where_to_insert: 'img', // Insert after the specified element in target_element. If unspecified, insert at the end of the target_element. ('img' 'p' '#id' etc)
letters_amount: 5, // Amount of letters (min 1 max 10)
letters_speed: 5, // Speed of letters (min 1 max 10)
letters_size: 1.0, // Size of letters (min 0.1)
letters_type: 'Aあऄก', // select letter (aA1ऄกあア)
letters_angle: true, // Random default angle / affect rotation mode
letters_rotate_mode: 1, //Rotation mode (0=false 1=X 2=Y 3=XY)
letters_rotate_deg: 180, // (0 = false 360 = 360deg)
letters_rotate_reverse: true, // reverse rotation
letters_color: 'random', // Color of letters (hex, rgba, name, random)
letters_neon_light: true, // Emission of light from letters.
letters_neon_color: 'random', // Color of letters (hex, rgba, name, random)
animation_time: 600 // Animation time (s)
};
fallingLettersAnim(option);
Also see: Tab Triggers