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="container">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<!-- The viewBox is defined by the size of the image -->
<symbol id="Source">
<image id="image" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/259155/cat_landscape.jpg" x="0" y="0" height="2448" width="3264"/>
</symbol>
<!-- Colour effects -->
<filter id="filter">
<feColorMatrix
type="matrix"
values="0 0 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 1 0 "/>
</filter>
<mask id="mask">
<g id="grid"></g>
</mask>
<mask id="filterMask">
<rect id="filterMaskArea" width="50%" height="100%" x="0" y="0" fill="white" />
</mask>
</defs>
<g mask="url(#mask)">
<use x="0" y="0" xlink:href="#Source" />
<g mask="url(#filterMask)" filter="url(#filter)">
<use x="0" y="0" xlink:href="#Source" />
</g>
</g>
</svg>
</div>
html, body {
background: hsla(240, 0%, 80%, 1);
}
#container {
margin: 10vh auto;
width: 500px;
background: hsla(240, 0%, 70%, 1);
box-shadow: 0px 0px 3px 3px hsla(240, 0%, 60%, 0.8);
visibility: hidden;
}
svg {
width: 100%;
height: 100%;
fill: white;
}
// wait until DOM is ready
document.addEventListener("DOMContentLoaded", function(event) {
// wait until window, stylesheets, images, links, and other media assets are loaded
window.onload = function() {
// All ready, start!
init();
};
});
// We manipulate a few of the DOM elements via JavaScript in order to minimise the amount of editing that would otherwise have to be done to the DOM elements every time the ratio/size of the image is altered
function init() {
// Get the image size
var img = document.querySelector("#image").getBBox();
// Update the <symbol> tag's viewbox based on the image size
document.getElementById("Source").setAttribute("viewBox", "0 0 " + img.width + " " + img.height);
// Calculate the ratio of the image
var ratio = calculateRatio(img.width, img.height);
// Get the main SVG tag
var svg = document.getElementById("svg");
var container = document.getElementById("container");
// Update its height to match the image ratio
container.style.height = updateHeight(container.offsetWidth, ratio) +"px";
TweenMax.set(filterMaskArea, {
attr:{
height:function(){
return updateHeight(container.offsetWidth, ratio);
}
}
});
// Start listening for the mouse
container.addEventListener("mouseenter", onEnter);
container.addEventListener("mouseleave", onLeave);
function onEnter(e) {
// console.log(e.target.id);
container.addEventListener("mousemove", onMove);
}
function onLeave(e) {
container.removeEventListener("mousemove", onMove);
}
function onMove(e) {
var width = e.clientX - container.getBoundingClientRect().left;
TweenMax.set(filterMaskArea, {
attr:{
width:width
}
})
}
// Calculate the aspect ratio of the image.
function calculateRatio(w,h) {
return Math.max(w,h) / Math.min(w,h);
}
// Return an updated height based on width/ratio
function updateHeight(width, ratio) {
return Math.round(width / ratio);
}
// Create X squares of a size along a grid the size of the image
var squareSize = 20;
var squares = {
ttlHorz: squaresPerLength(svg.getBBox().width, squareSize),
ttlVert: squaresPerLength(svg.getBBox().height, squareSize),
}
function squaresPerLength(width, size) {
return width / size;
}
var g = document.getElementById("grid");
var arr = [];
// Loop thru the total squares and create the grid
for(var i = 0; i < squares.ttlHorz; i++) {
for(var j = 0; j < squares.ttlVert; j++) {
var square = document.createElementNS("http://www.w3.org/2000/svg", "rect");
square.setAttribute("width", squareSize);
square.setAttribute("height", squareSize);
square.setAttribute("x", i * squareSize);
square.setAttribute("y", j * squareSize);
arr.push(square);
g.appendChild(square);
}
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
var newArr = shuffle(arr)
// Animate the squares
TweenMax.staggerTo(newArr, 1.5, {
autoAlpha:0,
repeat:-1,
repeatDelay:3,
yoyo:true,
ease:Power3.easeInOut,
cycle:{
delay:[0,0.15,0.3,0.45,0.6,0.75,0.9],
x:function() {
return randomIntFromInterval(-50,50);
},
y:function() {
return randomIntFromInterval(-50,50);
}
}
});
TweenMax.to(container, 0.3, {autoAlpha:1});
};
Also see: Tab Triggers