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.
<div id="main">
<canvas></canvas>
<div id="box" class="anim"></div>
</div>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
#main {
position: relative;
max-width: 90vmin;
}
canvas {
display: block;
max-width: 100%;
position: absolute;
top: 0;
left: 0;
}
canvas:first-child {
position: static;
}
#box {
border: 1px solid rgb(51, 153, 255);
background-color: rgba(51, 153, 255, 0.4);
position: absolute;
top: 40%;
left: 10%;
visibility: hidden;
}
#box.anim {
animation: box 4s linear 1s infinite;
}
@keyframes box {
from {
visibility: visible;
width: 0;
height: 0;
}
20% {
width: 20%;
height: 20%;
}
40% {
opacity: 1;
}
50% {
width: 20%;
height: 20%;
opacity: 0;
visibility: hidden;
}
to {
width: 0;
height: 0;
}
}
import { Pane } from "https://cdn.skypack.dev/tweakpane@3";
import * as d3 from "https://cdn.skypack.dev/d3@7";
const size = 512
const boxElement = document.querySelector('#box')
const canvas = document.querySelector('canvas')
canvas.width = size
canvas.height = size
const ctx = canvas.getContext('2d')
const {
pow,
abs,
sign,
min,
} = Math
const colorConfigs = {
'grayscale': {
bodyBackground: 'white',
baseColor: 'black',
rgba: (iteration, minIteration) => [, , , 255 * (1 - pow(0.99, iteration - minIteration))],
},
'rainbow': {
bodyBackground: `radial-gradient(circle,${
new Array(20).fill().map((_, i) =>
`${d3.interpolateRainbow(i / 20)} ${50 + i / 20 * 50}%`
)}`,
shape: 'round',
baseColor: 'black',
rgba: (iteration, minIteration) => {
return d3.interpolateRainbow((iteration - minIteration) / 100).match(/\d+/g)
}
},
'deep ocean': {
bodyBackground: '#001',
backgroundColor: '#6cf',
baseColor: 'black',
rgba: (iteration, minIteration) => [, , 17, 255 * pow(0.99, iteration - minIteration)],
},
'black and white': {
baseColor: 'white',
rgba: () => [, , , 0],
},
}
class Mandelbrot {
constructor() {
this.left = -2
this.top = -2
this.viewSize = 4
this.colorScheme = 'deep ocean'
this.setup()
this.draw()
}
reset() {
this.left = -2
this.top = -2
this.viewSize = 4
this.setup()
}
setup = () => {
delete this.minIteration
this.colorConfig = colorConfigs[this.colorScheme]
canvas.style.backgroundColor = this.colorConfig.backgroundColor || 'transparent'
canvas.style.borderRadius = this.colorConfig.shape ? '50%' : 0
document.body.style.background = this.colorConfig.bodyBackground || 'black'
ctx.clearRect(0, 0, size, size)
if (this.colorConfig.baseColor) {
ctx.fillStyle = this.colorConfig.baseColor
ctx.fillRect(0, 0, size, size)
}
this.imageData = ctx.getImageData(0, 0, size, size)
this.iteration = 0
this._viewSize = this.viewSize
this.c = []
for (let i = 0; i < size; i++) {
this.c[i] = []
for (let j = 0; j < size; j++) {
const x0 = i * this._viewSize / size + this.left
const y0 = j * this._viewSize / size + this.top
this.c[i][j] = [x0, y0, 0, x0, y0]
}
}
};
draw = () => {
const data = this.imageData.data
const rgba = this.colorConfig.rgba(
this.iteration,
this.minIteration === undefined ? this.iteration : this.minIteration
)
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const [x, y, iteration, x0, y0] = this.c[i][j]
if (iteration >= this.iteration) {
if (x * x + y * y < 4) {
this.c[i][j] = [x * x - y * y + x0, 2 * x * y + y0, iteration + 1, x0, y0]
} else {
if (this.minIteration === undefined) {
this.minIteration = iteration
}
const p = j * size * 4 + i * 4
rgba.forEach((e, i) => {
data[p + i] = e
})
}
}
}
}
ctx.putImageData(this.imageData, 0, 0)
this.iteration += 1
requestAnimationFrame(this.draw)
};
}
const _ = new Mandelbrot()
const pane = new Pane({ title: "Controls" });
const lastChange = (e) => e.last && _.setup();
pane.addMonitor(_, 'iteration')
pane.addInput(_, 'colorScheme', { options: Object.fromEntries(Object.keys(colorConfigs).map(d => [d, d])) }).on('change', lastChange)
pane.addButton({ title: 'Reset'}).on('click', () => _.reset())
canvas.addEventListener('mousedown', function(e) {
if (e.button === 0) {
start(e)
} else {
cancel()
}
})
document.body.addEventListener('click', cancel)
document.body.addEventListener('mousemove', move)
document.body.addEventListener('mouseup', end)
canvas.addEventListener('touchstart', function(e) {
if (e.touches.length === 1) {
e.preventDefault()
start(e.touches[0])
} else {
cancel()
}
})
document.body.addEventListener('touchmove', function(e) {
move(e.touches[0])
})
document.body.addEventListener('touchcancel', cancel)
document.body.addEventListener('touchend', function(e) {
end(e.changedTouches[0])
})
function start(e) {
boxElement.className = ''
const rect = canvas.getBoundingClientRect()
_.box = {
startX: (e.clientX - rect.left) * size / rect.width,
startY: (e.clientY - rect.top) * size / rect.width,
}
}
function move(e) {
if (_.box) {
const rect = canvas.getBoundingClientRect()
const offsetX = (e.clientX - rect.left) * size / rect.width
const offsetY = (e.clientY - rect.top) * size / rect.width
const box = _.box
const _viewSize = min(abs(offsetX - box.startX), abs(offsetY - box.startY))
box.endX = box.startX + sign(offsetX - box.startX) * _viewSize
box.endY = box.startY + sign(offsetY - box.startY) * _viewSize
const style = boxElement.style
style.visibility = 'visible'
style.left = min(box.startX, box.endX) * rect.width / size + 'px'
style.top = min(box.startY, box.endY) * rect.width / size + 'px'
style.width = _viewSize * rect.width / size + 'px'
style.height = _viewSize * rect.width / size + 'px'
}
}
function cancel() {
delete _.box
boxElement.style.visibility = 'hidden'
}
function end(e) {
if (_.box) {
const rect = canvas.getBoundingClientRect()
const offsetX = (e.clientX - rect.left) * size / rect.width
const offsetY = (e.clientY - rect.top) * size / rect.width
const box = _.box
const _viewSize = min(abs(offsetX - box.startX), abs(offsetY - box.startY))
if (_viewSize === 0) {
return
}
box.endX = box.startX + sign(offsetX - box.startX) * _viewSize
box.endY = box.startY + sign(offsetY - box.startY) * _viewSize
const _left = min(box.startX, box.endX)
const _top = min(box.startY, box.endY)
const viewSize = _viewSize / size * _.viewSize
const left = _.left + _left / size * _.viewSize
const top = _.top + _top / size * _.viewSize
_.viewSize = viewSize
_.left = left
_.top = top
cancel()
_.setup()
}
}
Also see: Tab Triggers