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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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="matrix-js"></div>
html, body, #matrix-js {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#canvas {
margin: 0;
padding: 0;
}
#matrix-js {
background-color: black;
}
/* __ .__
* _____ _____ _/ |________|__|__ ___
* / \\__ \\ __\_ __ \ \ \/ /
* | Y Y \/ __ \| | | | \/ |> <
* |__|_| (____ /__| |__| |__/__/\_ \
* \/ \/ \/
* By Jonnelin Marzielli Leonardo
*/
var container;
var canvas;
var ctx;
// Keeps track of all x positions that
// are taken
var takenXPos;
// Number of x positions that exist
var numXPos;
// Number of y positions to create above screen
var numYPos;
// Size of each character
var charSize;
// Size of each column
var columnSize;
// Contains the japanese hiragana
// characters to be used in drops
var CHARACTERS;
// Character fill styles
// 0: white
// 1: gray: rgb(179,178,179)
// 2: gray: rgb(112,111,112)
// 3: white-green: rgb(88,106,94)
// 4: green: rgb(29,98,32)
// 5: -0.2 opacity
// 6: -0.4 opacity
// 7: -0.6 opacity
// 8: -0.8 opacity
// 9: 0 opacity
var FILL_STYLES;
/*----- DROP -----*/
// Number of Chars in each drop; (number of rows in screen * 2) - (number of rows in
// screen * 4)
var dropLength;
// Characters in the drop
var dropChars;
// Char to fade
var fadeIndex;
// Number of characters to fade
var numToFade;
// The x position of the drop
var dropXPos;
// The starting y position of the drop
var initDropYPos;
/*----- CHAR -----*/
// The character of the Char
var char;
// X position of char
var xPos;
// Y position of char
var yPos;
// Char fill style
var fillStyle;
// Char fill style number
var fillStyleIndex;
// Char index in drop
var index;
// Create and initialize canvas
container = document.getElementById("matrix-js");
container.innerHTML = "<canvas id='canvas'></canvas>";
canvas = document.getElementById("canvas");
canvas.width = container.offsetWidth;
canvas.height = container.offsetHeight;
ctx = canvas.getContext("2d");
FILL_STYLES = [];
FILL_STYLES[0] = "rgba(255,255,255,1)";
FILL_STYLES[1] = "rgba(179,178,179,1)";
FILL_STYLES[2] = "rgba(112,111,112,1)";
FILL_STYLES[3] = "rgba(88,106,94,1)";
FILL_STYLES[4] = "rgba(29,98,32,1)";
FILL_STYLES[5] = "rgba(29,98,32,0.8)";
FILL_STYLES[6] = "rgba(29,98,32,0.6)";
FILL_STYLES[7] = "rgba(29,98,32,0.4)";
FILL_STYLES[8] = "rgba(29,98,32,0.2)";
FILL_STYLES[9] = "rgba(29,98,32,0)";
CHARACTERS = "日アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンあいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん";
charSize = 15;
numXPos = Math.ceil(canvas.width / charSize);
numYPos = Math.ceil((canvas.height * 3) / charSize);
ctx.font = "bold " + charSize + "px Arial";
takenXPos = [];
rainAnimate();
// Constructor for a single character
function Char(xPosition, yPosition, index) {
this.char = CHARACTERS.charAt(Math.floor(Math.random() * CHARACTERS.length));
this.xPos = xPosition;
this.yPos = yPosition;
this.index = index;
this.fillStyle = FILL_STYLES[0];
this.fillStyleIndex = 0;
}
// Constructor for a single rain drop
function Drop() {
// this.dropLength = Math.floor(Math.random() * ((((canvas.height / charSize) * 2) - (canvas.height / charSize)) + 1)) + (canvas.height / charSize);
this.dropLength = Math.floor(Math.random() * ((((canvas.height / charSize) * 3) - (canvas.height / charSize)) + 1)) + (canvas.height / charSize);
this.dropChars = [];
this.fadeIndex = 0;
this.numToFade = 1;
this.initDropYPos = 0 - (Math.round(Math.random() * numYPos) * charSize);
// Generate random x position that is not taken
this.dropXPos = Math.round(Math.random() * numXPos) * charSize;
while (isXTaken(this.dropXPos)) {
this.dropXPos = Math.round(Math.random() * numXPos) * charSize;
}
takenXPos.push(this.dropXPos);
}
// Complete rain animation
function rainAnimate() {
// Contains all drops
var drops;
// The minimum number of drops; 80% of the number of
// x positions
var minNumDrops;
// The maximum number of drops; the number of x positions
var maxNumDrops;
// The number of drops
var numDrops;
var changeInterval;
minNumDrops = Math.ceil(0.8 * numXPos);
maxNumDrops = numXPos;
numDrops = Math.floor(Math.random() * ((maxNumDrops - minNumDrops) + 1)) + minNumDrops;
drops = [];
for (var n = 0; n < numDrops; n++) {
drops[n] = new Drop();
}
drawChars(drops);
// Interval for randomly changing a random character
// in all drops
changeInterval = setInterval(function() {
var indexToChange = [];
var randomChar = [];
for (var d = 0; d < drops.length; d++) {
if (drops[d].dropChars.length == 0) {
break;
}
indexToChange[d] = Math.floor(Math.random() * drops[d].dropChars.length);
randomChar[d] = CHARACTERS.charAt(Math.floor(Math.random() * CHARACTERS.length));
drops[d].dropChars[indexToChange[d]].char = randomChar[d];
}
}, 30);
}
// Draws all drop characters
function drawChars(drops) {
// Reset canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Iterate through each drop. At each iteration
// add a new character to the drop. Change the style
// to add white to green effect in the front, and
// fading to black tail. Draw all characters then reset
// every drop whose tail reaches the bottom of the screen
for (var n = 0; n < drops.length; n++) {
var drop = drops[n];
// Add new character
if (drop.dropChars.length == 0) {
drop.dropChars[0] = new Char(drop.dropXPos, drop.initDropYPos, 0);
}
else {
var previousChar = drop.dropChars[drop.dropChars.length - 1];
drop.dropChars[previousChar.index + 1] = new Char(drop.dropXPos, previousChar.yPos + charSize, previousChar.index + 1);
}
// Create white fading to green front
if (drop.dropChars.length > 1) {
var currentChar;
var currentFillStyle;
for (currentChar = drop.dropChars.length - 1, currentFillStyle = 0; currentChar >= 0 && currentFillStyle < 5; currentFillStyle++, currentChar--) {
drop.dropChars[currentChar].fillStyleIndex = currentFillStyle;
drop.dropChars[currentChar].fillStyle = FILL_STYLES[currentFillStyle];
}
}
// Fade tail
if (drop.dropChars.length > drop.dropLength) {
if (drop.dropChars[drop.fadeIndex].fillStyleIndex > 8) {
drop.fadeIndex++;
}
for (var f = 0; f < drop.numToFade && drop.fadeIndex + f < drop.dropChars.length; f++) {
drop.dropChars[drop.fadeIndex + f].fillStyleIndex++;
drop.dropChars[drop.fadeIndex + f].fillStyle = FILL_STYLES[drop.dropChars[drop.fadeIndex + f].fillStyleIndex];
}
drop.numToFade++;
}
// Draw all chars
for(var c = 0; c < drop.dropChars.length; c++) {
ctx.fillStyle = drop.dropChars[c].fillStyle;
ctx.fillText(drop.dropChars[c].char, drop.dropChars[c].xPos, drop.dropChars[c].yPos);
}
// Reset drop
// Remove from taken x pos when done
//if (drop.fadeIndex > (canvas.height/ charSize)) {
if (drop.dropChars[drop.fadeIndex].yPos > canvas.height) {
var indexOfXPos = takenXPos.indexOf(drop.dropXPos);
takenXPos.splice(indexOfXPos, 1);
drops[n] = new Drop();
}
}
// Animate drops
setTimeout(function() {
requestAnimationFrame(function() {
drawChars(drops);
});
}, 50);
}
// Checks if x position is taken
function isXTaken(xCheck) {
if (takenXPos.indexOf(xCheck) != -1) {
return true;
}
else {
return false;
}
}
Also see: Tab Triggers