HTML5 Canvas 旋转图像

标签 html image canvas rotation html5-canvas

jQuery('#carregar').click(function() {
  var canvas    = document.getElementById('canvas');
  var image   = document.getElementById('image');
  var element = canvas.getContext("2d");
  element.clearRect(0, 0, canvas.width, canvas.height);
  element.drawImage(image, 0, 0, 300, 300);
});

jsfiddle.net/braziel/nWyDE/

我无法将图像向右或向左旋转 90°。

我在 Canvas 上使用了一张图片,同一个屏幕会有几个与示例相同的 Canvas ,但我把它放在尽可能靠近项目的地方。

请问,点击“向左旋转”和“向右旋转”时,如何将图片向左或向右旋转90°?

我在互联网上尝试了几个代码,但没有一个有效。

最佳答案

您可以使用 Canvas 的 context.translate 和 context.rotate 来旋转您的图像

enter image description here

下面是一个函数,用于绘制按指定度数旋转的图像:

function drawRotated(degrees){
    context.clearRect(0,0,canvas.width,canvas.height);

    // save the unrotated context of the canvas so we can restore it later
    // the alternative is to untranslate & unrotate after drawing
    context.save();

    // move to the center of the canvas
    context.translate(canvas.width/2,canvas.height/2);

    // rotate the canvas to the specified degrees
    context.rotate(degrees*Math.PI/180);

    // draw the image
    // since the context is rotated, the image will be rotated also
    context.drawImage(image,-image.width/2,-image.width/2);

    // we’re done with the rotating so restore the unrotated context
    context.restore();
}

这是代码和 fiddle :http://jsfiddle.net/m1erickson/6ZsCz/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

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

    var angleInDegrees=0;

    var image=document.createElement("img");
    image.onload=function(){
        ctx.drawImage(image,canvas.width/2-image.width/2,canvas.height/2-image.width/2);
    }
    image.src="houseicon.png";

    $("#clockwise").click(function(){ 
        angleInDegrees+=30;
        drawRotated(angleInDegrees);
    });

    $("#counterclockwise").click(function(){ 
        angleInDegrees-=30;
        drawRotated(angleInDegrees);
    });

    function drawRotated(degrees){
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.save();
        ctx.translate(canvas.width/2,canvas.height/2);
        ctx.rotate(degrees*Math.PI/180);
        ctx.drawImage(image,-image.width/2,-image.width/2);
        ctx.restore();
    }


}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas><br>
    <button id="clockwise">Rotate right</button>
    <button id="counterclockwise">Rotate left</button>
</body>
</html>

关于HTML5 Canvas 旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17411991/

相关文章:

c# - 你如何在 C# 中为 Canvas 上的线条设置动画?

javascript - 使用 for 循环动态创建在 Canvas 中绘制的图像

javascript - 访问另一个 JS 文件中的数组 - Chrome 扩展

c++ - 使用ffmpeg将平面RGB图像组合成无损视频

java - 在 Web 应用程序中显示来自本地计算机的 JFree 图表图像

python - 在 Python 中检索 .dpx 文件的分辨率

html - 禁用 html5 Canvas

html - 不同缩放级别的定位表单

宽度为 :100% ignores margin-right:200px 的 html 表格

html - 更改标题字体大小会使 div 不对齐