Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Take on me

Eva Ferreira
September 20, 2019

Take on me

Eva Ferreira

September 20, 2019
Tweet

More Decks by Eva Ferreira

Other Decks in Programming

Transcript

  1. Hi! I’m Eva • Front-end Developer by day • Protector

    of useless creativity at night • Mozilla Tech Speaker • CSSConf Argentina organizer @evaferreira92
  2. Hi! I’m María Evangelina Ferreira Kuzminski • Front-end Developer by

    day • Protector of useless creativity at night • Mozilla Tech Speaker • CSSConf Argentina organizer @evaferreira92
  3. Rotoscoping 1. Record a video 2. Draw on top 15

    to 25 images per second @evaferreira92
  4. Chroma Key • Green or blue background • REALLY good

    light  Natural light is the devil. @evaferreira92
  5. Chroma key in the browser 1. Take the user’s camera

    input 1. Put it in a video tag 2. Copy that into a canvas 3. Process canvas 1. Remove green or blue 2. Show background image or video @evaferreira92
  6. Put it in a video tag @evaferreira92 .then(function (stream) {

    video.srcObject = stream; }) .catch(function (error) { console.log("Error:", error); }); navigator.mediaDevices.getUserMedia({video: true})
  7. Update the canvas function time_callback () { if (video.paused ||

    video.ended) { return; } draw(); // Update setTimeout(function () { this.time_callback(); }, 0); };
  8. Ask for pixel information (RGBa) • The red value •

    The green value • The blue value • The Alpha value @evaferreira92
  9. Ask for pixel information (RGBa) • The red value •

    The green value • The blue value • The Alpha value  It will always return 255  … Unless we change it @evaferreira92
  10. Making Chroma happen. 1. Find the green pixels 2. Remove

    them 1. Alpha value of 0 @evaferreira92
  11. Ask for pixel data @evaferreira92 function findGreen () { //

    Ask for color data let frame = this.ctx1.getImageData(0, 0, this.width, this.height); ... }
  12. @evaferreira92 R G B A R G B A R

    G B A R G B A R G B A
  13. Find the green pixels @evaferreira92 function findGreen () { ...

    let allData = frame.data.length / 4; for (let i = 0; i < allData; i++) { ... } }
  14. @evaferreira92 // Save all the data of 1.2m pixels let

    allData = frame.data.length / 4; // Go through it and identify it for (let i = 0; i < allData; i++) { let r = frame.data[i * 4 + 0]; let g = frame.data[i * 4 + 1]; let b = frame.data[i * 4 + 2]; // Ask if the greatest value is green if (g > r && g > b) { // Remove opacity frame.data[i * 4 + 3] = 0; } }
  15. Return the updated data @evaferreira92 function findGreen () { let

    frame = this.ctx1.getImageData(0, 0, this.width, this.height); let l = frame.data.length / 4; for (let i = 0; i < l; i++) { ... frame.data[i * 4 + 3] = 0; } this.ctx1.putImageData(frame, 0, 0); return; }
  16. Improve it 1. Better light 2. Remove only if the

    green is > 100 @evaferreira92 if (g > r && g > b) { if (g > 100 ) { frame.data[i * 4 + 3] = 0; } }
  17. Copy content into canvases @evaferreira92 <canvas id="canvas1" class="canvas canvas1"></canvas> <canvas

    id="canvas2" class="canvas canvas2"></canvas> <canvas id="canvas3" class="canvas canvas3"></canvas>
  18. Copy content into canvases @evaferreira92 function findColor (color) { let

    frame1 = this.ctx1.getImageData(0, 0, this.width, this.height); let frame2 = this.ctx1.getImageData(0, 0, this.width, this.height); let frame3 = this.ctx1.getImageData(0, 0, this.width, this.height); ... this.ctx1.putImageData(frame1, 0, 0); this.ctx2.putImageData(frame2, 0, 0); this.ctx3.putImageData(frame3, 0, 0); return; }
  19. Turn what’s left into pink @evaferreira92 for (let i =

    0; i < l; i++) { ... if (g > r && g > b) { // Remove green } else { frame2.data[i * 4 + 0] = 200; frame3.data[i * 4 + 0] = 200; frame2.data[i * 4 + 2] = 200; frame3.data[i * 4 + 2] = 200; } } 0 = Red 1 = Green 2 = Blue 3 = Alpha
  20. Thanks! :) • Slides: https://speakerdeck.com/evaferreira • Green screen online: https://chroma-code.netlify.com/

    • GitHub repo: https://github.com/evaferreira/chroma-code • @evaferreira92