Note that there are some explanatory texts on larger screens.

plurals
  1. POChromakey'd HTML webcam wont save with transparancy
    primarykey
    data
    text
    <p>I have set up a green screen with a webcam, HTML5 video and canvas. I took an idea from MDN on creating a chroma key (green screen). But with a live webcam instead of a prerecorded video. Everything works as expected except when I go to save the image, the chroma key area is white instead of transparent. There is a background of the ocean. When the webcam is on and a person is standing in front of it, it looks like the person is in the ocean. Again, saving that image just gives me a white box with myself in the middle of it. The webcam dimensions are 640 x 480 and the canvas dimensions are 1600 x 800. </p> <p><strong>Edit:</strong> I changed the chroma key tolerance to be anything dark greyish and above. Now if you put something white in front of your camera, it should, in theory, show the entire background image. But its left with a 640x480 box the same color as the <code>&lt;body&gt;</code>.</p> <p><a href="http://cultofpoopsy.net/cam/booth.html" rel="nofollow">Here is a demo</a> Doesn't really work unless you have some green in the background. Webkit only.</p> <p>Webcam chroma key:</p> <pre><code>var processor = { timerCallback: function() { if (this.webcam.paused || this.webcam.ended) { return; } this.computeFrame(); var self = this; setTimeout(function () { self.timerCallback(); }, 0); }, doLoad: function() { this.webcam = document.getElementById("webcam"); this.c1 = document.getElementById("c1"); this.ctx1 = this.c1.getContext("2d"); this.c2 = document.getElementById("c2"); this.ctx2 = this.c2.getContext("2d"); var self = this; this.webcam.addEventListener("play", function() { self.width = 640; self.height = 480; self.timerCallback(); }, false); }, computeFrame: function() { this.ctx1.drawImage(this.webcam, 0, 0, 640, 480); //draw webcam to canvas var frame = this.ctx1.getImageData(0, 0, 640, 480); //capture a frame var l = frame.data.length / 4; for (var i = 0; i &lt; l; i++) { //loop through pixels var r = frame.data[i * 4 + 0]; var g = frame.data[i * 4 + 1]; var b = frame.data[i * 4 + 2]; if (g &gt; 100 &amp;&amp; r &lt; 95 &amp;&amp; b &gt; 60 &amp;&amp; b &lt; 160 ){ frame.data[i * 4 + 3] = 0; } } this.ctx2.putImageData(frame, 480, 300); //place chroma video over bg img return; } }; </code></pre> <p>script.js</p> <pre><code>processor.doLoad(); //more stuff runs takePicture(); function takepicture() { var canv = document.getElementById('c2'); var photo = document.querySelector('#inptPhoto'); var data = canv.toDataURL('image/png'); photo.setAttribute('src', data); } </code></pre> <p>I came across another SO post that suggested my image might not in fact be transparent, but white in the background, which is consistent with my results. Is there another way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload