Javascript相机切换不切换

时间:2018-04-18 03:41:58

标签: javascript ios camera getusermedia

我希望将相机(facingMode)从user切换到environment。切换似乎在front从true切换到false的意义上起作用。但是,facingMode中的constraints仍然是environment

我在俯瞰什么?

Codepen:https://codepen.io/abenjamin/pen/qYWeXj?editors=1111

var front = false;
document.getElementById('cameraToggle').onclick = function() { front = !front; console.log(constraints) };

var constraints = { video: { facingMode: (front? "user" : "environment") } };

1 个答案:

答案 0 :(得分:1)

你在codepen中重复了这一行,一次在顶部,一次在底部:

document.getElementById('cameraToggle').onclick = function() { front = !front; console.log(constraints) };

另一个问题是原语由 value 引用。如果您有let x = 3; const obj = { x }; x = 4;,则对象不会更改 - 它将继续为{ x: 3 }

您需要做的是更新约束对象本身,然后继续执行您对变异对象所需的任何操作。例如:

document.getElementById('cameraToggle').onclick = function() {
  front = !front;
  constraints.video.facingMode = front
    ? "user"
    : "environment";
  // interact with the mutated `constraints` object if needed
};