【问题标题】:Javascript Camera toggle not togglingJavascript相机切换不切换
【发布时间】:2018-04-18 03:41:58
【问题描述】:

我希望将相机 (facingMode) 从 user 切换到 environment。切换似乎在front 从真到假切换的意义上起作用。但是,constraints 中的 facingMode 仍然是 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") } };

【问题讨论】:

    标签: javascript ios camera getusermedia


    【解决方案1】:

    您在代码笔中重复了这行代码两次,一次在顶部,一次在底部:

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

    另一个问题是原语被引用。如果您有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
    };
    

    【讨论】:

    • 很好的重复。我实现了一条线来让对象自行更新,但它似乎有时只能工作。这真的很奇怪。 front 切换应该从真/假切换,但 facingMode 有时只会切换,并且经常卡在 userenvironment 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多