• 这是一个CSS3效果,IE9以下用户看不到效果,推荐使用火狐和Chrome浏览器,使用translate——移动、rotate ——旋转、scale——缩放实现的功能,对于transform方法的使用,其实很难把握,所以写了这个演示,或许对你有帮助。

点击(此处)折叠或打开

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='UTF-8' />
  5. <title>河北草坪</title>
  6. <style>
  7. #rule,#pad{float:left;margin:10px;}
  8. #rule p{padding;0;margin:0;line-height:25px;}
  9. #rule label{display:inline-block;width:80px;text-align:right;font-size:12px;}
  10. #rule input{border:0;width:40px;}
  11. #rule b{position:relative;display:inline-block;width:150px;height:10px;font-size:0;line-height:0;background:#fff;border:1px solid #aaa;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;}
  12. #rule p a{position:absolute;top:-4px;left:50%;width:15px;height:15px;margin-left:-8px;border:1px solid #d3d3d3;background:#ececec;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;}
  13. #rule p a:hover{border:1px solid #999;background:#dcdcdc;}
  14. .footer{font-size:12px;padding:10px 0;}
  15. </style>
  16. </head>
  17. <body>
  18. <div id='rule'>
  19.     <h3>Transform方法可视化操作演示</h3>
  20.     <p onmousedown='G.change(this,event,4);'><label>水平缩放 m11: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='m11' type='text' /></p>
  21.     <p onmousedown='G.change(this,event,4);'><label>y轴斜切 m12: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='m12' type='text' /></p>
  22.     <p onmousedown='G.change(this,event,4);'><label>x轴斜切 m21: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='m21' type='text' /></p>
  23.     <p onmousedown='G.change(this,event,4);'><label>垂直缩放 m22: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='m22' type='text' /></p>
  24.     <p onmousedown='G.change(this,event,1000);'><label>平移 dx: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='dx' type='text' /></p>
  25.     <p onmousedown='G.change(this,event,1000);'><label>纵移 dy: </label> <b class='range'><a onfocus="blur();" href='javascript:;'>&nbsp;</a></b> <input id='dy' type='text' /></p>
  26.     <div class='footer'>首先在右侧用鼠标画一矩形。<br />Javascript by <a href='http://www.cssass.com'>cssass.com</a></div>
  27. </div>
  28. <canvas id="pad" width='800' height="600" style="position:relative;border:1px solid #c3c3c3;cursor:crosshair;">注意:ie9以下用户请绕行</canvas>
  29. <script>
  30. var $id=function(o){return document.getElementById(o);}
  31. var con=$id("pad").getContext('2d');
  32. var G={};
  33. G.x1=G.y1=G.x2=G.y2=0;
  34. G.val=[[4,1],[4,0],[4,0],[4,1],[1000,0],[1000,0]];
  35. G.init=function(){
  36.     var list=$id('rule').getElementsByTagName('p');
  37.     var left, value;
  38.     for(var i=0; i<list.length; i++){
  39.         value = G.val[i][1];
  40.         left =(0.5 + G.val[i][1] / G.val[i][0] ) * 100;
  41.         list[i].getElementsByTagName('a')[0].style.left= left + '%';
  42.         list[i].getElementsByTagName('input')[0].value= value;
  43.     }
  44. }
  45. G.draw=function(){
  46.     G.init();
  47.     var e = arguments[0];
  48.         G.x1 = (e.layerX) ? e.layerX : e.offsetX,
  49.         G.y1 = (e.layerY) ? e.layerY : e.offsetY,
  50.     $id("pad").onmousemove=function(e){
  51.         con.clearRect(0,0,800,600);
  52.         G.x2= (e.layerX) ? e.layerX : e.offsetX;
  53.         G.y2= (e.layerY) ? e.layerY : e.offsetY;
  54.         con.save();
  55.         con.translate(G.x1,G.y1)
  56.         G.rect(G.x1,G.y1,G.x2,G.y2);
  57.         con.restore();
  58.     }
  59.     $id("pad").onmouseup=function(){
  60.         $id("pad").onmousedown=null;
  61.         $id("pad").onmousemove=null;
  62.     }
  63. }
  64. G.change=function(obj,e,l,v){
  65.     var o=obj.getElementsByTagName('a')[0];
  66.     var e = e ? e : window.event;
  67.     if(!window.event) {e.preventDefault();}
  68.     var tX=o.offsetLeft,
  69.         dx=e.clientX;
  70.     document.onmousemove=function(e){
  71.         var e = e ? e : window.event;
  72.         var len=tX+e.clientX-dx+8;
  73.         if(len>=0 && len<=150){
  74.             var rat=(len / 150) * 100;
  75.             o.style.left=rat+ "%";
  76.             obj.getElementsByTagName('input')[0].value =l * Math.round((rat - 50)*10)/1000;
  77.             G.transform();
  78.         }
  79.         if(window.event) e.returnValue=false;
  80.     }
  81.     document.onmouseup=function(){
  82.         document.onmousemove=null;
  83.         document.onmouseup=null;
  84.     }
  85. }
  86. G.transform=function(){
  87.     var m11=($id('m11').value == 0)? 0.001 : $id('m11').value, //firefox下m11,m12值不能为0
  88.         m12=$id('m12').value,
  89.         m21=$id('m21').value,
  90.         m22=($id('m22').value == 0)? 0.001 : $id('m22').value,
  91.         dx=$id('dx').value,
  92.         dy=$id('dy').value;
  93.     con.clearRect(0,0,800,600);
  94.     con.save();
  95.     con.translate(G.x1,G.y1); //变形的参考点设为图形的左上角
  96.     con.transform(m11, m12, m21, m22, dx, dy);
  97.     G.rect(G.x1,G.y1,G.x2,G.y2);
  98.     con.restore();
  99. }
  100. G.rect=function(x1,y1,x2,y2){
  101.     var grd=con.createLinearGradient(0, 0, x2 - x1, y2 - y1); //弄一个渐变效果
  102.             grd.addColorStop(0,"#3F5816");
  103.             grd.addColorStop(1,"#B40000");
  104.         con.fillStyle=grd;
  105.         con.fillRect(0, 0, x2 - x1, y2 - y1);
  106. }
  107. window.addEventListener("load", G.init, false);
  108. $id("pad").addEventListener("mousedown", G.draw, false);
  109. </script>
  110. </body>
  111. </html>


10-02 21:53