【问题标题】:How to flip the webcam video on canvas without inverting the texts written in the canvas如何在画布上翻转网络摄像头视频而不反转画布中写的文本
【发布时间】:2021-12-13 07:23:53
【问题描述】:

我使用ctx.translate()ctx.scale() 翻转了一个网络摄像头流以使其镜像,但它也翻转了画布上绘制的所有其他内容。有没有办法翻转网络摄像头视频而不是画布上的其他绘图(如文本)?

这是我目前正在使用的代码(摘录):

const setupcamera = () => {
  navigator.mediaDevices
    .getUserMedia({
      video: { width: 600, height: 400 },
      audio: false,
    })
    .then((stream) => {
      video.srcObject = stream;
    });
};

const detectpose = async () => {
  const poses = await detector.estimatePoses(video, false);

  console.log(poses);
  ctx.drawImage(video, 0, 0, 600, 400);

  ctx.font = "30px Comic Sans MS";
    ctx.fillStyle = "green";
    ctx.fillText(po, elbowx, elbowy);

setupcamera();

video.addEventListener("loadeddata", async () => {
  ctx.translate(600, 0);
  ctx.scale(-1, 1);
  detector = await poseDetection.createDetector(
    poseDetection.SupportedModels.MoveNet
  );
  setInterval(detectpose, 20);
});

我想过在翻转的画布上画一个新画布,但如果可能的话,我想在同一个画布上画出所有东西。

【问题讨论】:

  • 您能否分享您在画布上绘制网络摄像头的代码以及在其上绘制文本的代码?
  • 是的,我编辑了代码并添加了相关部分。如果有任何混淆,请告诉我。
  • 是否可以在画布上方放一个div,然后将文字设置为div
  • @TheKNVB 好主意没想到。会尝试

标签: javascript html5-canvas draw


【解决方案1】:

您需要在绘图期间更新上下文的矩阵变换。
首先缩放和翻译上下文,然后绘制视频,在此处重置上下文矩阵,最后绘制文本。

// using a video file because SO can't use gUM
const video = document.createElement("video");
video.muted = true;
video.crossOrigin = "anonymous";
video.src = "https://upload.wikimedia.org/wikipedia/commons/2/22/Volcano_Lava_Sample.webm";

// when available video.requestVideoFrameCallback is the best
const requestFrame = (cb) => {
  if(video.requestVideoFrameCallback) {
    return video.requestVideoFrameCallback(cb);
  }
  return requestAnimationFrame(cb);
};
video.play().then(() => {
  const canvas = document.querySelector("canvas");
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  const ctx = canvas.getContext("2d");
  ctx.font = "800 90px sans-serif";
  ctx.textAlign = "center";
  ctx.textBaseline = "middle";
  ctx.fillStyle = "green";
  
  requestFrame(draw);

  function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // vertical-flip
    ctx.scale(1, -1);
    ctx.translate(0, -canvas.height);
    ctx.drawImage(video, 0, 0);
    // reset the transform-matrix
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.fillText("Some text", canvas.width / 2, canvas.height / 2);
    requestFrame(draw);
  }
}).catch(console.error);
canvas { max-height: 100vh }
<canvas></canvas>

【讨论】:

  • 谢谢。我会试试的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2021-09-20
相关资源
最近更新 更多