如何旋转HTML5画布上的现有内容?

9
有没有一种方法可以通过JavaScript旋转HTML5画布的现有内容?我知道可以旋转将要绘制到画布上的图像,但我想旋转已经绘制在画布上的内容,例如400x400画布的200x200角落或任何特定区域的内容。
同样的问题也适用于缩放现有画布内容...
我知道getImageData/putImageData提供了一个潜在的转换像素数组的方法,但它太慢且效率低下。
2个回答

18

使用临时画布很容易实现。

Live Demo

Live Demo Animated(只是为了好玩)

以上示例绘制了两个矩形,然后从0,0旋转和缩放到200,200。

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

canvas.width = canvas.height = 400;

// fill the canvas black, and draw 2 boxes
ctx.fillStyle = "#000";
ctx.fillRect(0,0,400,400);
ctx.fillStyle = "rgb(255,0,0)";

ctx.fillRect(10,10,190,190);
ctx.fillStyle = "rgb(255,255,0)";
ctx.fillRect(250,250,90,90);

// Create a temp canvas to store our data (because we need to clear the other box after rotation.
var tempCanvas = document.createElement("canvas"),
    tempCtx = tempCanvas.getContext("2d");

tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
// put our data onto the temp canvas
tempCtx.drawImage(canvas,0,0,canvas.width,canvas.height);

// Append for debugging purposes, just to show what the canvas did look like before the transforms.
document.body.appendChild(tempCanvas);

// Now clear the portion to rotate.
ctx.fillStyle = "#000";
ctx.fillRect(0,0,200,200);
ctx.save();
// Translate (190/2 is half of the box we drew)
ctx.translate(190/2, 0);
// Scale
ctx.scale(0.5,0.5);  
// Rotate it
ctx.rotate(45*Math.PI/180);
// Finally draw the image data from the temp canvas.
ctx.drawImage(tempCanvas,0,0,200,200,10,10,190,190);
ctx.restore();

5

如果您想先在 canvas 上绘制图形,然后旋转它以便在边角上使用,您可以通过“克隆”画布或使用 CSS 来实现。

示例

获取第一个 canvas 元素:

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

在它上面绘制:

ctx.fillStyle = 'blue';
ctx.fillRect(0,0, 25, 5);
ctx.fill();
ctx.fillStyle = 'red';
ctx.fillRect(25, 0, 25, 5);
ctx.fill();

将其克隆到另一个通过CSS旋转的画布中:

var ctx2 = document.getElementById("canvas2").getContext("2d");
ctx2.drawImage(canvas, 0,0);

或者在"克隆"画布时旋转它:

var ctx3 = document.getElementById("canvas3").getContext("2d");
ctx3.rotate(Math.PI/2);
ctx3.translate(0,-50);
ctx3.drawImage(canvas, 0,0);

以下是旋转的CSS:

#canvas2 {
    -webkit-transform:rotate(90deg);
    -moz-transform:rotate(90deg);
    -o-transform:rotate(90deg);
    -ms-transform:rotate(90deg);
}

这里是完整的例子:

输入图像描述

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function() {
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");

    ctx.fillStyle = 'blue';
    ctx.fillRect(0,0, 25, 5);
    ctx.fill();
    ctx.fillStyle = 'red';
    ctx.fillRect(25, 0, 25, 5);
    ctx.fill();

    var ctx2 = document.getElementById("canvas2").getContext("2d");
    ctx2.drawImage(canvas, 0,0);

    var ctx3 = document.getElementById("canvas3").getContext("2d");
    ctx3.rotate(Math.PI/2);
    ctx3.translate(0,-50);
    ctx3.drawImage(canvas, 0,0);

}
</script>
<style>
#canvas2 {
    -webkit-transform:rotate(90deg);
    -moz-transform:rotate(90deg);
    -o-transform:rotate(90deg);
    -ms-transform:rotate(90deg);
}
</style>
</head>
<body>
<canvas id="canvas" width="50" height="50"></canvas>
<canvas id="canvas2" width="50" height="50"></canvas>
<canvas id="canvas3" width="50" height="50"></canvas>
</body>
</html>

1
我认为这不是OP真正想要的,他想要旋转和缩放已经绘制在画布上的一部分,并将其放回到现有画布中。此外,他想要使用JS而不是CSS进行操作。您的方法存在问题,现在旋转画布上的所有内容都会被旋转。 - Loktar
4
谢谢!我想旋转整张图片,这对我非常关键! - Dameo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接