html5 webcam instagram filters clown in javascript

To create a webcam with Instagram filters in JavaScript, you can use the HTML5 Canvas element and the getUserMedia() method to access the user's webcam. Then, you can apply filters to the images captured from the webcam using a library like CamanJS or Pixastic.

For this particular case, we'll be using CamanJS, a powerful image manipulation library that provides a simple API for applying filters to images.

First, you need to create an HTML page with a video element, a canvas element, and a button to take a picture:

<!DOCTYPE html>
<html>
<head>
  <title>HTML5 WebCam Instagram Filters Clown in JavaScript</title>
</head>
<body>
  <video id="video" width="640" height="480" autoplay></video>
  <canvas id="canvas" width="640" height="480"></canvas>
  <button id="take-picture">Take Picture</button>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/camanjs/4.1.2/caman.min.js"></script>
  <script src="script.js"></script>
</body>
</html>
427 chars
14 lines

Then, you need to write the JavaScript code to access the user's webcam, create a stream with the getUserMedia() method, and set the stream as the source for the video element:

index.tsx
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const button = document.getElementById('take-picture');
const constraints = { video: true };

navigator.mediaDevices.getUserMedia(constraints)
  .then((stream) => {
    video.srcObject = stream;
    video.play();
  })
  .catch((error) => {
    console.error(`Error accessing user's webcam: ${error}`);
  });
406 chars
14 lines

Next, you need to write the code to take a picture from the video stream, draw it onto the canvas, and apply the filters using CamanJS:

index.tsx
button.addEventListener('click', () => {
  canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
  Caman(canvas, function() {
    this.revert(false);
    this.newLayer(function() {
      this.fillColor('#FFFFFF');
      this.opacity(20);
      this.setBlendingMode('overlay');
    });
    this.brightness(10);
    this.contrast(30);
    this.saturation(-20);
    this.render();
  });
});
411 chars
16 lines

In this example, we're applying a white overlay with the blending mode set to overlay, increasing the brightness by 10, increasing the contrast by 30, and decreasing the saturation by 20. You can experiment with different filters and settings to achieve the Instagram effect you want.

And that's it! With this code, you'll have an HTML5 webcam with Instagram filters clown in JavaScript.

gistlibby LogSnag