11/* scripts.js */
22
3- /* DOM NODES */
3+ /**
4+ * DOM NODES
5+ */
46
7+ /* misc */
58const video = document . querySelector ( '.video' ) ;
69const photobooth = document . querySelector ( '.photobooth' ) ;
710const photoboothA = document . querySelector ( '.photobooth::after' ) ;
811const photoboothB = document . querySelector ( '.photobooth::before' ) ;
912const canvas = document . querySelector ( '.canvas' ) ;
1013const ctx = canvas . getContext ( '2d' ) ;
1114const strip = document . querySelector ( '.strip' ) ;
12- const btnSnap = document . querySelector ( '#snap' ) ;
1315const snapSound = document . querySelector ( '.sound-snap' ) ;
1416const snapLimit = document . querySelector ( '#limit' ) ;
1517
@@ -21,24 +23,29 @@ const alertFxMsgs = alertFx.querySelectorAll('.msg');
2123const selectFx = ctrlFx . querySelector ( '#fx' ) ;
2224/* array of tables of control interfaces */
2325const ctrlTables = ctrlFx . querySelectorAll ( 'table' ) ;
24- /* video croma key controls */
25- const fxChroma = ctrlFx . querySelector ( '#ctrl_fx--chroma' ) ;
26- const inputsChroma = ctrlFx . querySelectorAll ( '#ctrl_fx--chroma input' )
27- /* video channel split controls */
28- const fxSplit = ctrlFx . querySelector ( '#ctrl_fx--split' ) ;
29- const selectSplit = ctrlFx . querySelector ( '#split' ) ;
3026/* video channel colorize controls */
31- const fxRGB = ctrlFx . querySelector ( '#ctrl_fx--rgb' ) ;
27+ const ctrlRGB = ctrlFx . querySelector ( '#ctrl_fx--rgb' ) ;
3228const selectRGB = ctrlFx . querySelector ( '#rgb' ) ;
29+ const btnApplyRGB = ctrlRGB . querySelector ( '.btn_apply' ) ;
30+ /* video channel split controls */
31+ const ctrlSplit = ctrlFx . querySelector ( '#ctrl_fx--split' ) ;
32+ const selectSplit = ctrlFx . querySelector ( '#split' ) ;
33+ const btnApplySplit = ctrlSplit . querySelector ( '.btn_apply' ) ;
34+ /* video croma key controls */
35+ const ctrlChroma = ctrlFx . querySelector ( '#ctrl_fx--chroma' ) ;
36+ const inputsChroma = ctrlFx . querySelectorAll ( '#ctrl_fx--chroma input' )
3337
3438/* buttons */
35- const btnCamOn = document . querySelector ( '.ctrl_camera .btn_on' ) ;
36- const btnCamOff = document . querySelector ( '.ctrl_camera .btn_off' ) ;
37- const btnCamClear = document . querySelector ( '.ctrl_camera .btn_clear' ) ;
38- const btnStripClear = document . querySelector ( '.ctrl_strip .btn_clear' ) ;
39- const btnStripSnap = document . querySelector ( '.ctrl_strip .btn_snap' ) ;
39+ const btnOn = document . querySelector ( '.ctrl_camera .btn_on' ) ;
40+ const btnOff = document . querySelector ( '.ctrl_camera .btn_off' ) ;
41+ const btnClearCam = document . querySelector ( '.ctrl_camera .btn_clear' ) ;
42+ const btnClearStrip = document . querySelector ( '.ctrl_strip .btn_clear' ) ;
43+ const btnSnap = document . querySelector ( '.ctrl_strip .btn_snap' ) ;
44+
45+ /**
46+ * VARIABLES
47+ */
4048
41- /* VARIABLES */
4249let myStream ;
4350let front = true ;
4451let positionX = 0 ;
@@ -49,39 +56,36 @@ let videoInterval;
4956
5057const stripMax = 5 ;
5158const stripItemW = 150 ;
59+ const videoSettings = { audio : false , video : true } ;
60+
61+ /**
62+ * DEFAULT STATES
63+ */
5264
53- /* default states */
54- // ctrlFx.style.display = 'none';
55- // ctrlFx.classList.add('disabled');
56- // selectFx.disabled = true;
57- selectFx . style . display = 'none' ;
5865snapLimit . checked = stripLimit ;
5966document . querySelector ( '.ctrl_strip label span' ) . innerHTML = `Limit? (${ stripMax } )` ;
6067
68+ /**
69+ * FUNCTIONS
70+ */
6171
62- /* FUNCTIONS */
63-
64- const videoSettings = {
65- audio : false ,
66- video : true
67- // video: {
68- // width: { min: 1024, ideal: 1280, max: 1920 },
69- // height: { min: 776, ideal: 720, max: 1080 },
70- // facingMode: (front ? "user" : "environment")
71- // }
72- } ;
73-
72+ /**
73+ * request access to client video and audio inputs.
74+ * @param {[type] } mode [description]
75+ * @param {[type] } effect [description]
76+ * @return {[type] } [description]
77+ */
7478function accessMedia ( mode , effect ) {
7579 console . group ( 'START accessMedia' ) ;
7680 console . log ( 'mode: ' , mode ) ;
7781 console . log ( 'effect: ' , effect ) ;
7882 navigator . mediaDevices . getUserMedia ( videoSettings )
79- . then ( function ( mediaStream ) {
83+ . then ( ( mediaStream ) => {
8084 myStream = mediaStream ;
8185 if ( mode && mode === 'start' ) {
8286 // turn video on
8387 video . src = window . URL . createObjectURL ( myStream ) ;
84- video . onloadedmetadata = function ( e ) {
88+ video . onloadedmetadata = ( e ) => {
8589 video . play ( ) ;
8690 } ;
8791 // photoboothB.style.opacity = 0;
@@ -107,19 +111,23 @@ function accessMedia(mode,effect) {
107111 btnSnap . disabled = true ;
108112 btnSnap . classList . add ( 'disabled' ) ;
109113 toggleControls ( ) ;
110- btnCamClear . disabled = false ;
111- btnCamClear . classList . remove ( 'disabled' ) ;
114+ btnClearCam . disabled = false ;
115+ btnClearCam . classList . remove ( 'disabled' ) ;
112116 }
113117 console . log ( 'video.src: ' , video . src ) ;
114118 console . log ( 'myStream: ' , myStream ) ;
115119 } )
116- . catch ( function ( err ) {
120+ . catch ( ( err ) => {
117121 console . error ( 'err: ' , err . name ) ;
118122 // console.log(err.name + ": " + err.message); // always check for errors at the end.
119123 } ) ;
120124 console . groupEnd ( ) ;
121125}
122126
127+ /**
128+ * copy video pixels and put them in the canvas.
129+ * @return {[type] } [description]
130+ */
123131function pixelsToCanvas ( ) {
124132 console . group ( 'START pixelsToCanvas' ) ;
125133 // console.log(video);
@@ -198,8 +206,13 @@ function pixelsToCanvas() {
198206 console . log ( 'selectRGB: ' , selectRGB . value ) ;
199207 } else {
200208 }
201-
202209 // console.log(canvas.width,canvas.height);
210+
211+ /**
212+ * manipulate pixels with video effect.
213+ * @param {[type] } fx [description]
214+ * @return {[type] } [description]
215+ */
203216 function videoFX ( fx ) {
204217 // console.log('fx: ', fx);
205218 ctx . drawImage ( video , positionX , positionY , vWidth , vHeight ) ;
@@ -238,6 +251,11 @@ function pixelsToCanvas() {
238251 return videoInterval ;
239252}
240253
254+ /**
255+ * copy canvas data (photo with video manipulations) to a
256+ * photo strip.
257+ * @return {[type] } [description]
258+ */
241259function snapPhoto ( ) {
242260 console . group ( 'START snapPhoto' ) ;
243261 if ( video . src !== '' ) {
@@ -270,8 +288,8 @@ function snapPhoto() {
270288 if ( stripLen > 0 ) {
271289 strip . style . display = 'flex' ;
272290 document . querySelector ( '.ctrl_strip legend' ) . innerHTML = `snapshots (${ stripLen } )` ;
273- btnStripClear . disabled = false ;
274- btnStripClear . classList . remove ( 'disabled' ) ;
291+ btnClearStrip . disabled = false ;
292+ btnClearStrip . classList . remove ( 'disabled' ) ;
275293 } else {
276294 strip . style . display = 'none' ;
277295 }
@@ -284,37 +302,60 @@ function snapPhoto() {
284302 console . groupEnd ( ) ;
285303}
286304
305+ /**
306+ * reset canvas to no image.
307+ * @return {[type] } [description]
308+ */
287309function clearCanvas ( ) {
288310 console . group ( 'START clearCanvas' ) ;
289311 ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
290- btnCamClear . disabled = true ;
291- btnCamClear . classList . add ( 'disabled' ) ;
312+ btnClearCam . disabled = true ;
313+ btnClearCam . classList . add ( 'disabled' ) ;
292314 console . groupEnd ( ) ;
293315}
294316
317+ /**
318+ * reset photo strip to empty HTML element.
319+ * @return {[type] } [description]
320+ */
295321function clearStrip ( ) {
296322 console . group ( 'START clearStrip' ) ;
297323 strip . innerHTML = `` ;
298324 stripLen = 0 ;
299325 strip . style . display = 'none' ;
300- btnStripClear . disabled = true ;
301- btnStripClear . classList . add ( 'disabled' ) ;
326+ btnClearStrip . disabled = true ;
327+ btnClearStrip . classList . add ( 'disabled' ) ;
328+ document . querySelector ( '.ctrl_strip legend' ) . innerHTML = 'snapshots' ;
302329 console . groupEnd ( ) ;
303330}
304331
332+ /**
333+ * switch between front- and back-facing cameras (mobile devices only).
334+ * @return {[type] } [description]
335+ */
305336function flipCamera ( ) {
306337 console . group ( 'START flipCamera' ) ;
307338 front = ! front ;
308339 console . groupEnd ( ) ;
309340}
310341
342+ /**
343+ * start/restart the video and audio media input recording.
344+ * @param {[type] } effect [description]
345+ * @return {[type] } [description]
346+ */
311347function startStream ( effect ) {
312348 console . group ( 'START startStream' ) ;
313349 clearInterval ( videoInterval ) ;
314350 accessMedia ( 'start' , effect ) ;
315351 console . groupEnd ( ) ;
316352}
317353
354+ /**
355+ * stop the video and audio media input recording.
356+ * TODO: revoke access to video and audio media inputs.
357+ * @return {[type] } [description]
358+ */
318359function stopStream ( ) {
319360 console . group ( 'CAMERA OFF' ) ;
320361 clearInterval ( videoInterval ) ;
@@ -343,13 +384,13 @@ function toggleControls (x) {
343384 /* manipulate pixels */
344385 switch ( x ) {
345386 case 'chroma' :
346- fxChroma . style . display = 'table' ;
387+ ctrlChroma . style . display = 'table' ;
347388 break ;
348389 case 'split' :
349- fxSplit . style . display = 'table' ;
390+ ctrlSplit . style . display = 'table' ;
350391 break ;
351392 case 'rgb_channel' :
352- fxRGB . style . display = 'table' ;
393+ ctrlRGB . style . display = 'table' ;
353394 break ;
354395 }
355396 } else {
@@ -360,8 +401,9 @@ function toggleControls (x) {
360401}
361402toggleControls ( ) ;
362403
363-
364- /* EVENT LISTENERS */
404+ /**
405+ * EVENT LISTENERS
406+ */
365407
366408/* when web cam starts, send pixels to canvas */
367409video . addEventListener ( 'canplay' , pixelsToCanvas ) ;
0 commit comments