Skip to content

Commit 16a271c

Browse files
committed
proj 19: work on modifying canvas dimensions to not distort video.
1 parent aec741a commit 16a271c

File tree

6 files changed

+102
-78
lines changed

6 files changed

+102
-78
lines changed

19--Webcam_Fun/css/controls.css

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
.ctrls fieldset {
2-
color:black;
3-
border-color:transparent;
4-
background-color:rgba(0,0,0,0.75);
5-
box-shadow:0 0.25rem 1rem 0 rgba(0, 0, 0, 0.25);
6-
}
7-
.ctrls legend {
8-
color:rgb(255,255,255);
9-
background-color:rgba(0,0,0,0);
10-
/* box-shadow:0 0.125rem 0.5rem 0 rgba(0, 0, 0, 0.5); */
11-
}
12-
131
.ctrl_fx .ctrl_group label {
142
width:100%;
153
background-color:rgb(204,204,204);
@@ -35,7 +23,8 @@
3523

3624
#ctrl_fx .ctrl_group .rgb label span {display: none;}
3725

38-
label span .value {
26+
.ctrls legend .value,
27+
.ctrls label .value {
3928
padding:0.25em 0.5rem;
4029
color:white;
4130
line-height:1.4;
@@ -51,3 +40,6 @@ label span .value {
5140
font-size: 2rem;
5241
} */
5342
/* .rgb:before {content:"=";} */
43+
44+
.setting {float:right; width:auto; color:black;}
45+
.setting span {font-size: 0.8rem;}

19--Webcam_Fun/css/style.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ h3,
2222
h4,
2323
h5,
2424
h6 {}
25-
/* code {font-size: 1.5em;} */
25+
code {font-size: 1.5em;}
2626
table {
2727
width:100%;
2828
border-collapse:separate;
@@ -71,7 +71,8 @@ fieldset {
7171
}
7272
legend {
7373
font-weight:900;
74-
margin-bottom:0.25rem;
74+
margin:0;
75+
padding:0;
7576
line-height:1;
7677
width: 100%;
7778
}

19--Webcam_Fun/css/theme.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* theme styles */
2+
.ctrls fieldset {
3+
color:black;
4+
border-color:transparent;
5+
background-color:rgba(127,127,127,0.5);
6+
box-shadow:0 0.25rem 1rem 0 rgba(0, 0, 0, 0.25);
7+
}
8+
.ctrls legend {
9+
position: relative;
10+
top:1.75em;
11+
padding: 0.25em 0;
12+
margin-bottom: 1.5em;
13+
font-weight:900;
14+
height:3em;
15+
color: rgb(255,255,255);
16+
background-color: rgba(0,0,0,0);
17+
}
18+
.ctrls legend .value,
19+
.ctrls label .value {
20+
border-radius:1rem;
21+
color:rgb(255,255,255);
22+
background-color:rgba(0,0,0,1);
23+
box-shadow:0 0.125rem 0.5rem 0 rgba(0, 0, 0, 0.5);
24+
}
25+
.input-range {}
26+
.input-select {}
27+
.input-toggle {}

19--Webcam_Fun/index-jds.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<link rel="stylesheet" href="css/controls.css">
99
<!-- <link rel="stylesheet" href="css/test.css"> -->
1010
<!-- <link rel="stylesheet" href="css/mq.css"> -->
11+
<link rel="stylesheet" href="css/theme.css">
1112
</head>
1213
<body>
1314

@@ -21,11 +22,11 @@ <h1>webcam fun</h1>
2122
<fieldset id="ctrl_camera">
2223
<legend>cam</legend>
2324
<button class="btn_on status success">On</button>
24-
<button class="btn_off status error disabled" disabled>Off</button>
25+
<button class="btn_off status error">Off</button>
2526

2627
<!-- convert on/off buttons into checkbox toggle -->
2728
<!-- <label class="" for="cam">
28-
<input type="checkbox" id="cam" class="">
29+
<input type="checkbox" id="cam" class="input-toggle">
2930
<span class="label">On?</span>
3031
</label> -->
3132

@@ -42,9 +43,9 @@ <h1>webcam fun</h1>
4243
<option value="Pixelate">Pixelate</option>
4344
</select>
4445
<!-- add checkbox toggle for mirror effect -->
45-
<label class="" for="mirror" style="float:right; width:auto; color:black;">
46-
<input type="checkbox" id="mirror" class="">
47-
<span class="label">Mirror?</span>
46+
<label class="setting" for="mirror" style="">
47+
<input type="checkbox" id="mirror" class="input-toggle" checked>
48+
<span class="label">Mirror</span>
4849
</label>
4950
</legend>
5051
<div class="alert info" id="msg_cam">
@@ -190,11 +191,12 @@ <h1>webcam fun</h1>
190191
</table>
191192
</fieldset>
192193
<fieldset id="ctrl_strip">
193-
<legend>snaps</legend>
194-
<label class="" for="limit">
195-
<input type="checkbox" id="limit" class="" checked>
196-
<span class="label">Limit?</span>
194+
<legend>snaps <code class="value">0</code>
195+
<label class="setting" for="limit" style="">
196+
<input type="checkbox" id="limit" class="input-toggle" checked>
197+
<span class="label">Limit <code class="value">0</code></span>
197198
</label>
199+
</legend>
198200
<button class="btn_snap status info disabled" disabled>Snap</button>
199201
<button class="btn_clear disabled" disabled>Clear</button>
200202
</fieldset>

19--Webcam_Fun/js/scripts.js

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ const constraints = {audio: false, video: true};
8080
*/
8181

8282
hide(video);
83+
show(btnOn);
84+
hide(btnOff);
8385
// hide(selectFx);
8486
// selectFx.disabled = true;
85-
document.querySelector('#ctrl_strip label span').innerHTML = `Limit? (${stripMax})`;
87+
document.querySelector('#ctrl_strip label span .value').innerHTML = `${stripMax}`;
8688

8789
/**
8890
* FUNCTIONS
@@ -100,7 +102,7 @@ function fChromaKeyInputs (group) {
100102
if (input.type === 'range'||input.type === 'number') {
101103
levels[input.name] = input.value;
102104
const label = input.parentElement;
103-
label.querySelector("span").querySelector("code").innerHTML = input.value;
105+
label.querySelector("span .value").innerHTML = input.value;
104106
const cell = label.parentElement;
105107
let bg;
106108
// console.log(cell.classList[0]+' '+cell.classList[1]+': '+input.value);
@@ -151,7 +153,7 @@ function fUpdateInput (input) {
151153
bg = `#808080`;
152154
br = `${1/input.value}rem`;
153155
} else {
154-
console.error(' NOT saturate OR pixelate!');
156+
console.warn(' NOT saturate OR pixelate!');
155157
}
156158
label.querySelector("code").style.backgroundColor = bg;
157159
label.querySelector("code").style.borderRadius = br;
@@ -163,6 +165,18 @@ inputAllRange.forEach((input)=>{
163165
fUpdateInput(input);
164166
});
165167

168+
169+
function fMirror () {
170+
if (mirror.checked) {
171+
console.log('FLIP IT');
172+
canvas.style.transform = 'scale(-1,1)';
173+
} else {
174+
console.log('DON\'T FLIP IT');
175+
canvas.style.transform = 'scale(1,1)';
176+
}
177+
}
178+
fMirror();
179+
166180
/**
167181
* [toggleFxControls description]
168182
* @return {[type]} [description]
@@ -201,7 +215,7 @@ function fMediaQueries () {
201215
/* switch input type between 'number'/'range' based on window size. */
202216
input.type = window.innerWidth<800 ? 'number' : 'range';
203217
/* show/hide color swatch based on window size (to match input type). */
204-
input.parentElement.querySelector("span code").style.display = window.innerWidth<800 ? 'none' : 'inline';
218+
input.parentElement.querySelector("span .value").style.display = window.innerWidth<800 ? 'none' : 'inline';
205219
}
206220
/* show/hide min rgb color label text based on window size. */
207221
// rgbMin.querySelector("span").style.display = window.innerWidth<800 ? 'none' : 'inline';
@@ -259,7 +273,8 @@ if (navigator.mediaDevices) {
259273
/* reset & enable the F/X select/option */
260274
// toggleButton(selectFx,'on');
261275

262-
toggleButton(btnOff,'on');
276+
hide(btnOn);
277+
show(btnOff);
263278
toggleButton(btnSnap,'on');
264279

265280
} else {
@@ -275,7 +290,8 @@ if (navigator.mediaDevices) {
275290
/* reset & disable the F/X select/option */
276291
// toggleButton(selectFx,'off');
277292

278-
toggleButton(btnOff,'off');
293+
show(btnOn);
294+
hide(btnOff);
279295
toggleButton(btnSnap,'off');
280296

281297
toggleFxControls();
@@ -308,37 +324,40 @@ if (navigator.mediaDevices) {
308324
const vRatio = vWidth/vHeight;
309325
console.log('vRatio: ', vRatio);
310326

311-
const wWidth = window.innerWidth;
312-
const wHeight = wWidth*vRatio;
313-
// const wHeight = window.innerHeight;
327+
let wWidth = window.innerWidth;
328+
let wHeight = window.innerHeight;
314329
console.log('wWidth:'+wWidth+' / wHeight:'+wHeight);
315330

316-
const wRatio = wWidth/wHeight;
317-
console.log('wRatio: ', wRatio);
331+
// const wRatio = wWidth/wHeight;
332+
// console.log('wRatio: ', wRatio);
318333

319334
/* set canvas to W&H of window */
320-
// canvas.width = wWidth;
321-
// canvas.height = wHeight;
335+
if (vWidth > wWidth) {
336+
console.info('vWidth > wWidth');
337+
if (vHeight > wHeight) {
338+
console.info('vHeight > wHeight');
339+
// shrink horizontally...
340+
// wWidth = wHeight * vRatio;
341+
} else {
342+
console.info('vHeight <= wHeight');
343+
// enlarge vertically to fill canvas with video...
344+
// wHeight = wWidth * vRatio;
345+
}
346+
} else {
347+
console.info('vWidth <= wWidth');
348+
if (vHeight > wHeight) {
349+
console.info('vHeight > wHeight');
350+
} else {
351+
console.info('vHeight <= wHeight');
352+
}
353+
}
354+
console.log('wWidth:'+wWidth+' / wHeight:'+wHeight);
355+
canvas.width = wWidth;
356+
canvas.height = wHeight;
322357

323358
/* center the canvas */
324-
// positionX = (wWidth - vWidth)/2;
325-
// positionY = (wHeight - vHeight)/2;
326-
327-
// if (vWidth < wWidth && vHeight > wHeight) {
328-
// console.log('video taller than window');
329-
// // stretch video vertically...
330-
// canvas.width = vWidth;
331-
// canvas.height = wHeight;
332-
// } else if (vWidth > wWidth && vHeight < wHeight) {
333-
// console.log('video wider than window');
334-
// // stretch video horizontally...
335-
// canvas.width = wWidth;
336-
// canvas.height = vHeight;
337-
// } else {
338-
// console.log('video ??? than window');
339-
canvas.width = vWidth;
340-
canvas.height = vHeight;
341-
// }
359+
positionX = (wWidth - vWidth)/2;
360+
positionY = (wHeight - vHeight)/2;
342361

343362
let randoms = []; // random numbers
344363
randoms.push(Math.floor(Math.random()*25)+Math.floor(Math.random()*250));
@@ -510,7 +529,7 @@ if (navigator.mediaDevices) {
510529

511530
if (stripLen > 0) {
512531
strip.style.display = 'flex';
513-
document.querySelector('#ctrl_strip legend').innerHTML = `snapshots (${stripLen})`;
532+
document.querySelector('#ctrl_strip legend .value').innerHTML = `${stripLen}`;
514533
toggleButton(btnClearStrip,'on');
515534
// btnClearStrip.disabled = false;
516535
// btnClearStrip.classList.remove('disabled');
@@ -551,7 +570,7 @@ if (navigator.mediaDevices) {
551570
toggleButton(btnClearStrip,'off');
552571
// btnClearStrip.disabled = true;
553572
// btnClearStrip.classList.add('disabled');
554-
document.querySelector('#ctrl_strip legend').innerHTML = 'snapshots';
573+
document.querySelector('#ctrl_strip legend .value').innerHTML = '0';
555574
console.groupEnd();
556575
}
557576

@@ -617,16 +636,7 @@ if (navigator.mediaDevices) {
617636
// });
618637

619638
/* listen for change on mirror checkbox */
620-
mirror.addEventListener('change',(e)=>{
621-
// console.log('mirror: ',mirror.checked);
622-
if (mirror.checked) {
623-
console.log('FLIP IT');
624-
canvas.style.transform = 'scale(-1,1)';
625-
} else {
626-
console.log('DON\'T FLIP IT');
627-
canvas.style.transform = 'scale(1,1)';
628-
}
629-
});
639+
mirror.addEventListener('change',fMirror);
630640

631641
/* listen for change on deeper F/X select/option */
632642
inputAllSelect.forEach(btn => {
@@ -689,6 +699,6 @@ window.addEventListener('resize', fMediaQueries);
689699

690700
/* fake UI clicks to open controls */
691701
startStream();
692-
selectFx.selectedIndex = 5;
702+
selectFx.selectedIndex = 3;
693703
toggleFxControls();
694704

19--Webcam_Fun/js/utils.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/* utils.js */
22
console.log('utils.js READY!');
3-
Array.prototype.move = function (old_index, new_index) {
4-
if (new_index >= this.length) {
5-
var k = new_index - this.length;
6-
while ((k--) + 1) {
7-
this.push(undefined);
8-
}
9-
}
10-
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
11-
return this; // for testing purposes
12-
};
133

144
/* toggle button enable/disable */
155
function toggleButton(el,state) {
@@ -36,6 +26,7 @@ function toggleButton(el,state) {
3626

3727
/* hide element by adding "hide" class */
3828
function hide(el,exception) {
29+
// console.dir(el);
3930
if (el.length > 1) {
4031
for (var i = 0; i < el.length; i++) {
4132
el[i].classList.add('hide');
@@ -50,6 +41,7 @@ function hide(el,exception) {
5041

5142
/* show element by removing "hide" class */
5243
function show(el,exception) {
44+
// console.dir(el);
5345
if (el.length > 1) {
5446
for (var i = 0; i < el.length; i++) {
5547
el[i].classList.remove('hide');

0 commit comments

Comments
 (0)