我有两个点 (x1, x2, y1, y2)。如何在画布上绘制一个图像(宽度为10,高度为100的矩形),其起始点为x1,y1,并且旋转角度由连接这两个点之间的斜率确定?
就像我想要将这条线与一张图片重叠:
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
我试过这样做:
slope = (y2 - y1) / (x2 - x1)
ctx.save();
ctx.rotate(-Math.atan(slope));
ctx.drawImage(image, x1, y1);
ctx.restore();
但是没有成功。
谢谢。