您当前的位置: 首页 > 前端教程 > HTML教程 > 任意旋转图片的js实例

任意旋转图片的js实例

作者:不详 来源:网络 发布时间: 2011-12-21 14:12 点击:
Html代码 html head meta http-equiv=Content-Type content=text/html;charset=utf-8 title任意旋转图片/title script type=text/javascript src=coos.ui.rotate.js/script /head body div id=innerimg id=theimage border=0 src=logo.jpg//div div style=position:abs

任意旋转图片的js实例

  Html代码
  
  <html>
  
  <head>
  
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  
  <title>任意旋转图片</title>
  
  <script type="text/javascript" src="coos.ui.rotate.js"></script>
  
  </head>
  
  <body>
  
  <div id="inner"><img id="theimage" border="0" src="logo.jpg"/></div>
  
  <div style="position:absolute;top:235px;height:40px;">
  
  <input type="button" value="<-Rotate逆时针" name="RotateL" id="RotateL" onclick="rotateRight('theimage',90);">
  
  <input type="button" value="顺时针Rotate->" name="RotateR" id="RotateR" onclick="rotateLeft('theimage',90);">
  
  <input type="button" value="顺时针270" name="RotateR" id="RotateR" onclick="rotateLeft('theimage',270);">
  
  </div>
  
  </body>
  
  </html>
  
  Js代码
  
  function rotate(id,angle,whence){
  
  var p=document.getElementById(id);
  
  //we store the angle inside the image tag for persistence
  
  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;
  
  }
  
  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);
  
  }

分享到:
本文"任意旋转图片的js实例"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4