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.
h1#title="Faux Bokeh"
div#container
canvas#canvas
@import "compass/css3";
@import url(https://fonts.googleapis.com/css?family=Raleway);
body {
background-color: #eee;
min-height: 100%;
color: #fff;
//@include background-image(radial-gradient(center, #eee 0%,#bbb 100%));
background-image: url('http://isaacsuttell.com/img/justine.jpg');
background-size: cover;
background-position: center;
font-family: Raleway, sans-serif;
}
#title {
position: fixed;
top: 10px;
left: 10px;
font-size: 20px;
letter-spacing: 0.1em;
z-index: 100;
color: #fff;
}
/**
* Canvas Experiment
* Based on https://tympanus.net/Development/AnimatedHeaderBackgrounds/index.html
* Deps: GreenSocks TweenLite
*/
/**
* Constructor
*/
function Animate(canvas, options) {
this.canvas = canvas;
this.options = defaults(options || {}, this.options);
this.init();
}
/**
* Default options
*/
Animate.prototype.options = {
density: 10, // Affects how many poitns are created
speed: 10, // Time in seconds to shift points
sync: false, // Should points move in sync
distance: 100, // Distance to move points
lineColor: '255, 255, 255',
circleColor: '255, 255, 255',
radius: 20,
lineWidth: 1,
lines: 3, // Number of closest lines to draw
updateClosest : false, // Update closet points each loop
mouse: true, // Link to mouse or random
};
/**
* Setup everything
*/
Animate.prototype.init = function(){
this.width = window.innerWidth;
this.height = window.innerHeight;
this.target = {
position: {
x: this.width / 2,
y: this.height / 2
}
};
// Setup canvas
this.canvas.width = this.width;
this.canvas.height = this.height;
this.ctx = canvas.getContext('2d');
window.addEventListener('resize', this.resize.bind(this));
if(this.options.mouse === true && !('ontouchstart' in window) ) {
window.addEventListener('mousemove', this.mousemove.bind(this));
}
this.points = [];
for(var x = 0; x < this.width; x = x + this.width / this.options.density) {
for(var y = 0; y < this.height; y = y + this.height/ this.options.density) {
var point = new Point({
x: x + Math.random() * this.width/ this.options.density,
y: y + Math.random() * this.height/this.options.density
}, this.ctx, this.points.length + 1, this.options);
this.points.push(point);
}
}
// Setup Circles
for(var i in this.points) {
this.points[i].circle = new Circle(this.points[i], this.ctx, this.options);
}
this.findClosest(); // Points
this.animate(); // Start the loop
this.shiftPoints(); // Start the tween loop
if(this.options.mouse === false) {
this.moveTarget(); // Start the random target loop
}
};
/*
* Cycles through each Point and finds its neighbors
*/
Animate.prototype.findClosest = function() {
for(var i = 0; i < this.points.length; i++) {
// Save the point
var point = this.points[i];
// Reset
point.closest = [];
// Cycle through the others
for(var j = 0; j < this.points.length; j++) {
// Point to test
var testPoint = this.points[j];
if(point.id !== testPoint.id) {
var placed = false, k;
for (k = 0; k < this.options.lines; k ++) {
if(!placed) {
if(typeof point.closest[k] === 'undefined') {
point.closest[k] = testPoint;
placed = true;
}
}
}
for(k = 0; k < this.options.lines; k++){
if(!placed) {
if(point.distanceTo(testPoint) < point.distanceTo(point.closest[k])) {
point.closest[k] = testPoint;
placed = true;
}
}
}
}
}
}
};
/**
* Animation Loop
*/
Animate.prototype.animate = function() {
var i;
// Should we recalucate closest?
if(this.options.updateClosest) {
this.findClosest();
}
// Calculate Opacity
for(i in this.points) {
if(this.points[i].distanceTo(this.target, true) < 5000) {
this.points[i].opacity = 0.4;
this.points[i].circle.opacity = 0.6;
} else if (this.points[i].distanceTo(this.target, true) < 10000) {
this.points[i].opacity = 0.2;
this.points[i].circle.opacity = 0.3;
} else if (this.points[i].distanceTo(this.target, true) < 30000) {
this.points[i].opacity = 0.1;
this.points[i].circle.opacity = 0.2;
} else {
this.points[i].opacity = 0.05;
this.points[i].circle.opacity = 0.05;
}
}
// Clear
this.ctx.clearRect(0, 0, this.width, this.height);
for(i in this.points) {
this.points[i].drawLines();
this.points[i].circle.draw();
}
// Loop
window.requestAnimationFrame(this.animate.bind(this));
};
/**
* Starts each point in tween loop
*/
Animate.prototype.shiftPoints = function() {
for(var i in this.points) {
this.points[i].shift();
}
};
/**
* Make sure the canvas is the right size
*/
Animate.prototype.resize = function() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.canvas.width = this.width;
this.canvas.height = this.height;
};
/**
* Mouse Move Event - Moves the target with the mouse
* @param event {Object} Mouse event
*/
Animate.prototype.mousemove = function(event) {
if(event.pageX || event.pageY) {
this.target.position.x = event.pageX;
this.target.position.y = event.pageY;
} else if(event.clientX || event.clientY) {
this.target.position.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
this.target.position.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
};
/**
* Randomly move the target
*/
Animate.prototype.moveTarget = function() {
var _this = this;
TweenLite.to(this.target.position, 2, {
x : (Math.random() * (this.width - 200)) + 100,
y : (Math.random() * (this.height - 200)) + 100,
ease: Expo.easeInOut,
onComplete: function() {
_this.moveTarget();
}
});
};
/**
* Point Constructor
* @param position {Object} set of x and u coords
* @param ctx {Object} Canvas context to draw on
* @param options {Object} options passed from main function
*/
function Point(position, ctx, id, options) {
this.options = options || {};
this.id = id;
this.ctx = ctx;
this.position = position || {x: 0, y: 0};
this.origin = {
x: this.position.x,
y: this.position.y
};
this.opacity = 0;
this.closest = [];
}
/**
* Caluclates the distance to another point
* @param point {Object} Another Point
* @param abs {Boolean} Return the absolute value or not
* @returns {Number}
*/
Point.prototype.distanceTo = function(point, abs) {
abs = abs || false;
var distance = Math.pow(this.position.x - point.position.x, 2) + Math.pow(this.position.y - point.position.y, 2);
return abs ? Math.abs(distance) : distance;
};
/**
* Draws lines to the closet points
*/
Point.prototype.drawLines = function() {
for(var i in this.closest) {
if(this.opacity > 0) {
this.ctx.beginPath();
this.ctx.moveTo(this.position.x, this.position.y);
var test = i + 1;
if(!this.closest[test]) {
test = 0;
}
this.ctx.lineCap = 'round';
this.ctx.strokeStyle = 'rgba(' + this.options.lineColor + ', ' + this.opacity + ')';
this.ctx.lineWidth = this.options.lineWidth;
this.ctx.lineTo(this.closest[i].position.x, this.closest[i].position.y);
this.ctx.stroke();
}
}
};
/**
* Tween loop to move each point around it's origin
*/
Point.prototype.shift = function() {
var _this = this,
speed = this.options.speed;
// Random the time a little
if(this.options.sync !== true) {
speed -= this.options.speed * Math.random();
}
TweenLite.to(this.position, speed, {
x : (this.origin.x - (this.options.distance/2) + Math.random() * this.options.distance),
y : (this.origin.y - (this.options.distance/2) + Math.random() * this.options.distance),
ease: Expo.easeInOut,
onComplete: function() {
_this.shift();
}
});
};
/**
* Circle Constructor
* @param point {Object} Point object
* @param ctx {Object} Context to draw on
* @param options {Object} options passed from main function
*/
function Circle(point, ctx, options) {
this.options = options || {};
this.point = point || null;
this.radius = this.options.radius || null;
this.color = this.options.color || null;
this.opacity = 0;
this.ctx = ctx;
}
/**
* Draws Circle to context
*/
Circle.prototype.draw = function() {
if(this.opacity > 0) {
this.ctx.beginPath();
this.ctx.arc(this.point.position.x, this.point.position.y, this.options.radius, 0, 2 * Math.PI, false);
this.ctx.fillStyle = 'rgba(' + this.options.circleColor + ', ' + this.opacity + ')';
this.ctx.fill();
}
};
// Get the balls rolling
new Animate(document.getElementById('canvas'));
/**
* Utility Function to set default options
* @param object {object}
* @param src {object}
*/
function defaults(object, src) {
for(var i in src) {
if(typeof object[i] === 'undefined') {
object[i] = src[i];
}
}
return object;
}
Also see: Tab Triggers