[英]p5.js | How can I change the size of the canvas?
The code below wants to achieve the following:下面的代码想要实现以下目标:
<video>
with a live video capture<video>
<canvas>
of that video capture<canvas>
<img>
on the page.<img>
附加到页面上。 let video
let canvasSize = 200
let PAUSE_DRAW = false
function setup() {
createCanvas(canvasSize, canvasSize)
video = createCapture(VIDEO)
video.size(canvasSize, canvasSize)
}
async function draw() {
if (video && video.loadedmetadata) {
if (!PAUSE_DRAW) {
// get a 200x200 from the center of the video and draw it on the canvas
image(video.get(), 0, 0, canvasSize, canvasSize, x, y, canvasSize, canvasSize)
let currentFrameAsBase64 = canvas.toDataURL()
// append <img> with current frame as src
document.getElementById('frame').src = currentFrameAsBase64
PAUSE_DRAW = true
}
}
}
Here is the problem I'm having:这是我遇到的问题:
The <canvas>
actual size is still 400x400. <canvas>
实际尺寸仍然是 400x400。 It's only the image that I draw inside it that's 200x200.只有我在其中绘制的图像是 200x200。 As a result, when I get the base64 of the canvas and append it as an
<img>
, the img is also 400x400.结果,当我获取画布的 base64 并将其附加为
<img>
,img 也是 400x400。 But I want the <canvas>
and the <img>
to be 200x200 as well.但我希望
<canvas>
和<img>
也是 200x200。
How do I fix this ?我该如何解决 ?
You have not provided your HTML code, which is necessary.您没有提供您的 HTML 代码,这是必要的。 But if I understand you correctly, the video is not the size of the canvas.
但如果我理解正确的话,视频不是画布的大小。
The canvas must have its size set in both the styles and attributes, so this is incorrect:画布的大小必须在样式和属性中设置,所以这是不正确的:
<style>
canvas {
height: 400px;
width: 400px;
}
</style>
<canvas></canvas>
And this is correct:这是正确的:
<style>
canvas {
height: 400px;
width: 400px;
}
</style>
<canvas width="400px" height="400px"></canvas>
Had to change setup() to:必须将 setup() 更改为:
function setup() {
createCanvas(canvasSize, canvasSize)
video = createCapture(VIDEO)
video.size(canvasSize, canvasSize)
video.elt.height = canvasSize
video.elt.width = canvasSize
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.