`
aoyoo111
  • 浏览: 12234 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

阅读更多

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

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
  5. <title>JavaScript图片切割效果(带拖放、缩放效果)</title>
  6. </head>
  7. <styletype="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:1pxsolid#000000;}
  10. </style>
  11. <tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
  12. <tr>
  13. <tdwidth="50%"><divid="bgDiv"style="width:240px;height:320px;">
  14. <divid="dragDiv">
  15. <divid="rRightDown"style="right:0;bottom:0;"></div>
  16. <divid="rLeftDown"style="left:0;bottom:0;"></div>
  17. <divid="rRightUp"style="right:0;top:0;"></div>
  18. <divid="rLeftUp"style="left:0;top:0;"></div>
  19. <divid="rRight"style="right:0;top:50%;"></div>
  20. <divid="rLeft"style="left:0;top:50%;"></div>
  21. <divid="rUp"style="top:0;left:50%;"></div>
  22. <divid="rDown"style="bottom:0;left:50%;"></div>
  23. </div>
  24. </div></td>
  25. <td><divid="viewDiv"style="width:200px;height:200px;"></div></td>
  26. </tr>
  27. </table>
  28. <script>
  29. var$=function(id){
  30. return"string"==typeofid?document.getElementById(id):id;
  31. };
  32. varisIE=(document.all)?true:false;
  33. functionaddEventHandler(oTarget,sEventType,fnHandler){
  34. if(oTarget.addEventListener){
  35. oTarget.addEventListener(sEventType,fnHandler,false);
  36. }elseif(oTarget.attachEvent){
  37. oTarget.attachEvent("on"+sEventType,fnHandler);
  38. }else{
  39. oTarget["on"+sEventType]=fnHandler;
  40. }
  41. };
  42. functionremoveEventHandler(oTarget,sEventType,fnHandler){
  43. if(oTarget.removeEventListener){
  44. oTarget.removeEventListener(sEventType,fnHandler,false);
  45. }elseif(oTarget.detachEvent){
  46. oTarget.detachEvent("on"+sEventType,fnHandler);
  47. }else{
  48. oTarget["on"+sEventType]=null;
  49. }
  50. };
  51. varClass={
  52. create:function(){
  53. returnfunction(){
  54. this.initialize.apply(this,arguments);
  55. }
  56. }
  57. }
  58. Object.extend=function(destination,source){
  59. for(varpropertyinsource){
  60. destination[property]=source[property];
  61. }
  62. returndestination;
  63. }
  64. //拖放程序
  65. varDrag=Class.create();
  66. Drag.prototype={
  67. //拖放对象,触发对象
  68. initialize:function(obj,drag,options){
  69. varoThis=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. variLeft=oEvent.clientX-this._x,iTop=oEvent.clientY-this._y;
  120. //设置范围限制
  121. if(this.Limit){
  122. //获取超出长度
  123. variRight=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. varResize=Class.create();
  151. Resize.prototype={
  152. //缩放对象
  153. initialize:function(obj,options){
  154. varoThis=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. varoThis=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. elseif(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. elseif(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. varImgCropper=Class.create();
  396. ImgCropper.prototype={
  397. //容器对象,拖放缩放对象,图片地址,宽度,高度
  398. initialize:function(container,drag,url,width,height,options){
  399. varoThis=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=newDrag(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. varoThis=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. varop=this.options;
  505. //有触发对象时才设置
  506. if(op.RightDown||op.LeftDown||op.RightUp||op.LeftUp||op.Right||op.Left||op.Up||op.Down){
  507. varoThis=this,_resize=newResize(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. if(op.RightUp){_resize.Set(op.RightUp,"right-up");}
  512. if(op.LeftUp){_resize.Set(op.LeftUp,"left-up");}
  513. if(op.Right){_resize.Set(op.Right,"right");}
  514. if(op.Left){_resize.Set(op.Left,"left");}
  515. if(op.Up){_resize.Set(op.Up,"up");}
  516. if(op.Down){_resize.Set(op.Down,"down");}
  517. return_resize;
  518. }else{returnnull;}
  519. },
  520. //设置切割
  521. SetPos:function(){
  522. varo=this.Drag;
  523. //按拖放对象的参数进行切割
  524. this._cropper.style.clip="rect("+o.offsetTop+"px"+(o.offsetLeft+o.offsetWidth)+"px"+(o.offsetTop+o.offsetHeight)+"px"+o.offsetLeft+"px)";
  525. //切割预览
  526. if(this.View)this.PreView();
  527. },
  528. //切割预览
  529. PreView:function(){
  530. //按比例设置宽度和高度
  531. varo=this.Drag,h=this.viewWidth,w=h*o.offsetWidth/o.offsetHeight;
  532. if(w>this.viewHeight){w=this.viewHeight;h=w*o.offsetHeight/o.offsetWidth;}
  533. //获取对应比例尺寸
  534. varscale=h/o.offsetHeight,ph=this.Height*scale,pw=this.Width*scale,pt=o.offsetTop*scale,pl=o.offsetLeft*scale,styleView=this._view.style;
  535. //设置样式
  536. styleView.width=pw+"px";styleView.height=ph+"px";
  537. styleView.top=-pt+"px";styleView.left=-pl+"px";
  538. //切割预览图
  539. styleView.clip="rect("+pt+"px"+(pl+w)+"px"+(pt+h)+"px"+pl+"px)";
  540. }
  541. }
  542. varic=newImgCropper("bgDiv","dragDiv","1.jpg",240,320,{
  543. dragTop:50,dragLeft:50,
  544. Right:"rRight",Left:"rLeft",Up:"rUp",Down:"rDown",
  545. RightDown:"rRightDown",LeftDown:"rLeftDown",RightUp:"rRightUp",LeftUp:"rLeftUp",
  546. View:"viewDiv",viewWidth:200,viewHeight:200
  547. })
  548. </script>
  549. <script>
  550. //设置图片大小
  551. functionSize(w,h){
  552. ic.Width=w;
  553. ic.Height=h;
  554. ic.Init();
  555. }
  556. //换图片
  557. functionPic(url){
  558. ic.Url=url;
  559. ic.Init();
  560. }
  561. //设置透明度
  562. functionOpacity(i){
  563. ic.Opacity=i;
  564. ic.Init();
  565. }
  566. //是否使用比例
  567. functionScale(b){
  568. ic.Scale=b;
  569. ic.Init();
  570. }
  571. functiontext(){
  572. varw=ic.Drag.offsetWidth;
  573. h=ic.Drag.offsetHeight;
  574. document.all.text1.value=w;
  575. document.all.text2.value=h;
  576. }
  577. </script>
  578. <br/>
  579. <br/>
  580. <div>
  581. <inputname="text1"type="text"value=""size="3"/>
  582. <inputtype="text"name="text2"value=""size="3"/>
  583. <inputtype="submit"name="button"id="button"onclick="text()"value="选取框W/H"/>
  584. <inputname=""type="button"value="增肥"onclick="Size(500,400)"/>
  585. <inputname=""type="button"value="还原"onclick="Size(300,400)"/>
  586. </div>
  587. <br/>
  588. <div>
  589. <inputname=""type="button"value="换图"onclick="Pic('img/2.jpg')"/>
  590. <inputname=""type="button"value="还原"onclick="Pic('img/1.jpg')"/>
  591. </div>
  592. <br/>
  593. <div>
  594. <inputname=""type="button"value="透明"onclick="Opacity(0)"/>
  595. <inputname=""type="button"value="还原"onclick="Opacity(50)"/>
  596. </div>
  597. <br/>
  598. <div>
  599. <inputname=""type="button"value="使用比例"onclick="Scale(true)"/>
  600. <inputname=""type="button"value="取消比例"onclick="Scale(false)"/>
  601. </div>
  602. <br/>
  603. </body>
  604. </html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics