I simplified @Kirupa's webcam access in browser code by about 20%

<p><video autoplay="true" id="videoElement"></video></p>
<button type="button" id="start" onclick="cam_on()">Cam-on</button>
<script> var video = document.querySelector("#videoElement");
function cam_on() {	stream = navigator.mediaDevices.getUserMedia({ video: true });
	stream.then(function (value) { video.srcObject = value; }); } </script>

Writeup on my blog:

I found @Kirupa’s code, here, to be among the most concise that I’ve found; however, there was a slight logical fallacy with the .then implementation, which I addressed with a variable declaration rather conditional, as before. Hoping to hear @Kirupa’s thoughts.

1 Like

Hey Yakun-hu,
I had a look at your blog and it appears that you have some templating issues…
Are you using raw PHP or a framework?

Hi @yakun-hu - welcome to the forums! :smiley:

I looked at your code and mine, and I am not sure what the bug in my code is. Can you please elaborate?

Kirupa :lady_beetle: