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 URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
<body>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@100;300;700;800&family=Roboto+Slab&family=Work+Sans:wght@100;200;400;700;800&display=swap" rel="stylesheet">
<canvas id="backgroundCanvas"></canvas>
<div id="mainContainer">
<h1>Procedural Interactive Pixel Art Kaleidoscope</h1>
<div id="h2Container">
<h2>Click and drag to move everything around</h2><h2>Use the scroll wheel to zoom</h2>
<h2>Hold "f" while moving the mouse to change how the colors are calculated</h2>
<h2>Press "w" to increase the iterations (also makes everything run slower)</h2>
<h2>Press "s" to decrease iterations</h2>
<h2>and finally, press "h" to hide this screen, and press "h" to show it again if you need help</h2>
</div>
</div>
</body>
html{
background: black;
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
#backgroundCanvas {
z-index: -99;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: Black;
}
#mainContainer {
background: rgba(0,0,0,.5);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
/*prevent zooming, the text is huge as it is so zoom is not needed for accessability*/
/*doesn't work in safari *sadface*/
-webkit-text-size-adjust: none;
-ms-text-size-adjust: none;
-moz-text-size-adjust: none;
text-size-adjust: none;
-webkit-transform: scale(1);
-moz-transform: scale(1);
-o-transform: scale(1);
transform: scale(1);
}
#h2Container {
display: flex;
width: 80vw;
justify-content: center;
flex-direction: column;
/*background: Red;*/
}
h2 {
color: White;
text-decoration: none;
flex: 0 10px;
z-index: 1;
margin: 0px;
font-family: "Inter", sans-serif;
font-weight: 300;
font-size: 30px;
text-align: center;
}
h1 {
font-family: "Work Sans", sans-serif;
font-weight: 400px;
font-size: 75px;
color: White;
z-index: 1;
text-align: center;
}
const canvas = document.getElementById("backgroundCanvas");
PIXI.utils.skipHello();
const app = new PIXI.Application({
view: canvas,
width: window.innerWidth,
height: window.innerHeight,
resolution: window.devicePixelRatio,
autoDensity: true,
antialias: true
});
let graphic = new PIXI.Graphics();
app.stage.addChild(graphic);
app.ticker.add(animationloop);
//app.ticker.start();
//render quality related global vars
var time = 0;
var slowFrames = 0;
var fastFrames = 0;
var lastLoop = new Date();
//User input related vars
var mouseX = 0;
var mouseY = 0;
var clickX = 0;
var clickY = 0;
var keyX = 0;
var keyY = 0;
var globalX = 300;
var globalY = 300;
var tempGlobalX = 300;
var tempGlobalY = 300;
var dragging = false;
var pressing = false;
var scrollDelta = 0;
var zoom = 100;
var colorMod = 10;
var tempColorMod = 10;
var modifier = 24;
var tempModifier = 24;
var iterations = 6; //changes how deep the picture is, ish? higher numbers = slower
//how many screen pixels wide/high each pixel is
var res = 20;
function animationloop() {
//res= 3;
time++;
//CALCULATE FPS
var thisLoop = new Date();
var fps = 1000 / (thisLoop - lastLoop);
lastLoop = thisLoop;
//console.log(dragging)
//if the fps is way too low, increment a counter to make sure it is not a fluke
if (fps < 15) {
slowFrames++;
}
if (fps > 45) {
fastFrames++;
}
//if our counter keeps incrementing, decrease resolution and reset the counter
if (slowFrames > 3) {
res++;
slowFrames = 0;
}
//if our framerate is good for ~300 frames, increase the resolution
//this function increases the number of good frames required as we get more frames and aproaches ~300
if (fastFrames > Math.atan((time * time) / 1000000) * 200 + 3) {
res--;
fastFrames = 0;
if (res < 1)
//make sure resolution doesn't get better than 1 pixel (otherwise bad things happen)
res = 1;
}
var width = app.screen.width;
var height = app.screen.height;
graphic.clear();
graphic.alpha = 1;
graphic.lineStyle(0, 0xffffff);
if (dragging) {
tempGlobalX = globalX + mouseX - clickX;
tempGlobalY = globalY + mouseY - clickY;
}
if (pressing) {
tempColorMod = colorMod + mouseX - keyX;
tempModifier = modifier + mouseY - keyY;
console.log(tempModifier)
}
zoom += scrollDelta;
scrollDelta *= 0.85;
renderFractal(res, 1 / zoom, tempModifier);
//modifier+=.01;
}
function renderFractal(res, scale, mod) {
for (var y = window.innerHeight / 2 + res; y > -1 * res; y -= res) {
for (var x = window.innerWidth / 2; x > -1 * res; x -= res) {
var ytemp = scale * (y - tempGlobalY);
var xtemp = scale * (x - tempGlobalX); //-mouseX);
//store modified x and y values for input into the XOR
var h = (xor(xtemp + ytemp, ytemp, mod + 0.05) % 255) / 255; //modifier value +- .05 adds color
var s = 1;
var l = (xor(xtemp, ytemp + xtemp, mod - 0.1) % 255) / 255; //+70 for blue background
graphic.beginFill(hslToHex(h, s, l));
rect(x, y, res, res);
rect(x, window.innerHeight - y, res, res);
rect(window.innerWidth - x, y, res, res);
rect(window.innerWidth - x, window.innerHeight - y, res, res);
}
}
}
function xor(x, y, mod) {
y = y - 1; //fixes strange center glitch
//a modified xor (not actually xor)
x = x - 1;
var out = 0;
for (i = 0; i < iterations; i++) {
var powI = Math.pow(
mod / 100 + 1,
i * (tempColorMod / window.innerWidth) * 4
);
out += Math.abs(
200 * powI * ((Math.round(x / powI) * Math.round(y / powI)) % 2)
);
}
return out;
}
function line(x0, y0, x1, y1) {
graphic.moveTo(x0, y0);
graphic.lineTo(x1, y1);
}
function rect(x0, y0, x1, y1) {
graphic.drawRect(x0, y0, x1, y1);
}
window.addEventListener("resize", function () {
app.renderer.resize(window.innerWidth, window.innerHeight);
});
canvas.onmousemove = function (e) {
mouseX = e.pageX - this.offsetLeft;
mouseY = e.pageY - this.offsetTop;
};
window.addEventListener("keydown", function (event) {
const key = event.key; // "a", "1", "Shift", etc.
if ((key == "f" && !pressing)) {
keyX = mouseX;
keyY = mouseY;
pressing = true;
}
if (key == "w") {
iterations++;
}
if (key == "s") {
iterations--;
if(iterations < 1){
iterations = 1;
}
}
if (key == "h") {
if(canvas.style.zIndex == -99){
canvas.style.zIndex = 99;
}else{
canvas.style.zIndex = -99;
}
}
});
window.addEventListener("keyup", function (event) {
const key = event.key; // "a", "1", "Shift", etc.
if (key == "f") {
colorMod = colorMod + mouseX - keyX;
modifier = modifier + mouseY - keyY;
pressing = false;
}
});
document.getElementById("mainContainer").onClick = function(){
canvas.style.zIndex = 99;
}
canvas.addEventListener("mousedown", function () {
clickX = mouseX;
clickY = mouseY;
dragging = true;
});
canvas.addEventListener("mouseup", function () {
dragging = false;
globalX = globalX + mouseX - clickX;
globalY = globalY + mouseY - clickY;
});
function MouseWheelHandler(e) {
//http://blogs.sitepointstatic.com/examples/tech/mouse-wheel/index.html
// cross-browser wheel delta
var e = window.event || e;
scrollDelta += Math.max(-1, Math.min(1, e.wheelDelta || -e.detail));
}
if (canvas.addEventListener) {
canvas.addEventListener("mousewheel", MouseWheelHandler, false);
canvas.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else canvas.attachEvent("onmousewheel", MouseWheelHandler);
function hslToHex(h, s, l) {
var r, g, b;
if (s == 0) {
r = g = b = l; // achromatic
} else {
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return PIXI.utils.rgb2hex([r, g, b]);
}
Also see: Tab Triggers