javascript - 如何在画布上翻转网络摄像头视频而不反转画布中写的文本
问题描述
我翻转了一个网络摄像头流以使其镜像使用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);
});
我想在翻转的画布上画一个新的画布,但如果可能的话,我想在同一个画布上画出所有东西。
解决方案
您需要在绘图期间更新上下文的矩阵变换。
首先缩放和翻译上下文,然后绘制视频,在此处重置上下文矩阵,然后最终绘制文本。
// 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>
推荐阅读
- php - wordpress插件下载监控器将我们通过它上传的数据存储在哪里?
- node.js - mongoose count 查询耗时过长,需要减少时间
- css - 在 React 中更改字体大小而不遇到最大“超出最大更新深度”
- angular - Angular 编译器和入口组件
- angularjs - 从工厂传播范围事件
- node.js - 节点中的日期格式以生成pdf
- javascript - Vue.js - 单击按钮时在文本区域中淡入/淡出文本
- android - 如何创建像 Android 输入一样的托盘形底部边框?
- java - Android RadioButton 仅显示文本,而不是设备模拟器中的实际按钮
- python - 每当使用python本地更新到100个开发人员时,如何从远程服务器实时流式传输大型日志文件