gpt4 book ai didi

javascript - 旋转图像并在 Canvas 上绘图

转载 作者:行者123 更新时间:2023-11-28 04:07:41 25 4
gpt4 key购买 nike

我一直在尝试不同的方法,在尺寸为 1080*1920 的 Canvas 上从尺寸为 1920*1080 的视频流中绘制图像。我想将视频旋转 -90°,使其完全适合 Canvas (不通过缩小和扩展来调整原始视频的大小)。以下是我尝试过的:

canvas = document.createElement('canvas');
canvas.height = 1920;
canvas.width = 1080;
context = canvas.getContext('2d');
context.drawImage(videoSrc, 0, 0, 1080, 1920, 1080, -1920, 1080, 1920);
// I have been trying different combinations of above numbers
return canvas;

我没有使用 context.rotate() 或任何其他翻译方法,因为我不想旋转 Canvas ,而只是从 Canvas 中的不同位置开始绘制视频。也许我错了。感谢您的阅读

最佳答案

在绘制图像之前,您需要在上下文中调用 rotate()。就像这样:

canvas = document.createElement('canvas');
canvas.height = 1920;
canvas.width = 1080;

//Angle to rotate, negative for counter clockwise
degrees = 45;

context = canvas.getContext('2d');

//Rotation calculated in radians (hence the math).
context.rotate(degrees * Math.PI / 180);

context.drawImage(videoSrc, 0, 0, 1080, 1920, 1080, -1920, 1080, 1920);

//Reset rotation
context.resetTransform();

return canvas;

关于javascript - 旋转图像并在 Canvas 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46547883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com