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.
<input id="file" type="file" />
<canvas id="canvas"></canvas>
function decodeNumber(code, bits) {
const l = Math.pow(2, code - 1);
if (bits >= l) {
return bits;
}
return bits - (2 * l - 1);
}
class IDCT {
constructor() {
this.base = new Array(64).fill(0);
this.zigzag = [
[0, 1, 5, 6, 14, 15, 27, 28],
[2, 4, 7, 13, 16, 26, 29, 42],
[3, 8, 12, 17, 25, 30, 41, 43],
[9, 11, 18, 24, 31, 40, 44, 53],
[10, 19, 23, 32, 39, 45, 52, 54],
[20, 22, 33, 38, 46, 51, 55, 60],
[21, 34, 37, 47, 50, 56, 59, 61],
[35, 36, 48, 49, 57, 58, 62, 63],
];
this.idct_precision = 8;
this.idct_table = new Array(this.idct_precision)
.fill(0)
.map(() => new Array(this.idct_precision).fill(0));
for (let u = 0; u < this.idct_precision; u++) {
for (let x = 0; x < this.idct_precision; x++) {
this.idct_table[u][x] =
this.normCoeff(u) * Math.cos(((2.0 * x + 1.0) * u * Math.PI) / 16.0);
}
}
}
normCoeff(n) {
if (n === 0) {
return 1.0 / Math.sqrt(2.0);
} else {
return 1.0;
}
}
rearrange_using_zigzag() {
for (let x = 0; x < 8; x++) {
for (let y = 0; y < 8; y++) {
this.zigzag[x][y] = this.base[this.zigzag[x][y]];
}
}
return this.zigzag;
}
perform_IDCT() {
const out = [];
for (let i = 0; i < 8; i++) {
out.push([]);
}
for (let x = 0; x < 8; x++) {
for (let y = 0; y < 8; y++) {
let local_sum = 0;
for (let u = 0; u < this.idct_precision; u++) {
for (let v = 0; v < this.idct_precision; v++) {
local_sum +=
this.zigzag[v][u] * this.idct_table[u][x] * this.idct_table[v][y];
}
}
out[y][x] = Math.floor(local_sum / 4);
}
}
this.base = out;
}
}
class BitStream {
constructor(data) {
this.data = data;
this.pos = 0;
}
getBit() {
const b = this.data[this.pos >> 3];
const s = 7 - (this.pos & 0x7);
this.pos += 1;
return (b >> s) & 1;
}
getBitN(l) {
let val = 0;
for (let i = 0; i < l; i++) {
val = val * 2 + this.getBit();
}
return val;
}
}
class HuffmanTable {
constructor() {
this.root = [];
this.elements = [];
}
bitsFromLengths(root, element, pos) {
if (Array.isArray(root)) {
if (pos === 0) {
if (root.length < 2) {
root.push(element);
return true;
}
return false;
}
for (let i = 0; i < 2; i++) {
if (root.length === i) {
root.push([]);
}
if (this.bitsFromLengths(root[i], element, pos - 1) === true) {
return true;
}
}
}
return false;
}
getHuffmanBits(lengths, elements) {
this.elements = elements;
let ii = 0;
for (let i = 0; i < lengths.length; i++) {
for (let j = 0; j < lengths[i]; j++) {
this.bitsFromLengths(this.root, elements[ii], i);
ii++;
}
}
}
find(st) {
let r = this.root;
while (Array.isArray(r)) {
r = r[st.getBit()];
}
return r;
}
getCode(st) {
while (true) {
let res = this.find(st);
if (res === 0) {
return 0;
} else if (res !== -1) {
return res;
}
}
}
}
file.addEventListener("change", (e) => {
const [file] = e.target.files;
const reader = new FileReader();
reader.onload = () => [parse(reader.result)];
reader.readAsArrayBuffer(file);
});
function buildQuantizationTables(data) {
let offset = 0;
// Get the header byte
let header = data[0];
offset += 1;
return {
header: header.toString(16),
table: data.slice(1, 64 + offset),
};
}
function decodeHuffman(data) {
let offset = 0;
// Get the header byte
let header = data[0];
offset += 1;
// Extract the 16 bytes containing length data
let lengths = [];
for (let i = 0; i < 16; i++) {
lengths.push(data[offset]);
offset++;
}
// Extract the elements after the initial 16 bytes
let elements = [];
lengths.forEach((length) => {
for (let i = 0; i < length; i++) {
elements.push(data[offset]);
offset++;
}
});
const hf = new HuffmanTable();
hf.getHuffmanBits(lengths, elements);
// log results
console.log("Header: ", header.toString(16));
console.log("Lengths: ", lengths);
console.log("Elements: ", elements);
console.log(data.slice(offset));
return {
header: header,
table: hf,
};
}
function buildMatrix({ st, idx, quant, olddccoeff, hf_tables }) {
const i = new IDCT();
let code = hf_tables[0 + idx].getCode(st);
const bits = st.getBitN(code);
const dccoeff = decodeNumber(code, bits) + olddccoeff;
i.base[0] = dccoeff * quant[0];
let l = 1;
while (l < 64) {
code = hf_tables[16 + idx].getCode(st);
if (code === 0) {
break;
}
if (code > 15) {
l += code >> 4;
code = code & 0x0f;
}
const bits = st.getBitN(code);
if (l < 64) {
const coeff = decodeNumber(code, bits);
i.base[l] = coeff * quant[l];
l += 1;
}
}
i.rearrange_using_zigzag();
i.perform_IDCT();
return [i, dccoeff];
}
function startOfScan({
data,
hdrlen,
width,
height,
quantMapping,
quantizationTables,
hfTables,
}) {
canvas.width = width;
canvas.height = height;
var [data, lenchunk] = removeFF00(data.slice(hdrlen));
const st = new BitStream(data);
let [oldlumdccoeff, oldCbdccoeff, oldCrdccoeff] = [0, 0, 0];
for (let y = 0; y < Math.floor(height / 8); y++) {
for (let x = 0; x < Math.floor(width / 8); x++) {
const resultL = buildMatrix({
st,
idx: 0,
quant: quantizationTables[quantMapping[0]],
hf_tables: hfTables,
olddccoeff: oldlumdccoeff,
});
oldlumdccoeff = resultL[1];
const resultCb = buildMatrix({
st,
idx: 1,
quant: quantizationTables[quantMapping[1]],
hf_tables: hfTables,
olddccoeff: oldCbdccoeff,
});
oldCbdccoeff = resultCb[1];
const resultCr = buildMatrix({
st,
idx: 1,
quant: quantizationTables[quantMapping[2]],
hf_tables: hfTables,
olddccoeff: oldCrdccoeff,
});
oldCrdccoeff = resultCr[1];
drawOnCanvas(x, y, resultL[0].base, resultCb[0].base, resultCr[0].base);
}
}
return lenchunk + hdrlen;
}
function clamp(value) {
return Math.min(Math.max(value, 0), 255);
}
function convert(Y, Cr, Cb) {
const R = Cr * (2 - 2 * 0.299) + Y;
const B = Cb * (2 - 2 * 0.114) + Y;
const G = (Y - 0.114 * B - 0.299 * R) / 0.587;
return [clamp(R + 128), clamp(G + 128), clamp(B + 128)];
}
function drawOnCanvas(x, y, matL, matCb, matCr) {
const ctx = canvas.getContext("2d");
for (let yy = 0; yy < 8; yy++) {
for (let xx = 0; xx < 8; xx++) {
const [r, g, b] = convert(matL[yy][xx], matCr[yy][xx], matCb[yy][xx]);
const str = `rgb(${Math.floor(r)}, ${Math.floor(g)}, ${Math.floor(b)})`;
ctx.fillStyle = str;
ctx.fillRect(x * 8 + xx, y * 8 + yy, 1, 1);
}
}
}
function baseDCT(data) {
const header = data[0];
const height = (data[1] << 8) + data[2];
const width = (data[3] << 8) + data[4];
const numOfComps = data[5];
let d = data.slice(6);
const result = [];
for (let i = 0; i < numOfComps; i++) {
const id = d[i * 3 + 0];
const samp = d[i * 3 + 1];
const qtbId = d[i * 3 + 2];
result.push(qtbId);
}
return {
result,
width,
height,
};
}
function removeFF00(data) {
const datapro = [];
let i = 0;
while (true) {
var b = data[i];
var bnext = data[i + 1];
if (b === 0xff) {
if (bnext !== 0) {
break;
}
datapro.push(b);
i += 2;
} else {
datapro.push(b);
i += 1;
}
}
return [datapro, i];
}
function parse(data) {
const hfTables = {};
const quantizationTables = {};
let quantMapping;
let d = new Uint8Array(data);
let w;
let h;
while (true) {
const marker = new Uint8Array(d.slice(0, 2));
// console.log(marker[0].toString(16), marker[1].toString(16));
if (marker[0] === 0xff && marker[1] === 0xd8) {
console.log("start of image");
d = d.slice(2);
} else if (marker[0] === 0xff && marker[1] === 0xd9) {
console.log("end of image");
return;
} else {
const lenchunk = d.slice(2, 4);
let len = (lenchunk[0] << 8) + lenchunk[1] + 2;
const chunk = d.slice(4, len);
if (marker[0] === 0xff && marker[1] === 0xc4) {
// console.log(d, chunk);
const { table, header } = decodeHuffman(chunk);
hfTables[header] = table;
} else if (marker[0] === 0xff && marker[1] === 0xdb) {
const { header, table } = buildQuantizationTables(chunk);
quantizationTables[header] = table;
} else if (marker[0] === 0xff && marker[1] === 0xc0) {
// start of frame
const { result, width, height } = baseDCT(chunk);
quantMapping = result;
w = width;
h = height;
} else if (marker[0] === 0xff && marker[1] === 0xda) {
// console.log(quantMapping, quantizationTables);
len = startOfScan({
data: d,
hdrlen: len,
width: w,
height: h,
quantizationTables,
quantMapping,
hfTables,
});
}
d = d.slice(len);
}
if (d.length === 0) {
break;
}
}
}
Also see: Tab Triggers