图片旋转的小例子

记事本 2010-08-05 10:00:54 累计浏览 3,763 次
<script type="text/javascript"> 
var c1 = document.getElementById("c1"); 
 ctx = c1.getContext("2d"), 
 image = document.createElement("IMG"); 
image.onload = function() { 
 c1.width = image.height; 
 c1.height = image.width; 
 ctx.translate(0, image.width); 
 ctx.rotate(270*Math.PI/180); 
 ctx.drawImage(image, 0, 0); 
} 
image.src = "http://img1.cache.netease.com/img09/logo/logo_v1.gif"; 
</script>

IE应该用滤镜实现同样的效果。

rotate()这个函数接收的是弧度值。角度乘以0.017(2π/360)可以转变为弧度。

图片转转转

<!DOCTYPE html> 
<html> 
<head> 
 <title>Demo</title> 
 <style type="text/css"> 
  html, body { 
   margin: 0; 
   padding: 0; 
  } 
 </style> 
</head> 
<body> 
 <canvas id="c1"></canvas> 
 <script type="text/javascript"> 
 var image = document.createElement("IMG"); 
 image.onload = function() { 
  var c1 = document.getElementById("c1"), rotate = null, 
   len = Math.sqrt(Math.pow(image.width, 2) + Math.pow(image.height, 2)), 
   center = {x: len / 2, y: len / 2}; 
  //判断是否为IE 
  if(/*@cc_on!@*/0) { 
   (function() { 
    var div = document.createElement("DIV"); 
    div.style.position = "relative"; 
    div.style.marginTop = div.style.marginLeft = len / 2 + "px"; 
    div.appendChild(image); 
    c1.parentNode.insertBefore(div, c1); 
    c1.parentNode.removeChild(c1); 
    c1 = null; 
    image.style.position = "absolute"; 
    //设置滤镜 
    image.style.filter = "progid:DXImageTransform.Microsoft.Matrix()"; 
    var filter = image.filters.item(0); 
    filter.SizingMethod = "auto expand"; 
    filter.FilterType = "bilinear";  
    rotate = function(rad){ 
     var costheta = Math.cos(rad), 
      sintheta = Math.sin(rad); 
     filter.M11 = filter.M22 = costheta; 
     filter.M12 = -(filter.M21 = sintheta); 
     //将图片的重心调节到旋转点。 
     image.style.top = (-image.offsetHeight) / 2 + ’px’; 
     image.style.left = (-image.offsetWidth) / 2 + ’px’; 
    } 
   })(); 
  } else { 
   (function() { 
    var ctx = c1.getContext("2d"); 
    rotate = function(rad){ 
     c1.width = c1.height = len; 
     ctx.translate(center.x, center.y); 
     ctx.rotate(rad); 
     //绘制图片,并将图片的重心调节到旋转点。 
     ctx.drawImage(image, -image.width / 2, -image.height / 2); 
    } 
   })(); 
  }   
  //开始旋转 
  (function() { 
   if(rotate) { 
    var angle = 0; 
    setInterval(function() { 
     var rad = ((angle++)*Math.PI / 180) % 360; 
     rotate(rad) 
    }, 10); 
   } 
  })(); 
 } 
 image.src = "http://img1.cache.netease.com/img09/logo/logo_v1.gif"; 
 </script> 
</body> 
</html>

点击查看DEMO

同分类推荐文章

  1. Embracing Playful Imperfection With The CSS `random()` Function (2026-11-14 03:10:02)
  2. Document Picture-in-Picture API与任意元素画中画实现 (2026-11-13 17:10:02)
  3. 给博客做个站内全文搜索:纯前端离线倒排索引的设计与取舍 (2026-10-03 16:40:31)

查看更多 前端 文章 →

建议继续学习

  1. JQuery实现Excel表格呈现 (累计阅读 48,734)
  2. 50个活力和动感的网页设计-颜色的灵感 (累计阅读 34,822)
  3. 深入理解Javascript之执行上下文(Execution Context) (累计阅读 18,923)
  4. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 16,397)
  5. 图片动态局部毛玻璃模糊效果的实现 (累计阅读 15,180)
  6. 天朝第二代身份证号码的验证机制 (累计阅读 15,109)
  7. HTML 5 的data-* 自定义属性 (累计阅读 14,741)
  8. 分享一个JQUERY颜色选择插件 (累计阅读 14,550)
  9. 什么是全栈工程师? (累计阅读 14,414)
  10. 视觉设计前瞻实用性研究(PNVD) 第二期 (累计阅读 13,328)