【发布时间】:2023-03-22 15:30:01
【问题描述】:
我想旋转图像,但根据图像的宽度和高度调整画布的大小。请在JSFiddle 中查看我的代码。我实际上是在基于画布旋转图像,但我的画布具有固定的高度和宽度,因此图像只是在内部旋转。我只想要这样的东西。请检查我随附的屏幕截图。绿色边框是画布,所以如果我旋转 90 度,那么画布应该扩大高度并显示整个图像。
你能帮忙吗?
HTML
<canvas id="canvas" ></canvas><br>
<button id="clockwise">Rotate right</button>
JavaScript
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var degrees=0;
var image=document.createElement("img");
image.onload=function(){
ctx.drawImage(image,0,0,image.width,image.height);
}
image.src="http://www.ajaxblender.com/article-sources/images/plane-1.jpg";
$("#clockwise").click(function(){
degrees+=90
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.save();
ctx.translate(image.width/2,image.height/2);
ctx.rotate(degrees*Math.PI/180);
ctx.drawImage(image,0,0,image.width,image.height,-image.width/2,-image.height /2,image.width,image.height);
ctx.restore();
});
【问题讨论】:
标签: javascript jquery html5-canvas image-rotation