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.
<form>
<label>
Height
<input id="height" type="number" value="8" />
</label>
<label>
Width
<input id="width" type="number" value="8" />
</label>
</form>
<div class="main">
<div class="left split">
<canvas id="bitmap" width="500" height="500"></canvas>
</div>
<div class="right split"><textarea name="" id="output" cols="30" rows="10" readonly></textarea></div>
</div>
body, html {
height: 100%;
width: 100%;
}
input {
padding: 0.2rem;
border-radius: 0.2rem;
border: 1px solid #aaa;
margin: 0.2rem;
}
#bitmap {
width: 500px;
height: 500px;
border: 1px solid #aaa;
}
.main {
width: 100%;
}
.split {
width: 500px;
height: 500px;
max-height: 500px;
display: inline-block;
vertical-align: top;
padding: 0;
}
#output {
width: 100%;
height: 100%;
margin: 0;
}
let output = document.getElementById( 'output' );
let canvas = document.getElementById( 'bitmap' );
let ctx = canvas.getContext("2d");
ctx.lineWidth = 2;
ctx.fillStyle = 'black';
let heightInput = document.getElementById( 'height' );
let widthInput = document.getElementById( 'width' );
/*
* Width and height of the Bitmap
* Scale is essentially the side length of a grid pixel as displayed in the browser.
* It's unrealted to the end result and gets smaller as the bitmap gets larger.
* When width or height reach 64 the scale gets unbearably small.
*/
var width, height;
var scale = 50;
/*
* Values is a bool array, containing the entire bitmap
* including overflow
*/
var values = new Array( 256 );
for (var i = 0; i < 256; i++) {
values[i] = new Array( 256 ).fill( false );
}
/*
* pressedLocations is used during drag-coloring
* to make sure that tiles can't flicker.
*/
var pressedLocations = new Array( 256 );
for (var i = 0; i < 256; i++) {
pressedLocations[i] = new Array( 256 ).fill( false );
}
/*
* Drawing mode determines which color is used when drag-painting
* The brush should always draw the same color,
* rather than flip any tile it crosses, it's more intuitive.
* True means paint black; False means paint white.
*/
var brushColor = undefined;
// Draw grid for the first time
updateGrid();
printImageCode();
function updateGrid() {
width = parseInt( widthInput.value, 10 );
height = parseInt( heightInput.value, 10 );
fixScale();
ctx.clearRect(0, 0, 500, 500);
for ( var i = 0; i < width; i++ ) {
for ( var j = 0; j < height; j++ ) {
if ( values[i][j] ) {
// Activated Positions are filled
ctx.fillRect( i*scale, j*scale, scale, scale );
} else {
// Deactivated is drawn empty.
ctx.strokeRect( i*scale, j*scale, scale, scale );
}
}
}
}
function printImageCode() {
// Reset text to a flood of keywords
output.value = "#define bitmap_name_height " + height + "\n";
output.value += "#define bitmap_name_width " + width + "\n\n";
output.value += "static const unsigned char PROGMEM bitmap_name[] = {\n";
for ( var i = 0; i < height; i++ ) {
// Do not use the width, rather round up to the next multiple of 8, because a char has to be initialized.
for ( var j = 0; j < 8*Math.ceil(width/8); j++ ) {
// Begin every char with a binary prefix
if ( j % 8 == 0 ) { output.value += ' B' }
/*
* Only check actual values if they are 'in sight'
* Otherwise, print 0 - so extra rows/cols can just be ignored
* and need not be cleared
*/
if ( j < width ) {
output.value += (values[j][i]) ? "1" : "0";
} else {
output.value += 0;
}
// Seperating by comma is neccesarry in array literals. You know that.
if ( j % 8 == 7 ) { output.value += "," }
}
output.value += "\n";
}
output.value += "};\n\n"
output.value += "display.drawBitmap(0, 0, bitmap_name, bitmap_name_width, bitmap_name_height, WHITE);"
}
function fixScale() {
// Only the bigger direction is relevant for the scale
var limitingSize = ( width > height ) ? width : height;
// Fit it nicely into thze viewbox
scale = ( 500 / ( limitingSize ) );
// But give it a maximum size
scale = Math.min( scale, 50 );
}
function activateMouseMoveHandler( event ) {
// The drag-handler can be the exact same as the click handler.
canvas.onmousemove = canvasClickHandler;
// Also, pass trough the first click.
canvasClickHandler( event );
}
function deactivateMouseMoveHandler() {
canvas.onmousemove = null;
brushColor = undefined;
// Allow all tiles to be changed again.
for (var i = 0; i < 256; i++) {
pressedLocations[i] = new Array( 256 ).fill( false );
}
printImageCode();
}
function canvasClickHandler( event ) {
/*
* First, get relative position in canvas element.
* Then, remove the modulo of scale - giving the upper left point of clicked tile
* Divide by scale to get a clean index.
*/
var x = ( ( event.pageX - canvas.offsetLeft ) -
( ( event.pageX - canvas.offsetLeft ) % scale ) ) / scale;
var y = ( ( event.pageY - canvas.offsetTop ) -
( ( event.pageY - canvas.offsetTop) % scale ) ) / scale;
/*
* Set brush color at the beginning of each drag.
* It will be reset to undefined in deactivateMouseMoveHandler
* Note that we have to compare to undefined in the long way,
* not just check for a falsey value,
* because 'false' is a valid value.
*/
if ( brushColor === undefined ) {
brushColor = !values[x][y];
}
/*
* Color the current tile only if it has not been changed
* during the current drag mode.
* pressedLocations is reset in deactivateMouseHandler.
*/
if ( !pressedLocations[x][y] ) {
if ( brushColor !== undefined ) {
values[x][y] = brushColor;
} else {
values[x][y] = !values[x][y];
}
pressedLocations[x][y] = true;
}
updateGrid();
}
function dimensionChanged() {
updateGrid();
printImageCode();
}
canvas.onmousedown = activateMouseMoveHandler;
canvas.onmouseup = deactivateMouseMoveHandler;
canvas.onmouseout = deactivateMouseMoveHandler;
heightInput.onchange = dimensionChanged;
widthInput.onchange = dimensionChanged;
Also see: Tab Triggers