JavaScript 图片切割效果(带拖放、缩放效果)[转/改]

原文来自:http://www.cnblogs.com/cloudgamer/archive/2008/07/21/1247267.html
作者:cloudgamer
自己的网站需要这个功能,看到了cloudgamer的程序,非常值得收藏和使用,所以自己请教了cloudgamer,并进行了小小的修改,感谢cloudgamer的支持,本程序可以获取到选取框的尺寸!

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>JavaScript 图片切割效果(带拖放、缩放效果) </title>
  6. </head>
  7. <style type="text/css">
  8. #rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{position:absolute;background:#F00;width:5px; height:5px; z-index:500; font-size:0;}
  9. #dragDiv{border:1px solid #000000;}
  10. </style>
  11. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  12.   <tr>
  13.     <td width="50%"><div id="bgDiv" style="width:240px; height:320px;">
  14.         <div id="dragDiv">
  15.           <div id="rRightDown" style="right:0; bottom:0;"> </div>
  16.           <div id="rLeftDown" style="left:0; bottom:0;"> </div>
  17.           <div id="rRightUp" style="right:0; top:0;"> </div>
  18.           <div id="rLeftUp" style="left:0; top:0;"> </div>
  19.           <div id="rRight" style="right:0; top:50%;"> </div>
  20.           <div id="rLeft" style="left:0; top:50%;"> </div>
  21.           <div id="rUp" style="top:0; left:50%;"> </div>
  22.           <div id="rDown" style="bottom:0;left:50%;"></div>
  23.         </div>
  24.       </div></td>
  25.     <td><div id="viewDiv" style="width:200px; height:200px;"> </div></td>
  26.   </tr>
  27. </table>
  28. <script>
  29. var $ = function (id) {
  30.     return "string" == typeof id ? document.getElementById(id) : id;
  31. };
  32. var isIE = (document.all) ? true : false;
  33. function addEventHandler(oTarget, sEventType, fnHandler) {
  34.  if (oTarget.addEventListener) {
  35.   oTarget.addEventListener(sEventType, fnHandler, false);
  36.  } else if (oTarget.attachEvent) {
  37.   oTarget.attachEvent("on" + sEventType, fnHandler);
  38.  } else {
  39.   oTarget["on" + sEventType] = fnHandler;
  40.  }
  41. };
  42. function removeEventHandler(oTarget, sEventType, fnHandler) {
  43.     if (oTarget.removeEventListener) {
  44.         oTarget.removeEventListener(sEventType, fnHandler, false);
  45.     } else if (oTarget.detachEvent) {
  46.         oTarget.detachEvent("on" + sEventType, fnHandler);
  47.     } else { 
  48.         oTarget["on" + sEventType] = null;
  49.     }
  50. };
  51. var Class = {
  52.   create: function() {
  53.     return function() {
  54.       this.initialize.apply(this, arguments);
  55.     }
  56.   }
  57. }
  58. Object.extend = function(destination, source) {
  59.     for (var property in source) {
  60.         destination[property] = source[property];
  61.     }
  62.     return destination;
  63. }
  64. //拖放程序
  65. var Drag = Class.create();
  66. Drag.prototype = {
  67.   //拖放对象,触发对象
  68.   initialize: function(obj, drag, options) {
  69.     var oThis = this;
  70.  this._obj = $(obj);//拖放对象
  71.  this.Drag = $(drag);//触发对象
  72.  this._x = this._y = 0;//记录鼠标相对拖放对象的位置
  73.  //事件对象(用于移除事件)
  74.  this._fM = function(e){ oThis.Move(window.event || e); }
  75.  this._fS = function(){ oThis.Stop(); }
  76.  this.SetOptions(options);
  77.  this.Limit = !!this.options.Limit;
  78.  this.mxLeft = parseInt(this.options.mxLeft);
  79.  this.mxRight = parseInt(this.options.mxRight);
  80.  this.mxTop = parseInt(this.options.mxTop);
  81.  this.mxBottom = parseInt(this.options.mxBottom);
  82.  this.onMove = this.options.onMove;
  83.  this._obj.style.position = "absolute";
  84.  addEventHandler(this.Drag, "mousedown"function(e){ oThis.Start(window.event || e); });
  85.   },
  86.   //设置默认属性
  87.   SetOptions: function(options) {
  88.     this.options = {//默认值
  89.   Limit:  false,//是否设置限制(为true时下面参数有用,可以是负数)
  90.   mxLeft:  0,//左边限制
  91.   mxRight: 0,//右边限制
  92.   mxTop:  0,//上边限制
  93.   mxBottom: 0,//下边限制
  94.   onMove:  function(){}//移动时执行
  95.     };
  96.     Object.extend(this.options, options || {});
  97.   },
  98.   //准备拖动
  99.   Start: function(oEvent) {
  100.  //记录鼠标相对拖放对象的位置
  101.  this._x = oEvent.clientX - this._obj.offsetLeft;
  102.  this._y = oEvent.clientY - this._obj.offsetTop;
  103.  //mousemove时移动 mouseup时停止
  104.  addEventHandler(document, "mousemove"this._fM);
  105.  addEventHandler(document, "mouseup"this._fS);
  106.  //使鼠标移到窗口外也能释放
  107.  if(isIE){
  108.   addEventHandler(this.Drag, "losecapture"this._fS);
  109.   this.Drag.setCapture();
  110.  }else{
  111.   addEventHandler(window, "blur"this._fS);
  112.  }
  113.   },
  114.   //拖动
  115.   Move: function(oEvent) {
  116.  //清除选择(ie设置捕获后默认带这个)
  117.  window.getSelection && window.getSelection().removeAllRanges();
  118.  //当前鼠标位置减去相对拖放对象的位置得到offset位置
  119.  var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
  120.  //设置范围限制
  121.  if(this.Limit){
  122.   //获取超出长度
  123.   var iRight = iLeft + this._obj.offsetWidth - this.mxRight, iBottom = iTop + this._obj.offsetHeight - this.mxBottom;
  124.   //这里是先设置右边下边再设置左边上边,可能会不准确
  125.   if(iRight > 0) iLeft -= iRight;
  126.   if(iBottom > 0) iTop -= iBottom;
  127.   if(this.mxLeft > iLeft) iLeft = this.mxLeft;
  128.   if(this.mxTop > iTop) iTop = this.mxTop;
  129.  }
  130.  //设置位置
  131.  this._obj.style.left = iLeft + "px";
  132.  this._obj.style.top = iTop + "px"
  133.  //附加程序
  134.  this.onMove();
  135.   },
  136.   //停止拖动
  137.   Stop: function() {
  138.  //移除事件
  139.  removeEventHandler(document, "mousemove"this._fM);
  140.  removeEventHandler(document, "mouseup"this._fS);
  141.  if(isIE){
  142.   removeEventHandler(this.Drag, "losecapture"this._fS);
  143.   this.Drag.releaseCapture();
  144.  }else{
  145.   removeEventHandler(window, "blur"this._fS);
  146.  }
  147.   }
  148. };
  149. //缩放程序
  150. var Resize = Class.create();
  151. Resize.prototype = {
  152.   //缩放对象
  153.   initialize: function(obj, options) {
  154.     var oThis = this;
  155.  this._obj = $(obj);//缩放对象
  156.  this._right = this._down = this._left = this._up = 0;//坐标参数
  157.  this._scale = 1;//比例参数
  158.  this._touch = null;//当前触发对象
  159.  //用currentStyle(ff用getComputedStyle)取得最终样式
  160.  var _style = this._obj.currentStyle || document.defaultView.getComputedStyle(this._obj, null);
  161.  this._xBorder = parseInt(_style.borderLeftWidth) + parseInt(_style.borderRightWidth);
  162.  this._yBorder = parseInt(_style.borderTopWidth) + parseInt(_style.borderBottomWidth);
  163.  //事件对象(用于移除事件)
  164.  this._fR = function(e){ oThis.Resize(e); }
  165.  this._fS = function(){ oThis.Stop(); }
  166.  this.SetOptions(options);
  167.  this.Limit = !!this.options.Limit;
  168.  this.mxLeft = parseInt(this.options.mxLeft);
  169.  this.mxRight = parseInt(this.options.mxRight);
  170.  this.mxTop = parseInt(this.options.mxTop);
  171.  this.mxBottom = parseInt(this.options.mxBottom);
  172.  this.MinWidth = parseInt(this.options.MinWidth);
  173.  this.MinHeight = parseInt(this.options.MinHeight);
  174.  this.Scale = !!this.options.Scale;
  175.  this.onResize = this.options.onResize;
  176.  this._obj.style.position = "absolute";
  177.   },
  178.   //设置默认属性
  179.   SetOptions: function(options) {
  180.     this.options = {//默认值
  181.   Limit:  false,//是否设置限制(为true时下面mx参数有用)
  182.   mxLeft:  0,//左边限制
  183.   mxRight: 0,//右边限制
  184.   mxTop:  0,//上边限制
  185.   mxBottom: 0,//下边限制
  186.   MinWidth: 50,//最小宽度
  187.   MinHeight: 50,//最小高度
  188.   Scale:  false,//是否按比例缩放
  189.   onResize: function(){}//缩放时执行
  190.     };
  191.     Object.extend(this.options, options || {});
  192.   },
  193.   //设置触发对象
  194.   Set: function(resize, side) {
  195.  var oThis = this, resize = $(resize), _fun, _cursor;
  196.  if(!resize) return;
  197.  //根据方向设置 _fun是缩放时执行的程序 _cursor是鼠标样式
  198.  switch (side.toLowerCase()) {
  199.  case "up" :
  200.   _fun = function(e){ if(oThis.Scale){ oThis.scaleUpRight(e); }else{ oThis.SetUp(e); } };
  201.   _cursor = "n-resize";
  202.   break;
  203.  case "down" :
  204.   _fun = function(e){ if(oThis.Scale){ oThis.scaleDownRight(e); }else{ oThis.SetDown(e); } };
  205.   _cursor = "n-resize";
  206.   break;
  207.  case "left" :
  208.   _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); } };
  209.   _cursor = "e-resize";
  210.   break;
  211.  case "right" :
  212.   _fun = function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); } };
  213.   _cursor = "e-resize";
  214.   break;
  215.  case "left-up" :
  216.   _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftUp(e); }else{ oThis.SetLeft(e); oThis.SetUp(e); } };
  217.   _cursor = "nw-resize";
  218.   break;
  219.  case "right-up" :
  220.   _fun = function(e){ if(oThis.Scale){ oThis.scaleRightUp(e); }else{ oThis.SetRight(e); oThis.SetUp(e); } };
  221.   _cursor = "ne-resize";
  222.   break;
  223.  case "left-down" :
  224.   _fun = function(e){ if(oThis.Scale){ oThis.scaleLeftDown(e); }else{ oThis.SetLeft(e); oThis.SetDown(e); } };
  225.   _cursor = "ne-resize";
  226.   break;
  227.  case "right-down" :
  228.  default :
  229.   _fun = function(e){ if(oThis.Scale){ oThis.scaleRightDown(e); }else{ oThis.SetRight(e); oThis.SetDown(e); } };
  230.   _cursor = "nw-resize";
  231.  }
  232.  //设置触发对象
  233.  resize.style.cursor = _cursor;
  234.  addEventHandler(resize, "mousedown"function(e){ oThis._fun = _fun; oThis._touch = resize; oThis.Start(window.event || e); });
  235.   },
  236.   //准备缩放
  237.   Start: function(oEvent, o) { 
  238.  //防止冒泡
  239.  if(isIE){ oEvent.cancelBubble = true; } else { oEvent.stopPropagation(); }
  240.  //计算样式初始值
  241.  this.style_width = this._obj.offsetWidth;
  242.  this.style_height = this._obj.offsetHeight;
  243.  this.style_left = this._obj.offsetLeft;
  244.  this.style_top = this._obj.offsetTop;
  245.  //设置缩放比例
  246.  if(this.Scale){ this._scale = this.style_width / this.style_height; }
  247.  //计算当前边的对应另一条边的坐标 例如右边缩放时需要左边界坐标
  248.  this._left = oEvent.clientX - this.style_width;
  249.  this._right = oEvent.clientX + this.style_width;
  250.  this._up = oEvent.clientY - this.style_height;
  251.  this._down = oEvent.clientY + this.style_height;
  252.  //如果有范围 先计算好范围内最大宽度和高度
  253.  if(this.Limit){
  254.   this._mxRight = this.mxRight - this._obj.offsetLeft;
  255.   this._mxDown = this.mxBottom - this._obj.offsetTop;
  256.   this._mxLeft = this.mxLeft + this.style_width + this._obj.offsetLeft;
  257.   this._mxUp = this.mxTop + this.style_height + this._obj.offsetTop;
  258.  }
  259.  //mousemove时缩放 mouseup时停止
  260.  addEventHandler(document, "mousemove"this._fR);
  261.  addEventHandler(document, "mouseup"this._fS);
  262.  //使鼠标移到窗口外也能释放
  263.  if(isIE){
  264.   addEventHandler(this._touch, "losecapture"this._fS);
  265.   this._touch.setCapture();
  266.  }else{
  267.   addEventHandler(window, "blur"this._fS);
  268.  }
  269.   },  
  270.   //缩放
  271.   Resize: function(e) {
  272.  //没有触发对象的话返回
  273.  if(!this._touch) return;
  274.  //清除选择(ie设置捕获后默认带这个)
  275.  window.getSelection && window.getSelection().removeAllRanges();
  276.  //执行缩放程序
  277.  this._fun(window.event || e);
  278.  //设置样式
  279.  //因为计算用的offset是把边框算进去的所以要减去
  280.  this._obj.style.width = this.style_width - this._xBorder + "px";
  281.  this._obj.style.height = this.style_height - this._yBorder + "px";
  282.  this._obj.style.top = this.style_top + "px";
  283.  this._obj.style.left = this.style_left + "px"
  284.  //附加程序
  285.  this.onResize();
  286.   },
  287.   //普通缩放
  288.   //右边
  289.   SetRight: function(oEvent) {
  290.  //当前坐标位置减去左边的坐标等于当前宽度
  291.  this.repairRight(oEvent.clientX - this._left);
  292.   },
  293.   //下边
  294.   SetDown: function(oEvent) {
  295.  this.repairDown(oEvent.clientY - this._up);
  296.   },
  297.   //左边
  298.   SetLeft: function(oEvent) {
  299.  //右边的坐标减去当前坐标位置等于当前宽度
  300.  this.repairLeft(this._right - oEvent.clientX);
  301.   },
  302.   //上边
  303.   SetUp: function(oEvent) {
  304.  this.repairUp(this._down - oEvent.clientY);
  305.   },
  306.   //比例缩放
  307.   //比例缩放右下
  308.   scaleRightDown: function(oEvent) {
  309.  //先计算宽度然后按比例计算高度最后根据确定的高度计算宽度(宽度优先)
  310.  this.SetRight(oEvent);
  311.  this.repairDown(parseInt(this.style_width / this._scale));
  312.  this.repairRight(parseInt(this.style_height * this._scale));
  313.   },
  314.   //比例缩放左下
  315.   scaleLeftDown: function(oEvent) {
  316.  this.SetLeft(oEvent);
  317.  this.repairDown(parseInt(this.style_width / this._scale));
  318.  this.repairLeft(parseInt(this.style_height * this._scale));
  319.   },
  320.   //比例缩放右上
  321.   scaleRightUp: function(oEvent) {
  322.  this.SetRight(oEvent);
  323.  this.repairUp(parseInt(this.style_width / this._scale));
  324.  this.repairRight(parseInt(this.style_height * this._scale));
  325.   },
  326.   //比例缩放左上
  327.   scaleLeftUp: function(oEvent) {
  328.  this.SetLeft(oEvent);
  329.  this.repairUp(parseInt(this.style_width / this._scale));
  330.  this.repairLeft(parseInt(this.style_height * this._scale));
  331.   },
  332.   //这里是高度优先用于上下两边(体验更好)
  333.   //比例缩放下右
  334.   scaleDownRight: function(oEvent) {
  335.  this.SetDown(oEvent);
  336.  this.repairRight(parseInt(this.style_height * this._scale));
  337.  this.repairDown(parseInt(this.style_width / this._scale));
  338.   },
  339.   //比例缩放上右
  340.   scaleUpRight: function(oEvent) {
  341.  this.SetUp(oEvent);
  342.  this.repairRight(parseInt(this.style_height * this._scale));
  343.  this.repairUp(parseInt(this.style_width / this._scale));
  344.   },
  345.   //修正程序
  346.   //修正右边
  347.   repairRight: function(iWidth) {
  348.  //右边和下边只要设置宽度和高度就行
  349.  //当少于最少宽度
  350.  if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
  351.  //当超过当前设定的最大宽度
  352.  if(this.Limit && iWidth > this._mxRight){ iWidth = this._mxRight; }
  353.  //修改样式
  354.  this.style_width = iWidth;
  355.   },
  356.   //修正下边
  357.   repairDown: function(iHeight) {
  358.  if (iHeight < this.MinHeight){ iHeight = this.MinHeight; }
  359.  if(this.Limit && iHeight > this._mxDown){ iHeight = this._mxDown; }
  360.  this.style_height = iHeight;
  361.   },
  362.   //修正左边
  363.   repairLeft: function(iWidth) {
  364.  //左边和上边比较麻烦 因为还要计算left和top
  365.  //当少于最少宽度
  366.  if (iWidth < this.MinWidth){ iWidth = this.MinWidth; }
  367.  //当超过当前设定的最大宽度
  368.  else if(this.Limit && iWidth > this._mxLeft){ iWidth = this._mxLeft; }
  369.  //修改样式
  370.  this.style_width = iWidth;
  371.  this.style_left = this._obj.offsetLeft + this._obj.offsetWidth - iWidth;
  372.   },
  373.   //修正上边
  374.   repairUp: function(iHeight) {
  375.  if(iHeight < this.MinHeight){ iHeight = this.MinHeight; }
  376.  else if(this.Limit && iHeight > this._mxUp){ iHeight = this._mxUp; }
  377.  this.style_height = iHeight;
  378.  this.style_top = this._obj.offsetTop + this._obj.offsetHeight - iHeight;
  379.   },
  380.   //停止缩放
  381.   Stop: function() {
  382.  //移除事件
  383.  removeEventHandler(document, "mousemove"this._fR);
  384.  removeEventHandler(document, "mouseup"this._fS);
  385.  if(isIE){
  386.   removeEventHandler(this._touch, "losecapture"this._fS);
  387.   this._touch.releaseCapture();
  388.  }else{
  389.   removeEventHandler(window, "blur"this._fS);
  390.  }
  391.  this._touch = null;
  392.   }
  393. };
  394. //图片切割
  395. var ImgCropper = Class.create();
  396. ImgCropper.prototype = {
  397.   //容器对象,拖放缩放对象,图片地址,宽度,高度
  398.   initialize: function(container, drag, url, width, height, options) {
  399.  var oThis = this;
  400.  //容器对象
  401.  this.Container = $(container);
  402.  this.Container.style.position = "relative";
  403.  this.Container.style.overflow = "hidden";
  404.  //拖放对象
  405.  this.Drag = $(drag);
  406.  this.Drag.style.cursor = "move";
  407.  this.Drag.style.zIndex = 200;
  408.  if(isIE){
  409.   //设置overflow解决ie6的渲染问题(缩放时填充对象高度的问题)
  410.   this.Drag.style.overflow = "hidden";
  411.   //ie下用一个透明的层填充拖放对象 不填充的话onmousedown会失效(未知原因)
  412.   (function(style){
  413.    style.width = style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
  414.   })(this.Drag.appendChild(document.createElement("div")).style)
  415.  }
  416.  this._pic = this.Container.appendChild(document.createElement("img"));//图片对象
  417.  this._cropper = this.Container.appendChild(document.createElement("img"));//切割对象
  418.  this._pic.style.position = this._cropper.style.position = "absolute";
  419.  this._pic.style.top = this._pic.style.left = this._cropper.style.top = this._cropper.style.left = "0";//对齐
  420.  this._cropper.style.zIndex = 100;
  421.  this._cropper.onload = function(){ oThis.SetPos(); }
  422.  this.Url = "img/1.jpg";//图片地址
  423.  this.Width = parseInt(width);//宽度
  424.  this.Height = parseInt(height);//高度
  425.  this.SetOptions(options);
  426.  this.Opacity = parseInt(this.options.Opacity);
  427.  this.dragTop = parseInt(this.options.dragTop);
  428.  this.dragLeft = parseInt(this.options.dragLeft);
  429.  this.dragWidth = parseInt(this.options.dragWidth);
  430.  this.dragHeight = parseInt(this.options.dragHeight);
  431.  //设置预览对象
  432.  this.View = $(this.options.View) || null;//预览对象
  433.  this.viewWidth = parseInt(this.options.viewWidth);
  434.  this.viewHeight = parseInt(this.options.viewHeight);
  435.  this._view = null;//预览图片对象
  436.  if(this.View){
  437.   this.View.style.position = "relative";
  438.   this.View.style.overflow = "hidden";
  439.   this._view = this.View.appendChild(document.createElement("img"));
  440.   this._view.style.position = "absolute";
  441.  }
  442.  this.Scale = parseInt(this.options.Scale);
  443.  //设置拖放
  444. this._drag = new Drag(this.Drag, this.Drag, { Limit: true, onMove: function(){ oThis.SetPos();text(); } });
  445.  //设置缩放
  446.  this._resize = this.GetResize();
  447.  this.Init();
  448.   },
  449.   //设置默认属性
  450.   SetOptions: function(options) {
  451.     this.options = {//默认值
  452.   Opacity: 50,//透明度(0到100) 
  453.   //拖放位置和宽高
  454.   dragTop: 0,
  455.   dragLeft: 0,
  456.   dragWidth: 100,
  457.   dragHeight: 100,
  458.   //缩放触发对象
  459.   Right:  "",
  460.   Left:  "",
  461.   Up:   "",
  462.   Down:  "",
  463.   RightDown: "",
  464.   LeftDown: "",
  465.   RightUp: "",
  466.   LeftUp:  "",
  467.   Scale:  false,//是否按比例缩放
  468.   //预览对象设置
  469.   View: "",//预览对象
  470.   viewWidth: 100,//预览宽度
  471.   viewHeight: 100//预览高度
  472.     };
  473.     Object.extend(this.options, options || {});
  474.   },
  475.   //初始化对象
  476.   Init: function() {
  477.  var oThis = this;
  478.  //设置容器
  479.  this.Container.style.width = this.Width + "px";
  480.  this.Container.style.height = this.Height + "px";
  481.  //设置拖放对象
  482.  this.Drag.style.top = this.dragTop + "px";
  483.  this.Drag.style.left = this.dragLeft + "px";
  484.  this.Drag.style.width = this.dragWidth + "px";
  485.  this.Drag.style.height = this.dragHeight + "px";
  486.  //设置切割对象
  487.  this._pic.src = this._cropper.src = this.Url;
  488.  this._pic.style.width = this._cropper.style.width = this.Width + "px";
  489.  this._pic.style.height = this._cropper.style.height = this.Height + "px";
  490.  if(isIE){
  491.   this._pic.style.filter = "alpha(opacity:" + this.Opacity + ")";
  492.  } else {
  493.   this._pic.style.opacity = this.Opacity / 100;
  494.  }
  495.  //设置预览对象
  496.  if(this.View){ this._view.src = this.Url; }
  497.  //设置拖放
  498.  this._drag.mxRight = this.Width; this._drag.mxBottom = this.Height;
  499.  //设置缩放
  500.  if(this._resize){ this._resize.mxRight = this.Width; this._resize.mxBottom = this.Height; this._resize.Scale = this.Scale; }
  501.   },
  502.   //设置获取缩放对象
  503.   GetResize: function() {
  504.  var op = this.options;
  505.  //有触发对象时才设置
  506.  if(op.RightDown || op.LeftDown || op.RightUp || op.LeftUp || op.Right || op.Left || op.Up || op.Down ){
  507.   var oThis = this, _resize = new Resize(this.Drag, { Limit: true, onResize: function(){ oThis.SetPos();text(); } });  
  508.   //设置缩放触发对象
  509.   if(op.RightDown){ _resize.Set(op.RightDown, "right-down"); }
  510.   if(op.LeftDown){ _resize.Set(op.LeftDown, "left-down"); }
  511.   
  512.   if(op.RightUp){ _resize.Set(op.RightUp, "right-up"); }
  513.   if(op.LeftUp){ _resize.Set(op.LeftUp, "left-up"); }
  514.   
  515.   if(op.Right){ _resize.Set(op.Right, "right"); }
  516.   if(op.Left){ _resize.Set(op.Left, "left"); }
  517.   
  518.   if(op.Up){ _resize.Set(op.Up, "up"); }
  519.   if(op.Down){ _resize.Set(op.Down, "down"); }
  520.   
  521.   return _resize;
  522.  } else { return null; }
  523.   },  
  524.   //设置切割
  525.   SetPos: function() {
  526.  var o = this.Drag;
  527.  //按拖放对象的参数进行切割
  528.  this._cropper.style.clip = "rect(" + o.offsetTop + "px " + (o.offsetLeft + o.offsetWidth) + "px " + (o.offsetTop + o.offsetHeight) + "px " + o.offsetLeft + "px)";
  529.  //切割预览
  530.  if(this.View) this.PreView();
  531.   },  
  532.   //切割预览
  533.   PreView: function() {
  534.  //按比例设置宽度和高度
  535.  var o = this.Drag, h = this.viewWidth, w = h * o.offsetWidth / o.offsetHeight;
  536.  if(w > this.viewHeight){ w = this.viewHeight; h = w * o.offsetHeight / o.offsetWidth; }
  537.  //获取对应比例尺寸
  538.  var scale = h / o.offsetHeight, ph = this.Height * scale, pw = this.Width * scale, pt = o.offsetTop * scale, pl = o.offsetLeft * scale, styleView = this._view.style;
  539.  //设置样式
  540.  styleView.width = pw + "px"; styleView.height = ph + "px";
  541.  styleView.top = - pt + "px "; styleView.left = - pl + "px";
  542.  //切割预览图
  543.  styleView.clip = "rect(" + pt + "px " + (pl + w) + "px " + (pt + h) + "px " + pl + "px)";
  544.   }
  545. }
  546. var ic = new ImgCropper("bgDiv""dragDiv""1.jpg", 240, 320, {
  547.  dragTop: 50, dragLeft: 50,
  548.  Right: "rRight", Left: "rLeft", Up: "rUp", Down: "rDown",
  549.  RightDown: "rRightDown", LeftDown: "rLeftDown", RightUp: "rRightUp", LeftUp: "rLeftUp",
  550.  View: "viewDiv", viewWidth: 200, viewHeight: 200
  551. })
  552. </script>
  553. <script>
  554. //设置图片大小
  555. function Size(w, h){
  556.  ic.Width = w;
  557.  ic.Height = h;
  558.  ic.Init();
  559. }
  560. //换图片
  561. function Pic(url){
  562.  ic.Url = url;
  563.  ic.Init();
  564. }
  565. //设置透明度
  566. function Opacity(i){
  567.  ic.Opacity = i;
  568.  ic.Init();
  569. }
  570. //是否使用比例
  571. function Scale(b){
  572.  ic.Scale = b;
  573.  ic.Init();
  574. }
  575. function text(){
  576. var w = ic.Drag.offsetWidth;
  577.  h = ic.Drag.offsetHeight; 
  578. document.all.text1.value=w;
  579. document.all.text2.value=h;
  580. }
  581. </script>
  582. <br />
  583. <br />
  584. <div>
  585. <input  name="text1" type="text" value="" size="3" />
  586. <input type="text"  name="text2" value="" size="3" />
  587. <input type="submit" name="button" id="button" οnclick="text()" value="选取框W/H" />
  588.   <input name="" type="button" value=" 增肥 " οnclick="Size(500,400)" />
  589.   <input name="" type="button" value=" 还原 " οnclick="Size(300,400)" />
  590. </div>
  591. <br />
  592. <div>
  593.   <input name="" type="button" value=" 换图 " οnclick="Pic('img/2.jpg')" />
  594.   <input name="" type="button" value=" 还原 " οnclick="Pic('img/1.jpg')" />
  595. </div>
  596. <br />
  597. <div>
  598.   <input name="" type="button" value=" 透明 " οnclick="Opacity(0)" />
  599.   <input name="" type="button" value=" 还原 " οnclick="Opacity(50)" />
  600. </div>
  601. <br />
  602. <div>
  603.   <input name="" type="button" value="使用比例" οnclick="Scale(true)" />
  604.   <input name="" type="button" value="取消比例" οnclick="Scale(false)" />
  605. </div>
  606. <br />
  607. </body>
  608. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值