Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>MediaPipe Selfie Segmentation - 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"></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>
              
            
!

CSS

              
                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%;
}

              
            
!

JS

              
                import scrawl from "https://unpkg.com/scrawl-canvas@8.5.5";

// 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";

              
            
!
999px

Console