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>MediaPipe Selfie Segmentation - model image output</h2>
<p>
<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/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/selfie_segmentation.js" crossorigin="anonymous"></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
});
// MediaPipe 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: "mediapipe-model-interpreter",
// MediaPipe gives us imageData objects which we can drawImage into the RawAsset canvas element
userAttributes: [
{
key: "mask",
defaultValue: false,
setter: function (item) {
item = item.segmentationMask ? item.segmentationMask : false;
if (item) {
this.canvasWidth = item.width;
this.canvasHeight = item.height;
this.mask = item;
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 MediaPipe model sends back new data, we can process it here in our RawAsset object
updateSource: function (assetWrapper) {
const { element, engine, canvasWidth, canvasHeight, mask } = assetWrapper;
if (canvasWidth && canvasHeight && mask) {
// Clear the canvas, resizing it if required
element.width = canvasWidth;
element.height = canvasHeight;
engine.drawImage(mask, 0, 0, canvasWidth, canvasHeight);
}
}
});
// The forever loop function, which captures the MediaPipe model's output and passes it on to our raw asset for processing
const perform = function (mask) {
myAsset.set({ mask });
// This code only runs once, when the model is up-and-running
if (!myOutline)
myOutline = scrawl.makePicture({
name: "outline",
asset: "mediapipe-model-interpreter",
order: 0,
width: "100%",
height: "100%",
copyWidth: "80%",
copyHeight: "80%",
copyStartX: "10%",
copyStartY: "10%",
filters: ["body-blur"]
});
};
// Import and use livestream ... convenience handles for the media stream asset and the Scrawl-canvas entitys
let video, model, myBackground, myOutline;
// Capture the media stream
scrawl
.importMediaStream({
name: "device-camera",
audio: false
})
.then((mycamera) => {
video = mycamera;
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"
});
// Start the MediaPipe model
model = new SelfieSegmentation({
locateFile: (file) =>
`https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/${file}`
});
model.setOptions({ modelSelection: 1 });
model.onResults(perform);
// Use MediaPipe's camera functionality to get updates to the forever loop
const mediaPipeCamera = new Camera(video.source, {
onFrame: async () => {
await model.send({ image: video.source });
},
width: 1280,
height: 720
});
mediaPipeCamera.start();
})
.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.set({
visibility: true
});
myBackground.addFilters(val);
} else {
myBackground.set({
visibility: false
});
}
} 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