【发布时间】:2015-10-01 07:26:24
【问题描述】:
我在画布中使用背景图像。我只想将背景图像旋转 90 度,但不应旋转上下文。如果我使用 css 变换,那么整个画布都会旋转。我该怎么办?
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
ctx.rect(20, 30, 200, 40);
ctx.strokeStyle="red";
ctx.lineWidth="2";
ctx.stroke();
$(function(){
//$('#canvas').css({'transform':'rotate(90deg)'});
});
#canvas{
background: #789;
border: 1px solid;
}
body{
margin:10px;
}
<canvas id="canvas" width="400" height="300" style="background-image: url(http://placehold.it/1600x800); background-size: 100%; background-position: -80px -50px;">Your browser does not support HTML5 Canvas</canvas>
【问题讨论】:
-
您是否尝试过使用旋转 JQuery code.google.com/p/jquery-rotate 旋转图像?
-
我刚刚按照建议尝试过,但没有成功。
-
这可能有点脏,但您可以尝试将画布移动到中,旋转该 div,为其提供背景图像,然后向相反方向旋转画布。其实这会让整个应用变得非常复杂,动态元素很多,很遗憾我避免触摸画布部分的任何东西。@KiiroSora09 提出了一个可行的解决方案,但如果您有许多动态元素,您可能应该考虑使用一个画布库来为您处理旋转的背景图像等内容。
标签: javascript jquery html css html5-canvas