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.
<h2>Tensorflow tfjs-models / body-pix experiment - model image output</h2>
<div class="controls">
<div class="yellow label">Background filter</div>
<div class="yellow">
<select class="controlItem" id="backgroundFilter">
<option value="">None</option>
<option value="grayscale">Grayscale</option>
<option value="sepia">Sepia</option>
<option value="invert">Invert</option>
<option value="red">Red</option>
<option value="background-blur">Blur</option>
<option value="pixelate">Pixelate</option>
</select>
</div>
<div class="yellow label">Blur outline</div>
<div class="yellow">
<select class="controlItem" id="outlineFilter">
<option value="0">False</option>
<option value="1">True</option>
</select>
</div>
</div>
<canvas id="mycanvas" width="600" height="400" data-scrawl-canvas></canvas>
<p>Learn more about <a href="https://scrawl-v8.rikweb.org.uk/">Scrawl-canvas v8</a> on the library's homepage</p>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]"></script>
body {
font-family: sans-serif;
text-align: center;
}
canvas {
margin: 0 auto;
}
.controls {
display: grid;
grid-column-gap: 2px;
grid-template-rows: auto;
grid-row-gap: 2px;
font-size: 12px;
grid-template-columns: 1fr 2fr 1fr 2fr;
}
.controls div {
box-sizing: border-box;
justify-self: stretch;
align-self: center;
text-align: center;
padding: 6px 0;
}
.label {
font-weight: bold;
}
.yellow {
background-color: palegoldenrod;
}
.controls select {
border: 0;
}
.controls input {
width: 70%;
}
import * as scrawl from "https://unpkg.com/[email protected]";
// Grab a handle to the canvas element in the DOM
const canvas = scrawl.library.canvas.mycanvas;
// Create some filters which we can use for the demo background
scrawl.makeFilter({
name: "grayscale",
method: "grayscale"
})
.clone({
name: "sepia",
method: "sepia"
})
.clone({
name: "invert",
method: "invert"
})
.clone({
name: "red",
method: "red"
});
scrawl.makeFilter({
name: "pixelate",
method: "pixelate",
tileWidth: 20,
tileHeight: 20,
});
scrawl.makeFilter({
name: "background-blur",
method: "gaussianBlur",
radius: 20
});
scrawl.makeFilter({
name: "body-blur",
method: "gaussianBlur",
radius: 10
});
// TensorFlow functionality - we'll handle everything in a raw asset object, which a Scrawl-canvas Picture entity can then use as its source
let myAsset = scrawl.makeRawAsset({
name: "tensorflow-model-interpreter",
// We're only interested in the pixel allocations generated by the tensorflow model for this demo
userAttributes: [
{
key: "data",
defaultValue: [],
setter: function (item) {
if (item && item.width && item.height && item.data) {
this.canvasWidth = item.width;
this.canvasHeight = item.height;
this.data = item.data;
this.dirtyData = true;
}
}
},
// We'll use these additional attributes in the update function, below
{
key: "canvasWidth",
defaultValue: 0,
setter: () => {}
},
{
key: "canvasHeight",
defaultValue: 0,
setter: () => {}
}
],
// Every time the TensorFlow model sends back new data, we can process it here in our RawAsset object
updateSource: function (assetWrapper) {
// The RawAsset object supplies its own canvas element and context engine, alongside the attributes we defined earlier
const { element, engine, canvasWidth, canvasHeight, data } = assetWrapper;
if (canvasWidth && canvasHeight && data) {
// Create a new image asrtray and image data object for each data update
const segLength = canvasWidth * canvasHeight,
imageDataLen = segLength * 4,
imageArray = new Uint8ClampedArray(imageDataLen);
for (let i = 0, o = 0; i < segLength; i++) {
o = i * 4 + 3;
if (data[i]) imageArray[o] = 255;
}
const iData = new ImageData(imageArray, canvasWidth, canvasHeight);
// Clear the canvas, resizing it if required
element.width = canvasWidth;
element.height = canvasHeight;
engine.putImageData(iData, 0, 0);
}
}
});
// The forever loop function, which captures the TensorFlow model's output and passes it on to our raw asset for processing
const perform = function (net) {
net.segmentPerson(video.source)
.then((data) => {
myAsset.set({ data });
perform(net);
})
.catch((e) => console.log('Perform error', e.message));
};
// Import and use livestream ... convenience handles for the media stream asset and the Scrawl-canvas entitys
let video, myBackground, myOutline;
// Capture the media stream
scrawl.importMediaStream({
name: "device-camera",
audio: false
})
.then((mycamera) => {
video = mycamera;
// This fixes the issue in Firefox where the media stream will crash TensorFlow if the stream's video element's dimensions have not been set
video.source.width = "1280";
video.source.height = "720";
// Take the media stream and display it in our canvas element
myBackground = scrawl.makePicture({
name: "background",
asset: mycamera.name,
order: 2,
width: "100%",
height: "100%",
copyWidth: "80%",
copyHeight: "80%",
copyStartX: "10%",
copyStartY: "10%",
filters: ['pixelate'],
globalCompositeOperation: "destination-over"
});
myBackground.clone({
name: "body",
order: 1,
filters: [],
globalCompositeOperation: "source-in"
});
// We need confirmation that the media stream is working before we start the model running
video.source.addEventListener('loadeddata', (event) => {
// Start the TensorFlow model
bodyPix.load()
.then((net) => {
// Display the visual generated by our raw asset
myOutline = scrawl.makePicture({
name: "outline",
asset: "tensorflow-model-interpreter",
order: 0,
width: "100%",
height: "100%",
copyWidth: "80%",
copyHeight: "80%",
copyStartX: "10%",
copyStartY: "10%",
// We blur here to make the outline merge into the background
filters: ["body-blur"]
});
// Invoke the forever loop
perform(net);
})
.catch((e) => console.log('BodyPix error', e.message));
});
})
.catch((e) => console.log('Media stream error', e.message));
// Create the Scrawl-canvas Display cycle animation
scrawl.makeRender({
name: "demo-animation",
target: canvas
});
// User interaction - Event listeners
scrawl.addNativeListener(
["input", "change"],
(e) => {
e.preventDefault();
e.returnValue = false;
if (e && e.target) {
const id = e.target.id,
val = e.target.value;
if ("backgroundFilter" === id) {
myBackground.clearFilters();
if (val) myBackground.addFilters(val);
} else {
if ("1" === val) myOutline.addFilters("body-blur");
else myOutline.clearFilters();
}
}
},
".controlItem"
);
// Set DOM form initial input values
document.querySelector("#backgroundFilter").value = "pixelate";
document.querySelector("#outlineFilter").value = "1";
Also see: Tab Triggers