旋转图像但不旋转画布

Puj*_*ava 8 html javascript css jquery html5-canvas

我在画布中使用背景图像.我想仅将背景图像旋转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)'});
});
Run Code Online (Sandbox Code Playgroud)
#canvas{
  background: #789;
  border: 1px solid;
}
body{
  margin:10px;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Kii*_*a09 3

请参阅此处的小提琴。

我创建了一个container与 canvas 元素大小相同的 div,然后在该containerdiv 上添加了一个伪元素,该元素将用作旋转背景。我认为我们不能在画布本身上使用伪元素,因为画布仅在不支持时显示其内容。