【问题标题】:Unable to draw on canvas at the correct position when canvas dimensions are not known in advance当CANVAS尺寸未提前知道时,无法在正确的位置绘制画布
【发布时间】:2020-04-26 16:03:45
【问题描述】:

我们需要在视频元素上使用鼠标进行绘制。我正在使用 Canvas 来渲染绘图。当我们事先不知道视频的宽度和高度时,我无法在正确的位置画线。它被绘制在不同的位置。下面是代码。

HTML

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="/css/main.css" />
</head>

<body>

  <h1>Realtime communication with WebRTC</h1>

  <div id="videos">
    <video id="localVideo" autoplay muted></video>
    <video id="remoteVideo" autoplay></video>
  </div>

  <canvas id="localCanvas"></canvas>

  <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  <script src="js/main.js"></script>

</body>
</html>

ma​​in.css

body {
  font-family: sans-serif;
}

video {
  max-width: 100%;
  width: 320px;
}

canvas {
  position: absolute;
}

ma​​in.js

'use strict';
var localVideo = document.querySelector('#localVideo');

navigator.mediaDevices.getUserMedia({
  audio: false,
  video: true
})
.then(gotStream)
.catch(function(e) {
  alert('getUserMedia() error: ' + e.name);
});

function gotStream(stream) {
  // Checking Whether video is ready to set canvas width and height
  var b = setInterval(() => {
    if(localVideo.readyState >= 3){
          document.getElementById('localCanvas').style.width = document.querySelector('#localVideo').offsetWidth + "px";
          document.getElementById('localCanvas').style.height = document.querySelector('#localVideo').offsetHeight + "px";
          clearInterval(b);
        }
    },500);

  localVideo.srcObject = stream;
}

document.addEventListener("DOMContentLoaded", function(){
  let xPos = window.scrollX + document.querySelector('#localVideo').getBoundingClientRect().left;
  let yPos = window.scrollY + document.querySelector('#localVideo').getBoundingClientRect().top;

  document.getElementById('localCanvas').style.left = xPos + "px";
  document.getElementById('localCanvas').style.top = yPos + "px";

  document.getElementById('localCanvas').style.width = document.querySelector('#localVideo').offsetWidth + "px";
  document.getElementById('localCanvas').style.height = document.querySelector('#localVideo').offsetHeight + "px";

  InitThis();
});

var mousePressed = false;
var lastX, lastY;
var ctx;

function InitThis() {
    ctx = document.getElementById('localCanvas').getContext("2d");
    const canvas = document.querySelector('#localCanvas');

    $('#localCanvas').mousedown(function (e) {
        mousePressed = true;
        Draw(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop, false);
    });

    $('#localCanvas').mousemove(function (e) {
        if (mousePressed) {
            Draw(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop, true);
        }
    });

    $('#localCanvas').mouseup(function (e) {
        mousePressed = false;
    });
        $('#localCanvas').mouseleave(function (e) {
        mousePressed = false;
    });
}

function Draw(x, y, isDown) {
    if (isDown) {
        ctx.beginPath();
        ctx.strokeStyle = "blue";
        ctx.lineWidth = 9;
        ctx.lineJoin = "round";
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(x, y);
        ctx.closePath();
        ctx.stroke();
    }
    lastX = x; lastY = y;
}

这里我们只是在 CSS 中设置最大宽度和宽度属性,我们事先并不知道高度。所以在onload事件监听函数的时候,我是根据localVideo来设置画布的宽高坐标的。但到那时视频高度为 160 像素。但是当加载视频时,高度变为 240 像素。同样,当视频状态准备好播放时,我正在更改 Canvas 的宽度和高度,如下所示在 gotStream() 中。

var b = setInterval(() => {
        if(localVideo.readyState >= 3){
              document.getElementById('localCanvas').style.width = document.querySelector('#localVideo').offsetWidth + "px";
              document.getElementById('localCanvas').style.height = document.querySelector('#localVideo').offsetHeight + "px";
              clearInterval(b);
            }
        },500);

视频准备好播放后,画布大小与 localVideo 的大小完全相同。但是当我尝试在画布上使用鼠标绘制时,它被绘制在不正确的位置,特别是当我试图在画布的下半部分或右边缘绘制时。谁能帮我解决这个问题。

更新:我在 onload 事件中将 Canvas 的高度硬编码为“240px”,这也不起作用。

【问题讨论】:

    标签: javascript html css html5-canvas html5-video


    【解决方案1】:

    画布被拉伸是因为宽度和高度被设置为样式而不是原始属性。通过以下代码修复了问题。

    document.getElementById('localCanvas').setAttribute("width", document.querySelector('#localVideo').offsetWidth;
    document.getElementById('localCanvas').setAttribute("height", document.querySelector('#localVideo').offsetHeight;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-14
      • 2012-05-11
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 2020-08-09
      相关资源
      最近更新 更多