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.
<canvas id="info">
The canvas tag is not supported in Internet Explorer 8 and earlier versions.
</canvas>
body{
margin:0;
}
canvas{
background: black;
}
let canvas = document.getElementById("info");
let ctx = canvas.getContext("2d");
// Give canvas the area
// a = b = c ==> b = c; a = b;
let height = canvas.height = window.innerHeight;
let width = canvas.width = window.innerWidth;
let name = "Your Text Here";
let particles = [];
// Particles config
let config = {
maxParticleSize: 7,
minParticleSize: 2,
particleSpeed: 100,
// Give specific colors
colors: ["#004455", "#468966", "#FFF0A5", "#FFB03B", "#006666", "#B64926", "#8E2800", "#FFFFCC"]
};
const getHexColorVariation = () => {
// Hex opacity values from 0-100% (with difference of 5)
// [ "00", "0D", "1A", "26", "33", "40", "4D", "59", "66", "73", "80", "8C", "99", "A6", "B3", "BF", "CC", "D9", "E6", "F2", "FF" ]
let hexOpacityValue = [ "FF" ];
let randomOpacity = hexOpacityValue[ Math.floor(Math.random() * hexOpacityValue.length) ]
return "#" + randomOpacity + "000000".replace(/0/g, function () {return (Math.round(Math.random() * 16)).toString(16);})
}
// Create a particle constructor
// ES5
// A constructor is used to initialize an object ( Hence when called, this constructor defines "this" as an instance of itself )
// Don't use arrow function ( Arrow functions do not have their own "this" )
/**
const Particle = function (x, y) {
this.target = {
x: x,
y: y
}; // co-ordinates to match the target data
this.x = Math.random() * width; // X co-ordinate
this.y = Math.random() * height; // Y co-ordinate
this.r = Math.random() * config.maxParticleSize; // radius
this.c = getHexColorVariation(); // color
// this.c = config.colors[Math.floor(Math.random()*6)];
this.vx = 0; // X velocity
this.vy = 0; // Y velocity
}
Particle.prototype.draw = function () {
// Move the randomly generated particles in the proper direction to form the text
// Target co-ordinate - current co-ordinate => total distance to move + in which direction
this.vx = (this.target.x - this.x) / config.particleSpeed; // specify the speed
this.vy = (this.target.y - this.y) / config.particleSpeed;
this.x += this.vx;
this.y += this.vy;
ctx.fillStyle = this.c;
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.fill();
}
*/
// ES6 Classes
class Particle {
constructor(x, y) {
this.target = {
x: x,
y: y
}; // co-ordinates to match the target data
this.x = Math.random() * width; // X co-ordinate
this.y = Math.random() * height; // Y co-ordinate
this.r = Math.random() * (config.maxParticleSize - config.minParticleSize) + config.minParticleSize; // radius
// this.c = getHexColorVariation(); // color
this.c = config.colors[Math.floor(Math.random()*6)];
this.vx = 0; // X velocity
this.vy = 0; // Y velocity
}
draw() {
// Move the randomly generated particles in the proper direction to form the text
// Target co-ordinate - current co-ordinate => total distance to move + in which direction
this.vx = (this.target.x - this.x) / config.particleSpeed; // specify the speed
this.vy = (this.target.y - this.y) / config.particleSpeed;
// console.log(this.target.x, this.x, this.r, this.c)
this.x += this.vx * 2;
this.y += this.vy * 2;
ctx.fillStyle = this.c;
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
ctx.fill();
}
}
// Adjust size
/**
*
* @param {String} value
* @param {String} fontFamily
*/
const fitTextOnCanvas = (value, fontFamily) =>
measureValueBinaryMethod(value, fontFamily, 0, width, width)
/**
*
* @param {String} value // The text value
* @param {String} fontFamily // Font-family of the text
* @param {Integer} min // Minimum text size
* @param {Integer} max // Maximum text size
* @param {Integer} desiredWidth // Width to fit the text
*/
const measureValueBinaryMethod = (value, fontFamily, min, max, desiredWidth) => {
if(max - min < 1) {
return min;
}
let interval = min + ((max - min) / 2); // Find half interval
let fontSize;
if(value.length < 4) {
fontSize = interval / 2
} else {
fontSize = interval
}
ctx.font = "bold " + fontSize + "px " + fontFamily;
// Returns a TextMetrics object containing the width of the text, in pixels
let measuredTextWidth = ctx.measureText(value).width;
let size;
if(measuredTextWidth > desiredWidth) {
size = measureValueBinaryMethod(value, fontFamily, min, interval, desiredWidth)
} else {
size = measureValueBinaryMethod(value, fontFamily, interval, max, desiredWidth)
}
return size;
}
ctx.textBaseline = "middle";
ctx.textAlign = "center";
fitTextOnCanvas(name, "sans-serif");
const init = () => {
// This part must be inside the function so that each Particle instance can get ctx info
ctx.clearRect(0, 0, width, height);
// Get something to take as a reference to build the particles
// For image, use ctx.drawImage()
ctx.fillText(name, width / 2, height / 2, width);
// fillText(val, x, y, maxWidth) ==> This will squeeze the text to fit the maxWidth
let imageData = ctx.getImageData(0, 0, width, height);
// imageData is an object containing width, height of image in pixels and data
let data = imageData.data// get pixel data
// particles.push(new Particle(12, 12))
// console.log(particles[ 0 ])
// Generate random x and y co-ordinates
// TO generate the particles inside the canvas imageData only, we can use imageData.width or imageData.height in the loop condition
for(let i = 0;i < width;i += Math.round(width / 100)) {
// using height messes up the amount
for(let j = 0;j < height;j += Math.round(height / 50)) {
// The data property returns a 'Uint8ClampedArray' which is accessed to look at the raw pixel data; each pixel is represented by 4 1-byte values (red, green, blue, alpha i.e. "RGBA" format).
// Each component is assigned a consecutive index within the array, with the top left pixel's red component being at index 0 within the array.
// Pixels then proceed from left to right, then downward, throughout the array.
// The Uint8ClampedArray contains height × width × 4 bytes of data, with index values ranging from 0 to (height×width×4)-1.
// Get the pixels from the text/image to draw the particles
let pix = (j * width + i) * 4 // each pixel = 4 unit of RGBA value, so multiply by 4
// Note that this pix is for the whole canvas not just the text/image. To get only the pixels for the text, we need to check if the imageData.data[pixel] has no transparent/rgba=0 and other unnecessary noise colors
if(data[ pix + 3 ] > 0) { // skip the next 3 bytes (4 bytes constitute same pixels)
// console.log("data", data[ 2888 ], pix, name)
particles.push(new Particle(i, j))
}
// console.log(particles.length)
}
}
}
const render = () => {
requestAnimationFrame(render);
ctx.clearRect(0, 0, width, height);
for(var i = 0;i < particles.length;i++) {
particles[ i ].draw();
}
};
const replay = () => {
ctx.clearRect(0, 0, width, height);
particles = [];
init();
}
init();
requestAnimationFrame(render);
Also see: Tab Triggers