Advertisement
STANAANDREY

p5js white black

Nov 5th, 2023
745
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
  7.   <title>p5.js Black and White Video from Camera</title>
  8. </head>
  9. <body>
  10.   <script>
  11.     let video;
  12.     let filteredVideo;
  13.  
  14.     function setup() {
  15.       createCanvas(640, 480);
  16.       video = createCapture(VIDEO);
  17.       video.size(width, height);
  18.       filteredVideo = createImage(width, height);
  19.       // Hide the video element, and just show the canvas
  20.       video.hide();
  21.     }
  22.  
  23.     function draw() {
  24.       // Capture the current video frame
  25.       video.loadPixels();
  26.       filteredVideo.loadPixels();
  27.  
  28.       // Apply black and white filter to the video frame
  29.       for (let i = 0; i < video.pixels.length; i += 4) {
  30.         let average = (video.pixels[i] + video.pixels[i + 1] + video.pixels[i + 2]) / 3;
  31.         filteredVideo.pixels[i] = average;
  32.         filteredVideo.pixels[i + 1] = average;
  33.         filteredVideo.pixels[i + 2] = average;
  34.         filteredVideo.pixels[i + 3] = 255;
  35.       }
  36.  
  37.       // Update the filtered video pixels and draw it onto the canvas
  38.       filteredVideo.updatePixels();
  39.       image(filteredVideo, 0, 0, width, height);
  40.     }
  41.   </script>
  42. </body>
  43. </html>
  44.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement