-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
81 lines (74 loc) · 1.83 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let capture;
let devices = [];
let mirrorFlag = false;
let deviceID = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
setupCamera();
}
function draw() {
background(0);
if (typeof (capture.loadedmetadata) != "undefined" && capture.loadedmetadata) {
if (mirrorFlag) {
push();
translate(windowWidth,0)
scale(-1, 1)
image(capture, 0, 0, windowWidth, width * capture.height / capture.width);
pop();
} else {
image(capture, 0, 0, windowWidth, width * capture.height / capture.width);
}
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function setupCamera() {
devices = [];
navigator.mediaDevices.enumerateDevices()
.then(gotDevices);
}
function gotDevices(deviceInfos) {
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
if (deviceInfo.kind == 'videoinput') {
devices.push({
label: deviceInfo.label,
id: deviceInfo.deviceId
});
}
}
console.log(devices);
let supportedConstraints = navigator.mediaDevices.getSupportedConstraints();
console.log(supportedConstraints);
var constraints = {
video: {
deviceId: {
exact: devices[deviceID].id
},
}
};
capture = createCapture(constraints);
}
function mouseClicked() {
setupContextmenu()
}
function keyPressed() {
if (keyCode == ENTER) {
mirrorFlag = !mirrorFlag;
}
}
function setupContextmenu() {
let text;
let deviceList = "All cameras:";
for (let i = 0; i !== devices.length; ++i) {
deviceList = deviceList + "\n id: " + i + " label: " + devices[i].label;
}
let setID = prompt(deviceList + "\n \n Enter the desired camera ID", "0");
if (setID == null || setID == "") {
console.log("User cancelled the prompt.");
} else {
deviceID = setID
setupCamera();
}
}