|
| 1 | +/** Reszie |
| 2 | + * @el dom |
| 3 | + * @container dom |
| 4 | + * @ratio string '1:1' like this |
| 5 | + * e events |
| 6 | + **/ |
| 7 | +import helper from './helper'; |
| 8 | + |
| 9 | +const isMobile = helper.isMobile; |
| 10 | +const W = document.body.offsetWidth; |
| 11 | +export default function resize(e, el, container, coor, ratio) { |
| 12 | + if (!el) { |
| 13 | + return ; |
| 14 | + } |
| 15 | + const H = document.body.offsetHeight; |
| 16 | + const ratioRemainder = 1 / ratio; |
| 17 | + const dotBoxW = parseInt(window.getComputedStyle(container).width); |
| 18 | + const dotBoxH = parseInt(window.getComputedStyle(container).height); |
| 19 | + const $topH = document.querySelector('.info-aside'); |
| 20 | + const halfX = (W - dotBoxW) / 2; |
| 21 | + const topH = parseInt(window.getComputedStyle($topH).height); |
| 22 | + const halfY = (H - dotBoxH - topH)/2; |
| 23 | + const resetX = isMobile ? e.changedTouches[0]['clientX'] : e.clientX; |
| 24 | + const resetY = isMobile ? e.changedTouches[0]['clientY'] : e.clientY; |
| 25 | + const elOffsetWidth = parseInt(el.offsetWidth); |
| 26 | + const elOffsetHeight = parseInt(el.offsetHeight); |
| 27 | + const CSSObj = {}; |
| 28 | + if (ratio >= 1 && resetX <= halfX + dotBoxW) { |
| 29 | + if (elOffsetWidth >= dotBoxW) { |
| 30 | + CSSObj.width = dotBoxW; |
| 31 | + } |
| 32 | + CSSObj.width = (coor.w + resetX - coor.x); |
| 33 | + CSSObj.height = elOffsetWidth * ratioRemainder; |
| 34 | + if (dotBoxW > dotBoxH) { |
| 35 | + if (elOffsetWidth > dotBoxH) { |
| 36 | + CSSObj.height = dotBoxH; |
| 37 | + CSSObj.width = dotBoxH * ratio; |
| 38 | + } |
| 39 | + } else if (dotBoxW < dotBoxH) { |
| 40 | + if (elOffsetWidth > dotBoxW) { |
| 41 | + CSSObj.width = dotBoxW; |
| 42 | + CSSObj.height = dotBoxW * ratioRemainder; |
| 43 | + } |
| 44 | + } else if (elOffsetWidth >= dotBoxW) { |
| 45 | + CSSObj.width = dotBoxW ; |
| 46 | + CSSObj.height = dotBoxW * ratioRemainder; |
| 47 | + } |
| 48 | + } else if (ratio < 1 && resetY < (halfY + dotBoxH + topH)) { |
| 49 | + CSSObj.height = (coor.h + resetY - coor.y); |
| 50 | + CSSObj.width = parseInt(el.style.height) * ratio; |
| 51 | + // 限制拖拉的范围在图片内 |
| 52 | + if (dotBoxW > dotBoxH) { |
| 53 | + if (elOffsetHeight > dotBoxH) { |
| 54 | + CSSObj.height = dotBoxH; |
| 55 | + CSSObj.width = dotBoxH * ratio; |
| 56 | + } |
| 57 | + } else if (dotBoxW < dotBoxH) { |
| 58 | + if (elOffsetWidth > dotBoxW) { |
| 59 | + CSSObj.width = dotBoxW; |
| 60 | + CSSObj.height = dotBoxW * ratioRemainder; |
| 61 | + } |
| 62 | + } else if (elOffsetWidth > dotBoxW) { |
| 63 | + CSSObj.width = dotBoxW; |
| 64 | + CSSObj.height = dotBoxW * ratioRemainder; |
| 65 | + } |
| 66 | + } else if(ratio == 'auto' && resetY <= (halfY + dotBoxH + topH) && resetX <= halfY + dotBoxW) { |
| 67 | + CSSObj.height = (coor.h + resetY - coor.y); |
| 68 | + CSSObj.width = (coor.w + resetX - coor.x); |
| 69 | + } else if (resetX <= halfX + dotBoxW) { |
| 70 | + CSSObj.width = (coor.w + resetX - coor.x); |
| 71 | + CSSObj.height = el.style.width; |
| 72 | + // limit the crop box area |
| 73 | + if (dotBoxW > dotBoxH) { |
| 74 | + if (elOffsetHeight > dotBoxH) { |
| 75 | + CSSObj.height = dotBoxH; |
| 76 | + CSSObj.width = dotBoxH; |
| 77 | + } |
| 78 | + } else if (dotBoxW < dotBoxH) { |
| 79 | + if (elOffsetWidth > dotBoxW) { |
| 80 | + CSSObj.width = dotBoxW; |
| 81 | + CSSObj.height = dotBoxW; |
| 82 | + } |
| 83 | + } else if (elOffsetWidth > dotBoxW) { |
| 84 | + CSSObj.width = el.style.height = dotBoxW; |
| 85 | + } |
| 86 | + } |
| 87 | + return CSSObj; |
| 88 | +}; |
0 commit comments