简体   繁体   English

平移和旋转后 canvas 上的 getImageData

[英]getImageData on canvas after translation and rotation

I'm trying to extract a part of an image through the getImageData function for canvas.我正在尝试通过 getImageData function 为 canvas 提取图像的一部分。

The difficulty is that my part is rotated.困难在于我的部分是旋转的。 My goal is to extract for example the rectangle on the image:我的目标是提取例如图像上的矩形: 在此处输入图像描述

To draw the rectangle below, I use a translation and a rotation, but these functions don't work when we use getImageData.为了绘制下面的矩形,我使用了平移和旋转,但是当我们使用 getImageData 时这些函数不起作用。

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var img = new Image();
img.onload = function () {
    ctx.drawImage(img, 0, 0, img.width, img.height);
drawRect();
}
img.src = "https://dummyimage.com/300x150/5873f0/ffffff.jpg"; 


function drawRect(){
    ctx.save();
    ctx.translate(75, 100);
    ctx.rotate(20 * Math.PI / 180)
    // imgData = ctx.getImageData(-50, -25, 100, 50);
    ctx.strokeStyle = 'black';
    ctx.strokeRect(-50, -25, 100, 50);
    ctx.restore()
}

This is the Fiddle I use.这是我使用的小提琴 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM