首页 > 代码库 > js图片旋转

js图片旋转

  <script type="text/javascript" language="javascript">
    function rotate(id, angle, whence) {
      var p = document.getElementById(id);
      if (!whence) {
        p.angle = ((p.angle == undefined ? 0 : p.angle) + angle) % 360;
      }
      else {
        p.angle = angle;
      }
      if (p.angle >= 0) {
        var rotation = Math.PI * p.angle / 180;
      }
      else {
        var rotation = Math.PI * (360 + p.angle) / 180;
      }
      var costheta = Math.cos(rotation);
      var sintheta = Math.sin(rotation);
      if (document.all && !window.opera) {
        var canvas = document.createElement(‘img‘);
        canvas.src = p.src;
        canvas.height = p.height;
        canvas.width = p.width;
        canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11=" + costheta + ",M12=" + (-sintheta) + ",M21=" + sintheta + ",M22=" + costheta + ",SizingMethod=‘auto expand‘)";
      }
      else {
        var canvas = document.createElement(‘canvas‘);
        if (!p.oImage) {
          canvas.oImage = new Image();
          canvas.oImage.src = p.src;
        }
        else {
          canvas.oImage = p.oImage;
        }
        //alert(canvas.width)
        canvas.style.width = canvas.width = Math.abs(costheta * canvas.oImage.width) + Math.abs(sintheta * canvas.oImage.height);
        canvas.style.height = canvas.height = Math.abs(costheta * canvas.oImage.height) + Math.abs(sintheta * canvas.oImage.width);
        var context = canvas.getContext(‘2d‘);
        context.save();
        if (rotation <= Math.PI / 2) {
          context.translate(sintheta * canvas.oImage.height, 0);
        }
        else if (rotation <= Math.PI) {
          context.translate(canvas.width, -costheta * canvas.oImage.height);
        }
        else if (rotation <= 1.5 * Math.PI) {
          context.translate(-costheta * canvas.oImage.width, canvas.height);
        }
        else {
          context.translate(0, -sintheta * canvas.oImage.width);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
        context.restore();
      }
      canvas.id = p.id;
      canvas.angle = p.angle;
      p.parentNode.replaceChild(canvas, p);
    }

    //向右旋转
    function rotateRight(id, angle) {
      rotate(id, angle == undefined ? 90 : angle);
    }
    //向左旋转
    function rotateLeft(id, angle) {
      rotate(id, angle == undefined ? -90 : -angle);
    }
  </script>

调用方法:

<div class="content">
  <div class="data">
    <a href="javascript:void(0)" style="font-weight: bold; text-decoration: none; font-size: 16px;"
      onclick="rotateRight(‘theimage‘,90);">右转</a> 
<a href="javascript:void(0)" style="font-weight: bold; font-size: 16px; text-decoration: none;"
onclick
="rotateLeft(‘theimage‘,90);">左转</a><br>
<img src="a.jpg" id="theimage" onl oad="resizeimg(this,this.width,this.height)" width="100%" alt="800×1422" style="cursor: pointer;" /> </a> <div style="clear: both"> </div> </div> </div>