CropperModal.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. import { defineComponent as D, openBlock as W, createElementBlock as I, withDirectives as T, createElementVNode as x, normalizeStyle as H, vShow as N, createCommentVNode as k, normalizeClass as F, toDisplayString as S, ref as C, resolveComponent as q, createBlock as V, withCtx as X, createVNode as M, unref as j, createTextVNode as $ } from "vue";
  2. import { n as Q, B as Z } from "./index2.js";
  3. import { _ as G, a as J } from "./index10.js";
  4. const A = {};
  5. A.getData = (t) => new Promise((e, s) => {
  6. let i = {};
  7. K(t).then((h) => {
  8. i.arrayBuffer = h;
  9. try {
  10. i.orientation = it(h);
  11. } catch (r) {
  12. i.orientation = -1;
  13. }
  14. e(i);
  15. }).catch((h) => {
  16. s(h);
  17. });
  18. });
  19. function K(t) {
  20. let e = null;
  21. return new Promise((s, i) => {
  22. if (t.src)
  23. if (/^data\:/i.test(t.src))
  24. e = et(t.src), s(e);
  25. else if (/^blob\:/i.test(t.src)) {
  26. var h = new FileReader();
  27. h.onload = function(o) {
  28. e = o.target.result, s(e);
  29. }, tt(t.src, function(o) {
  30. h.readAsArrayBuffer(o);
  31. });
  32. } else {
  33. var r = new XMLHttpRequest();
  34. r.onload = function() {
  35. if (this.status == 200 || this.status === 0)
  36. e = r.response, s(e);
  37. else
  38. throw "Could not load image";
  39. r = null;
  40. }, r.open("GET", t.src, !0), r.responseType = "arraybuffer", r.send(null);
  41. }
  42. else
  43. i("img error");
  44. });
  45. }
  46. function tt(t, e) {
  47. var s = new XMLHttpRequest();
  48. s.open("GET", t, !0), s.responseType = "blob", s.onload = function(i) {
  49. (this.status == 200 || this.status === 0) && e(this.response);
  50. }, s.send();
  51. }
  52. function et(t, e) {
  53. e = e || t.match(/^data\:([^\;]+)\;base64,/mi)[1] || "", t = t.replace(/^data\:([^\;]+)\;base64,/gmi, "");
  54. for (var s = atob(t), i = s.length % 2 == 0 ? s.length : s.length + 1, h = new ArrayBuffer(i), r = new Uint16Array(h), o = 0; o < i; o++)
  55. r[o] = s.charCodeAt(o);
  56. return h;
  57. }
  58. function st(t, e, s) {
  59. var i = "", h;
  60. for (h = e, s += e; h < s; h++)
  61. i += String.fromCharCode(t.getUint8(h));
  62. return i;
  63. }
  64. function it(t) {
  65. var e = new DataView(t), s = e.byteLength, i, h, r, o, a, c, n, l, u, p;
  66. if (e.getUint8(0) === 255 && e.getUint8(1) === 216)
  67. for (u = 2; u < s; ) {
  68. if (e.getUint8(u) === 255 && e.getUint8(u + 1) === 225) {
  69. n = u;
  70. break;
  71. }
  72. u++;
  73. }
  74. if (n && (h = n + 4, r = n + 10, st(e, h, 4) === "Exif" && (c = e.getUint16(r), a = c === 18761, (a || c === 19789) && e.getUint16(r + 2, a) === 42 && (o = e.getUint32(r + 4, a), o >= 8 && (l = r + o)))), l) {
  75. for (s = e.getUint16(l, a), p = 0; p < s; p++)
  76. if (u = l + p * 12 + 2, e.getUint16(u, a) === 274) {
  77. u += 8, i = e.getUint16(u, a);
  78. break;
  79. }
  80. }
  81. return i;
  82. }
  83. const ht = (t, e) => {
  84. const s = t.__vccOpts || t;
  85. for (const [i, h] of e)
  86. s[i] = h;
  87. return s;
  88. }, rt = D({
  89. data: function() {
  90. return {
  91. // 容器高宽
  92. w: 0,
  93. h: 0,
  94. // 图片缩放比例
  95. scale: 1,
  96. // 图片偏移x轴
  97. x: 0,
  98. // 图片偏移y轴
  99. y: 0,
  100. // 图片加载
  101. loading: !0,
  102. // 图片真实宽度
  103. trueWidth: 0,
  104. // 图片真实高度
  105. trueHeight: 0,
  106. move: !0,
  107. // 移动的x
  108. moveX: 0,
  109. // 移动的y
  110. moveY: 0,
  111. // 开启截图
  112. crop: !1,
  113. // 正在截图
  114. cropping: !1,
  115. // 裁剪框大小
  116. cropW: 0,
  117. cropH: 0,
  118. cropOldW: 0,
  119. cropOldH: 0,
  120. // 判断是否能够改变
  121. canChangeX: !1,
  122. canChangeY: !1,
  123. // 改变的基准点
  124. changeCropTypeX: 1,
  125. changeCropTypeY: 1,
  126. // 裁剪框的坐标轴
  127. cropX: 0,
  128. cropY: 0,
  129. cropChangeX: 0,
  130. cropChangeY: 0,
  131. cropOffsertX: 0,
  132. cropOffsertY: 0,
  133. // 支持的滚动事件
  134. support: "",
  135. // 移动端手指缩放
  136. touches: [],
  137. touchNow: !1,
  138. // 图片旋转
  139. rotate: 0,
  140. isIos: !1,
  141. orientation: 0,
  142. imgs: "",
  143. // 图片缩放系数
  144. coe: 0.2,
  145. // 是否正在多次缩放
  146. scaling: !1,
  147. scalingSet: "",
  148. coeStatus: "",
  149. // 控制emit触发频率
  150. isCanShow: !0,
  151. // 图片是否等于截图大小
  152. imgIsQqualCrop: !1
  153. };
  154. },
  155. props: {
  156. img: {
  157. type: [String, Blob, null, File],
  158. default: ""
  159. },
  160. // 输出图片压缩比
  161. outputSize: {
  162. type: Number,
  163. default: 1
  164. },
  165. outputType: {
  166. type: String,
  167. default: "jpeg"
  168. },
  169. info: {
  170. type: Boolean,
  171. default: !0
  172. },
  173. // 是否开启滚轮放大缩小
  174. canScale: {
  175. type: Boolean,
  176. default: !0
  177. },
  178. // 是否自成截图框
  179. autoCrop: {
  180. type: Boolean,
  181. default: !1
  182. },
  183. autoCropWidth: {
  184. type: [Number, String],
  185. default: 0
  186. },
  187. autoCropHeight: {
  188. type: [Number, String],
  189. default: 0
  190. },
  191. // 是否开启固定宽高比
  192. fixed: {
  193. type: Boolean,
  194. default: !1
  195. },
  196. // 宽高比 w/h
  197. fixedNumber: {
  198. type: Array,
  199. default: () => [1, 1]
  200. },
  201. // 固定大小 禁止改变截图框大小
  202. fixedBox: {
  203. type: Boolean,
  204. default: !1
  205. },
  206. // 输出截图是否缩放
  207. full: {
  208. type: Boolean,
  209. default: !1
  210. },
  211. // 是否可以拖动图片
  212. canMove: {
  213. type: Boolean,
  214. default: !0
  215. },
  216. // 是否可以拖动截图框
  217. canMoveBox: {
  218. type: Boolean,
  219. default: !0
  220. },
  221. // 上传图片按照原始比例显示
  222. original: {
  223. type: Boolean,
  224. default: !1
  225. },
  226. // 截图框能否超过图片
  227. centerBox: {
  228. type: Boolean,
  229. default: !1
  230. },
  231. // 是否根据dpr输出高清图片
  232. high: {
  233. type: Boolean,
  234. default: !0
  235. },
  236. // 截图框展示宽高类型
  237. infoTrue: {
  238. type: Boolean,
  239. default: !1
  240. },
  241. // 可以压缩图片宽高 默认不超过200
  242. maxImgSize: {
  243. type: [Number, String],
  244. default: 2e3
  245. },
  246. // 倍数 可渲染当前截图框的n倍 0 - 1000;
  247. enlarge: {
  248. type: [Number, String],
  249. default: 1
  250. },
  251. // 自动预览的固定宽度
  252. preW: {
  253. type: [Number, String],
  254. default: 0
  255. },
  256. /*
  257. 图片布局方式 mode 实现和css背景一样的效果
  258. contain 居中布局 默认不会缩放 保证图片在容器里面 mode: 'contain'
  259. cover 拉伸布局 填充整个容器 mode: 'cover'
  260. 如果仅有一个数值被给定,这个数值将作为宽度值大小,高度值将被设定为auto。 mode: '50px'
  261. 如果有两个数值被给定,第一个将作为宽度值大小,第二个作为高度值大小。 mode: '50px 60px'
  262. */
  263. mode: {
  264. type: String,
  265. default: "contain"
  266. },
  267. //限制最小区域,可传1以上的数字和字符串,限制长宽都是这么大
  268. // 也可以传数组[90,90]
  269. limitMinSize: {
  270. type: [Number, Array, String],
  271. default: () => 10,
  272. validator: function(t) {
  273. return Array.isArray(t) ? Number(t[0]) >= 0 && Number(t[1]) >= 0 : Number(t) >= 0;
  274. }
  275. },
  276. // 导出时,填充背景颜色
  277. fillColor: {
  278. type: String,
  279. default: ""
  280. }
  281. },
  282. computed: {
  283. cropInfo() {
  284. let t = {};
  285. if (t.top = this.cropOffsertY > 21 ? "-21px" : "0px", t.width = this.cropW > 0 ? this.cropW : 0, t.height = this.cropH > 0 ? this.cropH : 0, this.infoTrue) {
  286. let e = 1;
  287. this.high && !this.full && (e = window.devicePixelRatio), this.enlarge !== 1 & !this.full && (e = Math.abs(Number(this.enlarge))), t.width = t.width * e, t.height = t.height * e, this.full && (t.width = t.width / this.scale, t.height = t.height / this.scale);
  288. }
  289. return t.width = t.width.toFixed(0), t.height = t.height.toFixed(0), t;
  290. },
  291. isIE() {
  292. return !!window.ActiveXObject || "ActiveXObject" in window;
  293. },
  294. passive() {
  295. return this.isIE ? null : {
  296. passive: !1
  297. };
  298. },
  299. // 是否处于左右旋转
  300. isRotateRightOrLeft() {
  301. return [1, -1, 3, -3].includes(this.rotate);
  302. }
  303. },
  304. watch: {
  305. // 如果图片改变, 重新布局
  306. img() {
  307. this.checkedImg();
  308. },
  309. imgs(t) {
  310. t !== "" && this.reload();
  311. },
  312. cropW() {
  313. this.showPreview();
  314. },
  315. cropH() {
  316. this.showPreview();
  317. },
  318. cropOffsertX() {
  319. this.showPreview();
  320. },
  321. cropOffsertY() {
  322. this.showPreview();
  323. },
  324. scale(t, e) {
  325. this.showPreview();
  326. },
  327. x() {
  328. this.showPreview();
  329. },
  330. y() {
  331. this.showPreview();
  332. },
  333. autoCrop(t) {
  334. t && this.goAutoCrop();
  335. },
  336. // 修改了自动截图框
  337. autoCropWidth() {
  338. this.autoCrop && this.goAutoCrop();
  339. },
  340. autoCropHeight() {
  341. this.autoCrop && this.goAutoCrop();
  342. },
  343. mode() {
  344. this.checkedImg();
  345. },
  346. rotate() {
  347. this.showPreview(), this.autoCrop ? this.goAutoCrop(this.cropW, this.cropH) : (this.cropW > 0 || this.cropH > 0) && this.goAutoCrop(this.cropW, this.cropH);
  348. }
  349. },
  350. methods: {
  351. getVersion(t) {
  352. var e = navigator.userAgent.split(" "), s = "";
  353. let i = 0;
  354. const h = new RegExp(t, "i");
  355. for (var r = 0; r < e.length; r++)
  356. h.test(e[r]) && (s = e[r]);
  357. return s ? i = s.split("/")[1].split(".") : i = ["0", "0", "0"], i;
  358. },
  359. checkOrientationImage(t, e, s, i) {
  360. if (this.getVersion("chrome")[0] >= 81)
  361. e = -1;
  362. else if (this.getVersion("safari")[0] >= 605) {
  363. const o = this.getVersion("version");
  364. o[0] > 13 && o[1] > 1 && (e = -1);
  365. } else {
  366. const o = navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?) like mac os/);
  367. if (o) {
  368. let a = o[1];
  369. a = a.split("_"), (a[0] > 13 || a[0] >= 13 && a[1] >= 4) && (e = -1);
  370. }
  371. }
  372. let h = document.createElement("canvas"), r = h.getContext("2d");
  373. switch (r.save(), e) {
  374. case 2:
  375. h.width = s, h.height = i, r.translate(s, 0), r.scale(-1, 1);
  376. break;
  377. case 3:
  378. h.width = s, h.height = i, r.translate(s / 2, i / 2), r.rotate(180 * Math.PI / 180), r.translate(-s / 2, -i / 2);
  379. break;
  380. case 4:
  381. h.width = s, h.height = i, r.translate(0, i), r.scale(1, -1);
  382. break;
  383. case 5:
  384. h.height = s, h.width = i, r.rotate(0.5 * Math.PI), r.scale(1, -1);
  385. break;
  386. case 6:
  387. h.width = i, h.height = s, r.translate(i / 2, s / 2), r.rotate(90 * Math.PI / 180), r.translate(-s / 2, -i / 2);
  388. break;
  389. case 7:
  390. h.height = s, h.width = i, r.rotate(0.5 * Math.PI), r.translate(s, -i), r.scale(-1, 1);
  391. break;
  392. case 8:
  393. h.height = s, h.width = i, r.translate(i / 2, s / 2), r.rotate(-90 * Math.PI / 180), r.translate(-s / 2, -i / 2);
  394. break;
  395. default:
  396. h.width = s, h.height = i;
  397. }
  398. r.drawImage(t, 0, 0, s, i), r.restore(), h.toBlob(
  399. (o) => {
  400. let a = URL.createObjectURL(o);
  401. URL.revokeObjectURL(this.imgs), this.imgs = a;
  402. },
  403. "image/" + this.outputType,
  404. 1
  405. );
  406. },
  407. // checkout img
  408. checkedImg() {
  409. if (this.img === null || this.img === "") {
  410. this.imgs = "", this.clearCrop();
  411. return;
  412. }
  413. this.loading = !0, this.scale = 1, this.rotate = 0, this.imgIsQqualCrop = !1, this.clearCrop();
  414. let t = new Image();
  415. if (t.onload = () => {
  416. if (this.img === "")
  417. return this.$emit("img-load", new Error("图片不能为空")), !1;
  418. let s = t.width, i = t.height;
  419. A.getData(t).then((h) => {
  420. this.orientation = h.orientation || 1;
  421. let r = Number(this.maxImgSize);
  422. if (!this.orientation && s < r & i < r) {
  423. this.imgs = this.img;
  424. return;
  425. }
  426. s > r && (i = i / s * r, s = r), i > r && (s = s / i * r, i = r), this.checkOrientationImage(t, this.orientation, s, i);
  427. }).catch((h) => {
  428. this.$emit("img-load", "error"), this.$emit("img-load-error", h);
  429. });
  430. }, t.onerror = (s) => {
  431. this.$emit("img-load", "error"), this.$emit("img-load-error", s);
  432. }, this.img.substr(0, 4) !== "data" && (t.crossOrigin = ""), this.isIE) {
  433. var e = new XMLHttpRequest();
  434. e.onload = function() {
  435. var s = URL.createObjectURL(this.response);
  436. t.src = s;
  437. }, e.open("GET", this.img, !0), e.responseType = "blob", e.send();
  438. } else
  439. t.src = this.img;
  440. },
  441. // 当按下鼠标键
  442. startMove(t) {
  443. if (t.preventDefault(), this.move && !this.crop) {
  444. if (!this.canMove)
  445. return !1;
  446. this.moveX = ("clientX" in t ? t.clientX : t.touches[0].clientX) - this.x, this.moveY = ("clientY" in t ? t.clientY : t.touches[0].clientY) - this.y, t.touches ? (window.addEventListener("touchmove", this.moveImg), window.addEventListener("touchend", this.leaveImg), t.touches.length == 2 && (this.touches = t.touches, window.addEventListener("touchmove", this.touchScale), window.addEventListener("touchend", this.cancelTouchScale))) : (window.addEventListener("mousemove", this.moveImg), window.addEventListener("mouseup", this.leaveImg)), this.$emit("img-moving", {
  447. moving: !0,
  448. axis: this.getImgAxis()
  449. });
  450. } else
  451. this.cropping = !0, window.addEventListener("mousemove", this.createCrop), window.addEventListener("mouseup", this.endCrop), window.addEventListener("touchmove", this.createCrop), window.addEventListener("touchend", this.endCrop), this.cropOffsertX = t.offsetX ? t.offsetX : t.touches[0].pageX - this.$refs.cropper.offsetLeft, this.cropOffsertY = t.offsetY ? t.offsetY : t.touches[0].pageY - this.$refs.cropper.offsetTop, this.cropX = "clientX" in t ? t.clientX : t.touches[0].clientX, this.cropY = "clientY" in t ? t.clientY : t.touches[0].clientY, this.cropChangeX = this.cropOffsertX, this.cropChangeY = this.cropOffsertY, this.cropW = 0, this.cropH = 0;
  452. },
  453. // 移动端缩放
  454. touchScale(t) {
  455. t.preventDefault();
  456. let e = this.scale;
  457. var s = {
  458. x: this.touches[0].clientX,
  459. y: this.touches[0].clientY
  460. }, i = {
  461. x: t.touches[0].clientX,
  462. y: t.touches[0].clientY
  463. }, h = {
  464. x: this.touches[1].clientX,
  465. y: this.touches[1].clientY
  466. }, r = {
  467. x: t.touches[1].clientX,
  468. y: t.touches[1].clientY
  469. }, o = Math.sqrt(
  470. Math.pow(s.x - h.x, 2) + Math.pow(s.y - h.y, 2)
  471. ), a = Math.sqrt(
  472. Math.pow(i.x - r.x, 2) + Math.pow(i.y - r.y, 2)
  473. ), c = a - o, n = 1;
  474. n = n / this.trueWidth > n / this.trueHeight ? n / this.trueHeight : n / this.trueWidth, n = n > 0.1 ? 0.1 : n;
  475. var l = n * c;
  476. if (!this.touchNow) {
  477. if (this.touchNow = !0, c > 0 ? e += Math.abs(l) : c < 0 && e > Math.abs(l) && (e -= Math.abs(l)), this.touches = t.touches, setTimeout(() => {
  478. this.touchNow = !1;
  479. }, 8), !this.checkoutImgAxis(this.x, this.y, e))
  480. return !1;
  481. this.scale = e;
  482. }
  483. },
  484. cancelTouchScale(t) {
  485. window.removeEventListener("touchmove", this.touchScale);
  486. },
  487. // 移动图片
  488. moveImg(t) {
  489. if (t.preventDefault(), t.touches && t.touches.length === 2)
  490. return this.touches = t.touches, window.addEventListener("touchmove", this.touchScale), window.addEventListener("touchend", this.cancelTouchScale), window.removeEventListener("touchmove", this.moveImg), !1;
  491. let e = "clientX" in t ? t.clientX : t.touches[0].clientX, s = "clientY" in t ? t.clientY : t.touches[0].clientY, i, h;
  492. i = e - this.moveX, h = s - this.moveY, this.$nextTick(() => {
  493. if (this.centerBox) {
  494. let r = this.getImgAxis(i, h, this.scale), o = this.getCropAxis(), a = this.trueHeight * this.scale, c = this.trueWidth * this.scale, n, l, u, p;
  495. switch (this.rotate) {
  496. case 1:
  497. case -1:
  498. case 3:
  499. case -3:
  500. n = this.cropOffsertX - this.trueWidth * (1 - this.scale) / 2 + (a - c) / 2, l = this.cropOffsertY - this.trueHeight * (1 - this.scale) / 2 + (c - a) / 2, u = n - a + this.cropW, p = l - c + this.cropH;
  501. break;
  502. default:
  503. n = this.cropOffsertX - this.trueWidth * (1 - this.scale) / 2, l = this.cropOffsertY - this.trueHeight * (1 - this.scale) / 2, u = n - c + this.cropW, p = l - a + this.cropH;
  504. break;
  505. }
  506. r.x1 >= o.x1 && (i = n), r.y1 >= o.y1 && (h = l), r.x2 <= o.x2 && (i = u), r.y2 <= o.y2 && (h = p);
  507. }
  508. this.x = i, this.y = h, this.$emit("img-moving", {
  509. moving: !0,
  510. axis: this.getImgAxis()
  511. });
  512. });
  513. },
  514. // 移动图片结束
  515. leaveImg(t) {
  516. window.removeEventListener("mousemove", this.moveImg), window.removeEventListener("touchmove", this.moveImg), window.removeEventListener("mouseup", this.leaveImg), window.removeEventListener("touchend", this.leaveImg), this.$emit("img-moving", {
  517. moving: !1,
  518. axis: this.getImgAxis()
  519. });
  520. },
  521. // 缩放图片
  522. scaleImg() {
  523. this.canScale && window.addEventListener(this.support, this.changeSize, this.passive);
  524. },
  525. // 移出框
  526. cancelScale() {
  527. this.canScale && window.removeEventListener(this.support, this.changeSize);
  528. },
  529. // 改变大小函数
  530. changeSize(t) {
  531. t.preventDefault();
  532. let e = this.scale;
  533. var s = t.deltaY || t.wheelDelta, i = navigator.userAgent.indexOf("Firefox");
  534. s = i > 0 ? s * 30 : s, this.isIE && (s = -s);
  535. var h = this.coe;
  536. h = h / this.trueWidth > h / this.trueHeight ? h / this.trueHeight : h / this.trueWidth;
  537. var r = h * s;
  538. r < 0 ? e += Math.abs(r) : e > Math.abs(r) && (e -= Math.abs(r));
  539. let o = r < 0 ? "add" : "reduce";
  540. if (o !== this.coeStatus && (this.coeStatus = o, this.coe = 0.2), this.scaling || (this.scalingSet = setTimeout(() => {
  541. this.scaling = !1, this.coe = this.coe += 0.01;
  542. }, 50)), this.scaling = !0, !this.checkoutImgAxis(this.x, this.y, e))
  543. return !1;
  544. this.scale = e;
  545. },
  546. // 修改图片大小函数
  547. changeScale(t) {
  548. let e = this.scale;
  549. t = t || 1;
  550. var s = 20;
  551. if (s = s / this.trueWidth > s / this.trueHeight ? s / this.trueHeight : s / this.trueWidth, t = t * s, t > 0 ? e += Math.abs(t) : e > Math.abs(t) && (e -= Math.abs(t)), !this.checkoutImgAxis(this.x, this.y, e))
  552. return !1;
  553. this.scale = e;
  554. },
  555. // 创建截图框
  556. createCrop(t) {
  557. t.preventDefault();
  558. var e = "clientX" in t ? t.clientX : t.touches ? t.touches[0].clientX : 0, s = "clientY" in t ? t.clientY : t.touches ? t.touches[0].clientY : 0;
  559. this.$nextTick(() => {
  560. var i = e - this.cropX, h = s - this.cropY;
  561. if (i > 0 ? (this.cropW = i + this.cropChangeX > this.w ? this.w - this.cropChangeX : i, this.cropOffsertX = this.cropChangeX) : (this.cropW = this.w - this.cropChangeX + Math.abs(i) > this.w ? this.cropChangeX : Math.abs(i), this.cropOffsertX = this.cropChangeX + i > 0 ? this.cropChangeX + i : 0), !this.fixed)
  562. h > 0 ? (this.cropH = h + this.cropChangeY > this.h ? this.h - this.cropChangeY : h, this.cropOffsertY = this.cropChangeY) : (this.cropH = this.h - this.cropChangeY + Math.abs(h) > this.h ? this.cropChangeY : Math.abs(h), this.cropOffsertY = this.cropChangeY + h > 0 ? this.cropChangeY + h : 0);
  563. else {
  564. var r = this.cropW / this.fixedNumber[0] * this.fixedNumber[1];
  565. r + this.cropOffsertY > this.h ? (this.cropH = this.h - this.cropOffsertY, this.cropW = this.cropH / this.fixedNumber[1] * this.fixedNumber[0], i > 0 ? this.cropOffsertX = this.cropChangeX : this.cropOffsertX = this.cropChangeX - this.cropW) : this.cropH = r, this.cropOffsertY = this.cropOffsertY;
  566. }
  567. });
  568. },
  569. // 改变截图框大小
  570. changeCropSize(t, e, s, i, h) {
  571. t.preventDefault(), window.addEventListener("mousemove", this.changeCropNow), window.addEventListener("mouseup", this.changeCropEnd), window.addEventListener("touchmove", this.changeCropNow), window.addEventListener("touchend", this.changeCropEnd), this.canChangeX = e, this.canChangeY = s, this.changeCropTypeX = i, this.changeCropTypeY = h, this.cropX = "clientX" in t ? t.clientX : t.touches[0].clientX, this.cropY = "clientY" in t ? t.clientY : t.touches[0].clientY, this.cropOldW = this.cropW, this.cropOldH = this.cropH, this.cropChangeX = this.cropOffsertX, this.cropChangeY = this.cropOffsertY, this.fixed && this.canChangeX && this.canChangeY && (this.canChangeY = 0), this.$emit("change-crop-size", {
  572. width: this.cropW,
  573. height: this.cropH
  574. });
  575. },
  576. // 正在改变
  577. changeCropNow(t) {
  578. t.preventDefault();
  579. var e = "clientX" in t ? t.clientX : t.touches ? t.touches[0].clientX : 0, s = "clientY" in t ? t.clientY : t.touches ? t.touches[0].clientY : 0;
  580. let i = this.w, h = this.h, r = 0, o = 0;
  581. if (this.centerBox) {
  582. let n = this.getImgAxis(), l = n.x2, u = n.y2;
  583. r = n.x1 > 0 ? n.x1 : 0, o = n.y1 > 0 ? n.y1 : 0, i > l && (i = l), h > u && (h = u);
  584. }
  585. const [a, c] = this.checkCropLimitSize();
  586. this.$nextTick(() => {
  587. var n = e - this.cropX, l = s - this.cropY;
  588. if (this.canChangeX && (this.changeCropTypeX === 1 ? this.cropOldW - n < a ? (this.cropW = a, this.cropOffsertX = this.cropOldW + this.cropChangeX - r - a) : this.cropOldW - n > 0 ? (this.cropW = i - this.cropChangeX - n <= i - r ? this.cropOldW - n : this.cropOldW + this.cropChangeX - r, this.cropOffsertX = i - this.cropChangeX - n <= i - r ? this.cropChangeX + n : r) : (this.cropW = Math.abs(n) + this.cropChangeX <= i ? Math.abs(n) - this.cropOldW : i - this.cropOldW - this.cropChangeX, this.cropOffsertX = this.cropChangeX + this.cropOldW) : this.changeCropTypeX === 2 && (this.cropOldW + n < a ? this.cropW = a : this.cropOldW + n > 0 ? (this.cropW = this.cropOldW + n + this.cropOffsertX <= i ? this.cropOldW + n : i - this.cropOffsertX, this.cropOffsertX = this.cropChangeX) : (this.cropW = i - this.cropChangeX + Math.abs(n + this.cropOldW) <= i - r ? Math.abs(n + this.cropOldW) : this.cropChangeX - r, this.cropOffsertX = i - this.cropChangeX + Math.abs(n + this.cropOldW) <= i - r ? this.cropChangeX - Math.abs(n + this.cropOldW) : r))), this.canChangeY && (this.changeCropTypeY === 1 ? this.cropOldH - l < c ? (this.cropH = c, this.cropOffsertY = this.cropOldH + this.cropChangeY - o - c) : this.cropOldH - l > 0 ? (this.cropH = h - this.cropChangeY - l <= h - o ? this.cropOldH - l : this.cropOldH + this.cropChangeY - o, this.cropOffsertY = h - this.cropChangeY - l <= h - o ? this.cropChangeY + l : o) : (this.cropH = Math.abs(l) + this.cropChangeY <= h ? Math.abs(l) - this.cropOldH : h - this.cropOldH - this.cropChangeY, this.cropOffsertY = this.cropChangeY + this.cropOldH) : this.changeCropTypeY === 2 && (this.cropOldH + l < c ? this.cropH = c : this.cropOldH + l > 0 ? (this.cropH = this.cropOldH + l + this.cropOffsertY <= h ? this.cropOldH + l : h - this.cropOffsertY, this.cropOffsertY = this.cropChangeY) : (this.cropH = h - this.cropChangeY + Math.abs(l + this.cropOldH) <= h - o ? Math.abs(l + this.cropOldH) : this.cropChangeY - o, this.cropOffsertY = h - this.cropChangeY + Math.abs(l + this.cropOldH) <= h - o ? this.cropChangeY - Math.abs(l + this.cropOldH) : o))), this.canChangeX && this.fixed) {
  589. var u = this.cropW / this.fixedNumber[0] * this.fixedNumber[1];
  590. u < c ? (this.cropH = c, this.cropW = this.fixedNumber[0] * c / this.fixedNumber[1], this.changeCropTypeX === 1 && (this.cropOffsertX = this.cropChangeX + (this.cropOldW - this.cropW))) : u + this.cropOffsertY > h ? (this.cropH = h - this.cropOffsertY, this.cropW = this.cropH / this.fixedNumber[1] * this.fixedNumber[0], this.changeCropTypeX === 1 && (this.cropOffsertX = this.cropChangeX + (this.cropOldW - this.cropW))) : this.cropH = u;
  591. }
  592. if (this.canChangeY && this.fixed) {
  593. var p = this.cropH / this.fixedNumber[1] * this.fixedNumber[0];
  594. p < a ? (this.cropW = a, this.cropH = this.fixedNumber[1] * a / this.fixedNumber[0], this.cropOffsertY = this.cropOldH + this.cropChangeY - this.cropH) : p + this.cropOffsertX > i ? (this.cropW = i - this.cropOffsertX, this.cropH = this.cropW / this.fixedNumber[0] * this.fixedNumber[1]) : this.cropW = p;
  595. }
  596. });
  597. },
  598. checkCropLimitSize() {
  599. let { cropW: t, cropH: e, limitMinSize: s } = this, i = new Array();
  600. return Array.isArray(s) ? i = s : i = [s, s], t = parseFloat(i[0]), e = parseFloat(i[1]), [t, e];
  601. },
  602. // 结束改变
  603. changeCropEnd(t) {
  604. window.removeEventListener("mousemove", this.changeCropNow), window.removeEventListener("mouseup", this.changeCropEnd), window.removeEventListener("touchmove", this.changeCropNow), window.removeEventListener("touchend", this.changeCropEnd);
  605. },
  606. // 根据比例x/y,最小宽度,最小高度,现有宽度,现有高度,得到应该有的宽度和高度
  607. calculateSize(t, e, s, i, h, r) {
  608. const o = t / e;
  609. let a = h, c = r;
  610. return a < s && (a = s, c = Math.ceil(a / o)), c < i && (c = i, a = Math.ceil(c * o), a < s && (a = s, c = Math.ceil(a / o))), a < h && (a = h, c = Math.ceil(a / o)), c < r && (c = r, a = Math.ceil(c * o)), { width: a, height: c };
  611. },
  612. // 创建完成
  613. endCrop() {
  614. this.cropW === 0 && this.cropH === 0 && (this.cropping = !1);
  615. let [t, e] = this.checkCropLimitSize();
  616. const { width: s, height: i } = this.fixed ? this.calculateSize(
  617. this.fixedNumber[0],
  618. this.fixedNumber[1],
  619. t,
  620. e,
  621. this.cropW,
  622. this.cropH
  623. ) : { width: t, height: e };
  624. s > this.cropW && (this.cropW = s, this.cropOffsertX + s > this.w && (this.cropOffsertX = this.w - s)), i > this.cropH && (this.cropH = i, this.cropOffsertY + i > this.h && (this.cropOffsertY = this.h - i)), window.removeEventListener("mousemove", this.createCrop), window.removeEventListener("mouseup", this.endCrop), window.removeEventListener("touchmove", this.createCrop), window.removeEventListener("touchend", this.endCrop);
  625. },
  626. // 开始截图
  627. startCrop() {
  628. this.crop = !0;
  629. },
  630. // 停止截图
  631. stopCrop() {
  632. this.crop = !1;
  633. },
  634. // 清除截图
  635. clearCrop() {
  636. this.cropping = !1, this.cropW = 0, this.cropH = 0;
  637. },
  638. // 截图移动
  639. cropMove(t) {
  640. if (t.preventDefault(), !this.canMoveBox)
  641. return this.crop = !1, this.startMove(t), !1;
  642. if (t.touches && t.touches.length === 2)
  643. return this.crop = !1, this.startMove(t), this.leaveCrop(), !1;
  644. window.addEventListener("mousemove", this.moveCrop), window.addEventListener("mouseup", this.leaveCrop), window.addEventListener("touchmove", this.moveCrop), window.addEventListener("touchend", this.leaveCrop);
  645. let e = "clientX" in t ? t.clientX : t.touches[0].clientX, s = "clientY" in t ? t.clientY : t.touches[0].clientY, i, h;
  646. i = e - this.cropOffsertX, h = s - this.cropOffsertY, this.cropX = i, this.cropY = h, this.$emit("crop-moving", {
  647. moving: !0,
  648. axis: this.getCropAxis()
  649. });
  650. },
  651. moveCrop(t, e) {
  652. let s = 0, i = 0;
  653. t && (t.preventDefault(), s = "clientX" in t ? t.clientX : t.touches[0].clientX, i = "clientY" in t ? t.clientY : t.touches[0].clientY), this.$nextTick(() => {
  654. let h, r, o = s - this.cropX, a = i - this.cropY;
  655. if (e && (o = this.cropOffsertX, a = this.cropOffsertY), o <= 0 ? h = 0 : o + this.cropW > this.w ? h = this.w - this.cropW : h = o, a <= 0 ? r = 0 : a + this.cropH > this.h ? r = this.h - this.cropH : r = a, this.centerBox) {
  656. let c = this.getImgAxis();
  657. h <= c.x1 && (h = c.x1), h + this.cropW > c.x2 && (h = c.x2 - this.cropW), r <= c.y1 && (r = c.y1), r + this.cropH > c.y2 && (r = c.y2 - this.cropH);
  658. }
  659. this.cropOffsertX = h, this.cropOffsertY = r, this.$emit("crop-moving", {
  660. moving: !0,
  661. axis: this.getCropAxis()
  662. });
  663. });
  664. },
  665. // 算出不同场景下面 图片相对于外层容器的坐标轴
  666. getImgAxis(t, e, s) {
  667. t = t || this.x, e = e || this.y, s = s || this.scale;
  668. let i = {
  669. x1: 0,
  670. x2: 0,
  671. y1: 0,
  672. y2: 0
  673. }, h = this.trueWidth * s, r = this.trueHeight * s;
  674. switch (this.rotate) {
  675. case 0:
  676. i.x1 = t + this.trueWidth * (1 - s) / 2, i.x2 = i.x1 + this.trueWidth * s, i.y1 = e + this.trueHeight * (1 - s) / 2, i.y2 = i.y1 + this.trueHeight * s;
  677. break;
  678. case 1:
  679. case -1:
  680. case 3:
  681. case -3:
  682. i.x1 = t + this.trueWidth * (1 - s) / 2 + (h - r) / 2, i.x2 = i.x1 + this.trueHeight * s, i.y1 = e + this.trueHeight * (1 - s) / 2 + (r - h) / 2, i.y2 = i.y1 + this.trueWidth * s;
  683. break;
  684. default:
  685. i.x1 = t + this.trueWidth * (1 - s) / 2, i.x2 = i.x1 + this.trueWidth * s, i.y1 = e + this.trueHeight * (1 - s) / 2, i.y2 = i.y1 + this.trueHeight * s;
  686. break;
  687. }
  688. return i;
  689. },
  690. // 获取截图框的坐标轴
  691. getCropAxis() {
  692. let t = {
  693. x1: 0,
  694. x2: 0,
  695. y1: 0,
  696. y2: 0
  697. };
  698. return t.x1 = this.cropOffsertX, t.x2 = t.x1 + this.cropW, t.y1 = this.cropOffsertY, t.y2 = t.y1 + this.cropH, t;
  699. },
  700. leaveCrop(t) {
  701. window.removeEventListener("mousemove", this.moveCrop), window.removeEventListener("mouseup", this.leaveCrop), window.removeEventListener("touchmove", this.moveCrop), window.removeEventListener("touchend", this.leaveCrop), this.$emit("crop-moving", {
  702. moving: !1,
  703. axis: this.getCropAxis()
  704. });
  705. },
  706. getCropChecked(t) {
  707. let e = document.createElement("canvas"), s = e.getContext("2d"), i = new Image(), h = this.rotate, r = this.trueWidth, o = this.trueHeight, a = this.cropOffsertX, c = this.cropOffsertY;
  708. i.onload = () => {
  709. if (this.cropW !== 0) {
  710. let p = 1;
  711. this.high & !this.full && (p = window.devicePixelRatio), this.enlarge !== 1 & !this.full && (p = Math.abs(Number(this.enlarge)));
  712. let f = this.cropW * p, O = this.cropH * p, d = r * this.scale * p, g = o * this.scale * p, v = (this.x - a + this.trueWidth * (1 - this.scale) / 2) * p, w = (this.y - c + this.trueHeight * (1 - this.scale) / 2) * p;
  713. switch (u(f, O), s.save(), h) {
  714. case 0:
  715. this.full ? (u(f / this.scale, O / this.scale), s.drawImage(
  716. i,
  717. v / this.scale,
  718. w / this.scale,
  719. d / this.scale,
  720. g / this.scale
  721. )) : s.drawImage(i, v, w, d, g);
  722. break;
  723. case 1:
  724. case -3:
  725. this.full ? (u(f / this.scale, O / this.scale), v = v / this.scale + (d / this.scale - g / this.scale) / 2, w = w / this.scale + (g / this.scale - d / this.scale) / 2, s.rotate(h * 90 * Math.PI / 180), s.drawImage(
  726. i,
  727. w,
  728. -v - g / this.scale,
  729. d / this.scale,
  730. g / this.scale
  731. )) : (v = v + (d - g) / 2, w = w + (g - d) / 2, s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, w, -v - g, d, g));
  732. break;
  733. case 2:
  734. case -2:
  735. this.full ? (u(f / this.scale, O / this.scale), s.rotate(h * 90 * Math.PI / 180), v = v / this.scale, w = w / this.scale, s.drawImage(
  736. i,
  737. -v - d / this.scale,
  738. -w - g / this.scale,
  739. d / this.scale,
  740. g / this.scale
  741. )) : (s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, -v - d, -w - g, d, g));
  742. break;
  743. case 3:
  744. case -1:
  745. this.full ? (u(f / this.scale, O / this.scale), v = v / this.scale + (d / this.scale - g / this.scale) / 2, w = w / this.scale + (g / this.scale - d / this.scale) / 2, s.rotate(h * 90 * Math.PI / 180), s.drawImage(
  746. i,
  747. -w - d / this.scale,
  748. v,
  749. d / this.scale,
  750. g / this.scale
  751. )) : (v = v + (d - g) / 2, w = w + (g - d) / 2, s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, -w - d, v, d, g));
  752. break;
  753. default:
  754. this.full ? (u(f / this.scale, O / this.scale), s.drawImage(
  755. i,
  756. v / this.scale,
  757. w / this.scale,
  758. d / this.scale,
  759. g / this.scale
  760. )) : s.drawImage(i, v, w, d, g);
  761. }
  762. s.restore();
  763. } else {
  764. let p = r * this.scale, f = o * this.scale;
  765. switch (s.save(), h) {
  766. case 0:
  767. u(p, f), s.drawImage(i, 0, 0, p, f);
  768. break;
  769. case 1:
  770. case -3:
  771. u(f, p), s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, 0, -f, p, f);
  772. break;
  773. case 2:
  774. case -2:
  775. u(p, f), s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, -p, -f, p, f);
  776. break;
  777. case 3:
  778. case -1:
  779. u(f, p), s.rotate(h * 90 * Math.PI / 180), s.drawImage(i, -p, 0, p, f);
  780. break;
  781. default:
  782. u(p, f), s.drawImage(i, 0, 0, p, f);
  783. }
  784. s.restore();
  785. }
  786. t(e);
  787. };
  788. var n = this.img.substr(0, 4);
  789. n !== "data" && (i.crossOrigin = "Anonymous"), i.src = this.imgs;
  790. const l = this.fillColor;
  791. function u(p, f) {
  792. e.width = Math.round(p), e.height = Math.round(f), l && (s.fillStyle = l, s.fillRect(0, 0, e.width, e.height));
  793. }
  794. },
  795. // 获取转换成base64 的图片信息
  796. getCropData(t) {
  797. this.getCropChecked((e) => {
  798. t(e.toDataURL("image/" + this.outputType, this.outputSize));
  799. });
  800. },
  801. //canvas获取为blob对象
  802. getCropBlob(t) {
  803. this.getCropChecked((e) => {
  804. e.toBlob(
  805. (s) => t(s),
  806. "image/" + this.outputType,
  807. this.outputSize
  808. );
  809. });
  810. },
  811. // 自动预览函数
  812. showPreview() {
  813. if (this.isCanShow)
  814. this.isCanShow = !1, setTimeout(() => {
  815. this.isCanShow = !0;
  816. }, 16);
  817. else
  818. return !1;
  819. let t = this.cropW, e = this.cropH, s = this.scale;
  820. var i = {};
  821. i.div = {
  822. width: `${t}px`,
  823. height: `${e}px`
  824. };
  825. let h = (this.x - this.cropOffsertX) / s, r = (this.y - this.cropOffsertY) / s, o = 0;
  826. i.w = t, i.h = e, i.url = this.imgs, i.img = {
  827. width: `${this.trueWidth}px`,
  828. height: `${this.trueHeight}px`,
  829. transform: `scale(${s})translate3d(${h}px, ${r}px, ${o}px)rotateZ(${this.rotate * 90}deg)`
  830. }, i.html = `
  831. <div class="show-preview" style="width: ${i.w}px; height: ${i.h}px,; overflow: hidden">
  832. <div style="width: ${t}px; height: ${e}px">
  833. <img src=${i.url} style="width: ${this.trueWidth}px; height: ${this.trueHeight}px; transform:
  834. scale(${s})translate3d(${h}px, ${r}px, ${o}px)rotateZ(${this.rotate * 90}deg)">
  835. </div>
  836. </div>`, this.$emit("real-time", i);
  837. },
  838. // reload 图片布局函数
  839. reload() {
  840. let t = new Image();
  841. t.onload = () => {
  842. this.w = parseFloat(window.getComputedStyle(this.$refs.cropper).width), this.h = parseFloat(window.getComputedStyle(this.$refs.cropper).height), this.trueWidth = t.width, this.trueHeight = t.height, this.original ? this.scale = 1 : this.scale = this.checkedMode(), this.$nextTick(() => {
  843. this.x = -(this.trueWidth - this.trueWidth * this.scale) / 2 + (this.w - this.trueWidth * this.scale) / 2, this.y = -(this.trueHeight - this.trueHeight * this.scale) / 2 + (this.h - this.trueHeight * this.scale) / 2, this.loading = !1, this.autoCrop && this.goAutoCrop(), this.$emit("img-load", "success"), setTimeout(() => {
  844. this.showPreview();
  845. }, 20);
  846. });
  847. }, t.onerror = () => {
  848. this.$emit("img-load", "error");
  849. }, t.src = this.imgs;
  850. },
  851. // 背景布局的函数
  852. checkedMode() {
  853. let t = 1, e = this.trueWidth, s = this.trueHeight;
  854. const i = this.mode.split(" ");
  855. switch (i[0]) {
  856. case "contain":
  857. this.trueWidth > this.w && (t = this.w / this.trueWidth), this.trueHeight * t > this.h && (t = this.h / this.trueHeight);
  858. break;
  859. case "cover":
  860. e = this.w, t = e / this.trueWidth, s = s * t, s < this.h && (s = this.h, t = s / this.trueHeight);
  861. break;
  862. default:
  863. try {
  864. let h = i[0];
  865. if (h.search("px") !== -1) {
  866. h = h.replace("px", ""), e = parseFloat(h);
  867. const r = e / this.trueWidth;
  868. let o = 1, a = i[1];
  869. a.search("px") !== -1 && (a = a.replace("px", ""), s = parseFloat(a), o = s / this.trueHeight), t = Math.min(r, o);
  870. }
  871. if (h.search("%") !== -1 && (h = h.replace("%", ""), e = parseFloat(h) / 100 * this.w, t = e / this.trueWidth), i.length === 2 && h === "auto") {
  872. let r = i[1];
  873. r.search("px") !== -1 && (r = r.replace("px", ""), s = parseFloat(r), t = s / this.trueHeight), r.search("%") !== -1 && (r = r.replace("%", ""), s = parseFloat(r) / 100 * this.h, t = s / this.trueHeight);
  874. }
  875. } catch (h) {
  876. t = 1;
  877. }
  878. }
  879. return t;
  880. },
  881. // 自动截图函数
  882. goAutoCrop(t, e) {
  883. if (this.imgs === "" || this.imgs === null)
  884. return;
  885. this.clearCrop(), this.cropping = !0;
  886. let s = this.w, i = this.h;
  887. if (this.centerBox) {
  888. const o = Math.abs(this.rotate) % 2 > 0;
  889. let a = (o ? this.trueHeight : this.trueWidth) * this.scale, c = (o ? this.trueWidth : this.trueHeight) * this.scale;
  890. s = a < s ? a : s, i = c < i ? c : i;
  891. }
  892. var h = t || parseFloat(this.autoCropWidth), r = e || parseFloat(this.autoCropHeight);
  893. (h === 0 || r === 0) && (h = s * 0.8, r = i * 0.8), h = h > s ? s : h, r = r > i ? i : r, this.fixed && (r = h / this.fixedNumber[0] * this.fixedNumber[1]), r > this.h && (r = this.h, h = r / this.fixedNumber[1] * this.fixedNumber[0]), this.changeCrop(h, r);
  894. },
  895. // 手动改变截图框大小函数
  896. changeCrop(t, e) {
  897. if (this.centerBox) {
  898. let s = this.getImgAxis();
  899. t > s.x2 - s.x1 && (t = s.x2 - s.x1, e = t / this.fixedNumber[0] * this.fixedNumber[1]), e > s.y2 - s.y1 && (e = s.y2 - s.y1, t = e / this.fixedNumber[1] * this.fixedNumber[0]);
  900. }
  901. this.cropW = t, this.cropH = e, this.checkCropLimitSize(), this.$nextTick(() => {
  902. this.cropOffsertX = (this.w - this.cropW) / 2, this.cropOffsertY = (this.h - this.cropH) / 2, this.centerBox && this.moveCrop(null, !0);
  903. });
  904. },
  905. // 重置函数, 恢复组件置初始状态
  906. refresh() {
  907. this.img, this.imgs = "", this.scale = 1, this.crop = !1, this.rotate = 0, this.w = 0, this.h = 0, this.trueWidth = 0, this.trueHeight = 0, this.imgIsQqualCrop = !1, this.clearCrop(), this.$nextTick(() => {
  908. this.checkedImg();
  909. });
  910. },
  911. // 向左边旋转
  912. rotateLeft() {
  913. this.rotate = this.rotate <= -3 ? 0 : this.rotate - 1;
  914. },
  915. // 向右边旋转
  916. rotateRight() {
  917. this.rotate = this.rotate >= 3 ? 0 : this.rotate + 1;
  918. },
  919. // 清除旋转
  920. rotateClear() {
  921. this.rotate = 0;
  922. },
  923. // 图片坐标点校验
  924. checkoutImgAxis(t, e, s) {
  925. t = t || this.x, e = e || this.y, s = s || this.scale;
  926. let i = !0;
  927. if (this.centerBox) {
  928. let h = this.getImgAxis(t, e, s), r = this.getCropAxis();
  929. h.x1 >= r.x1 && (i = !1), h.x2 <= r.x2 && (i = !1), h.y1 >= r.y1 && (i = !1), h.y2 <= r.y2 && (i = !1), i || this.changeImgScale(h, r, s);
  930. }
  931. return i;
  932. },
  933. // 缩放图片,将图片坐标适配截图框坐标
  934. changeImgScale(t, e, s) {
  935. let i = this.trueWidth, h = this.trueHeight, r = i * s, o = h * s;
  936. if (r >= this.cropW && o >= this.cropH)
  937. this.scale = s;
  938. else {
  939. const a = this.cropW / i, c = this.cropH / h, n = this.cropH <= h * a ? a : c;
  940. this.scale = n, r = i * n, o = h * n;
  941. }
  942. this.imgIsQqualCrop || (t.x1 >= e.x1 && (this.isRotateRightOrLeft ? this.x = e.x1 - (i - r) / 2 - (r - o) / 2 : this.x = e.x1 - (i - r) / 2), t.x2 <= e.x2 && (this.isRotateRightOrLeft ? this.x = e.x1 - (i - r) / 2 - (r - o) / 2 - o + this.cropW : this.x = e.x2 - (i - r) / 2 - r), t.y1 >= e.y1 && (this.isRotateRightOrLeft ? this.y = e.y1 - (h - o) / 2 - (o - r) / 2 : this.y = e.y1 - (h - o) / 2), t.y2 <= e.y2 && (this.isRotateRightOrLeft ? this.y = e.y2 - (h - o) / 2 - (o - r) / 2 - r : this.y = e.y2 - (h - o) / 2 - o)), (r < this.cropW || o < this.cropH) && (this.imgIsQqualCrop = !0);
  943. }
  944. },
  945. mounted() {
  946. this.support = "onwheel" in document.createElement("div") ? "wheel" : document.onmousewheel !== void 0 ? "mousewheel" : "DOMMouseScroll";
  947. let t = this;
  948. var e = navigator.userAgent;
  949. this.isIOS = !!e.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), HTMLCanvasElement.prototype.toBlob || Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
  950. value: function(s, i, h) {
  951. for (var r = atob(this.toDataURL(i, h).split(",")[1]), o = r.length, a = new Uint8Array(o), c = 0; c < o; c++)
  952. a[c] = r.charCodeAt(c);
  953. s(new Blob([a], { type: t.type || "image/png" }));
  954. }
  955. }), this.showPreview(), this.checkedImg();
  956. },
  957. unmounted() {
  958. window.removeEventListener("mousemove", this.moveCrop), window.removeEventListener("mouseup", this.leaveCrop), window.removeEventListener("touchmove", this.moveCrop), window.removeEventListener("touchend", this.leaveCrop), this.cancelScale();
  959. }
  960. }), ot = {
  961. key: 0,
  962. class: "cropper-box"
  963. }, at = ["src"], ct = { class: "cropper-view-box" }, nt = ["src"], lt = { key: 1 };
  964. function pt(t, e, s, i, h, r) {
  965. return W(), I("div", {
  966. class: "vue-cropper",
  967. ref: "cropper",
  968. onMouseover: e[28] || (e[28] = (...o) => t.scaleImg && t.scaleImg(...o)),
  969. onMouseout: e[29] || (e[29] = (...o) => t.cancelScale && t.cancelScale(...o))
  970. }, [
  971. t.imgs ? (W(), I("div", ot, [
  972. T(x("div", {
  973. class: "cropper-box-canvas",
  974. style: H({
  975. width: t.trueWidth + "px",
  976. height: t.trueHeight + "px",
  977. transform: "scale(" + t.scale + "," + t.scale + ") translate3d(" + t.x / t.scale + "px," + t.y / t.scale + "px,0)rotateZ(" + t.rotate * 90 + "deg)"
  978. })
  979. }, [
  980. x("img", {
  981. src: t.imgs,
  982. alt: "cropper-img",
  983. ref: "cropperImg"
  984. }, null, 8, at)
  985. ], 4), [
  986. [N, !t.loading]
  987. ])
  988. ])) : k("", !0),
  989. x("div", {
  990. class: F(["cropper-drag-box", { "cropper-move": t.move && !t.crop, "cropper-crop": t.crop, "cropper-modal": t.cropping }]),
  991. onMousedown: e[0] || (e[0] = (...o) => t.startMove && t.startMove(...o)),
  992. onTouchstart: e[1] || (e[1] = (...o) => t.startMove && t.startMove(...o))
  993. }, null, 34),
  994. T(x("div", {
  995. class: "cropper-crop-box",
  996. style: H({
  997. width: t.cropW + "px",
  998. height: t.cropH + "px",
  999. transform: "translate3d(" + t.cropOffsertX + "px," + t.cropOffsertY + "px,0)"
  1000. })
  1001. }, [
  1002. x("span", ct, [
  1003. x("img", {
  1004. style: H({
  1005. width: t.trueWidth + "px",
  1006. height: t.trueHeight + "px",
  1007. transform: "scale(" + t.scale + "," + t.scale + ") translate3d(" + (t.x - t.cropOffsertX) / t.scale + "px," + (t.y - t.cropOffsertY) / t.scale + "px,0)rotateZ(" + t.rotate * 90 + "deg)"
  1008. }),
  1009. src: t.imgs,
  1010. alt: "cropper-img"
  1011. }, null, 12, nt)
  1012. ]),
  1013. x("span", {
  1014. class: "cropper-face cropper-move",
  1015. onMousedown: e[2] || (e[2] = (...o) => t.cropMove && t.cropMove(...o)),
  1016. onTouchstart: e[3] || (e[3] = (...o) => t.cropMove && t.cropMove(...o))
  1017. }, null, 32),
  1018. t.info ? (W(), I("span", {
  1019. key: 0,
  1020. class: "crop-info",
  1021. style: H({ top: t.cropInfo.top })
  1022. }, S(t.cropInfo.width) + " × " + S(t.cropInfo.height), 5)) : k("", !0),
  1023. t.fixedBox ? k("", !0) : (W(), I("span", lt, [
  1024. x("span", {
  1025. class: "crop-line line-w",
  1026. onMousedown: e[4] || (e[4] = (o) => t.changeCropSize(o, !1, !0, 0, 1)),
  1027. onTouchstart: e[5] || (e[5] = (o) => t.changeCropSize(o, !1, !0, 0, 1))
  1028. }, null, 32),
  1029. x("span", {
  1030. class: "crop-line line-a",
  1031. onMousedown: e[6] || (e[6] = (o) => t.changeCropSize(o, !0, !1, 1, 0)),
  1032. onTouchstart: e[7] || (e[7] = (o) => t.changeCropSize(o, !0, !1, 1, 0))
  1033. }, null, 32),
  1034. x("span", {
  1035. class: "crop-line line-s",
  1036. onMousedown: e[8] || (e[8] = (o) => t.changeCropSize(o, !1, !0, 0, 2)),
  1037. onTouchstart: e[9] || (e[9] = (o) => t.changeCropSize(o, !1, !0, 0, 2))
  1038. }, null, 32),
  1039. x("span", {
  1040. class: "crop-line line-d",
  1041. onMousedown: e[10] || (e[10] = (o) => t.changeCropSize(o, !0, !1, 2, 0)),
  1042. onTouchstart: e[11] || (e[11] = (o) => t.changeCropSize(o, !0, !1, 2, 0))
  1043. }, null, 32),
  1044. x("span", {
  1045. class: "crop-point point1",
  1046. onMousedown: e[12] || (e[12] = (o) => t.changeCropSize(o, !0, !0, 1, 1)),
  1047. onTouchstart: e[13] || (e[13] = (o) => t.changeCropSize(o, !0, !0, 1, 1))
  1048. }, null, 32),
  1049. x("span", {
  1050. class: "crop-point point2",
  1051. onMousedown: e[14] || (e[14] = (o) => t.changeCropSize(o, !1, !0, 0, 1)),
  1052. onTouchstart: e[15] || (e[15] = (o) => t.changeCropSize(o, !1, !0, 0, 1))
  1053. }, null, 32),
  1054. x("span", {
  1055. class: "crop-point point3",
  1056. onMousedown: e[16] || (e[16] = (o) => t.changeCropSize(o, !0, !0, 2, 1)),
  1057. onTouchstart: e[17] || (e[17] = (o) => t.changeCropSize(o, !0, !0, 2, 1))
  1058. }, null, 32),
  1059. x("span", {
  1060. class: "crop-point point4",
  1061. onMousedown: e[18] || (e[18] = (o) => t.changeCropSize(o, !0, !1, 1, 0)),
  1062. onTouchstart: e[19] || (e[19] = (o) => t.changeCropSize(o, !0, !1, 1, 0))
  1063. }, null, 32),
  1064. x("span", {
  1065. class: "crop-point point5",
  1066. onMousedown: e[20] || (e[20] = (o) => t.changeCropSize(o, !0, !1, 2, 0)),
  1067. onTouchstart: e[21] || (e[21] = (o) => t.changeCropSize(o, !0, !1, 2, 0))
  1068. }, null, 32),
  1069. x("span", {
  1070. class: "crop-point point6",
  1071. onMousedown: e[22] || (e[22] = (o) => t.changeCropSize(o, !0, !0, 1, 2)),
  1072. onTouchstart: e[23] || (e[23] = (o) => t.changeCropSize(o, !0, !0, 1, 2))
  1073. }, null, 32),
  1074. x("span", {
  1075. class: "crop-point point7",
  1076. onMousedown: e[24] || (e[24] = (o) => t.changeCropSize(o, !1, !0, 0, 2)),
  1077. onTouchstart: e[25] || (e[25] = (o) => t.changeCropSize(o, !1, !0, 0, 2))
  1078. }, null, 32),
  1079. x("span", {
  1080. class: "crop-point point8",
  1081. onMousedown: e[26] || (e[26] = (o) => t.changeCropSize(o, !0, !0, 2, 2)),
  1082. onTouchstart: e[27] || (e[27] = (o) => t.changeCropSize(o, !0, !0, 2, 2))
  1083. }, null, 32)
  1084. ]))
  1085. ], 4), [
  1086. [N, t.cropping]
  1087. ])
  1088. ], 544);
  1089. }
  1090. const ut = /* @__PURE__ */ ht(rt, [["render", pt], ["__scopeId", "data-v-a742df44"]]), dt = { class: "cropper-preview" }, gt = ["src"], ft = {
  1091. key: 1,
  1092. class: "cropper-preview-tip"
  1093. }, mt = {
  1094. __name: "CropperModal",
  1095. emits: ["cropper-ok", "cropper-no"],
  1096. setup(t, { expose: e, emit: s }) {
  1097. const i = s, h = C(!1), r = C(!1), o = C(800), a = C(360), c = C(""), n = C(""), l = C("png"), u = C(!1), p = C([1, 1]), f = C(320), O = C(320), d = C({}), g = C();
  1098. let v = {};
  1099. function w(m) {
  1100. if (m === 0 || m === "0") return null;
  1101. if (typeof m == "string" && m.includes(":")) {
  1102. const [y, Y] = m.split(":").map((L) => Number(L));
  1103. return y > 0 && Y > 0 ? [y, Y] : [1, 1];
  1104. }
  1105. const b = Number(m);
  1106. return b > 0 ? [b, 1] : [1, 1];
  1107. }
  1108. function z(m) {
  1109. if (!m) return [320, 320];
  1110. const y = m[0] / m[1];
  1111. return y >= 1 ? [320, Math.round(320 / y)] : [Math.round(320 * y), 320];
  1112. }
  1113. const _ = (m = {}) => {
  1114. const b = w(m.aspectRatio), [y, Y] = z(b);
  1115. v = m, n.value = m.img || "", c.value = m.title || window.$l("图片裁剪"), l.value = m.outputType || "png", u.value = !b, p.value = b || [1, 1], f.value = y, O.value = Y, d.value = {}, r.value = !1, h.value = !0;
  1116. }, B = (m) => {
  1117. d.value = m || {};
  1118. }, E = () => {
  1119. h.value = !1, r.value = !1, i("cropper-no");
  1120. }, P = () => {
  1121. g.value && (r.value = !0, g.value.getCropBlob((m) => {
  1122. r.value = !1, h.value = !1, i("cropper-ok", { blob: m, file: v.file });
  1123. }));
  1124. };
  1125. return e({ edit: _ }), (m, b) => {
  1126. const y = J, Y = G, L = Z, R = q("s-modal");
  1127. return W(), V(R, {
  1128. value: h.value,
  1129. "onUpdate:value": b[0] || (b[0] = (U) => h.value = U),
  1130. title: c.value,
  1131. width: o.value,
  1132. "mask-closable": !1,
  1133. onHandCancel: E
  1134. }, {
  1135. footer: X(() => [
  1136. M(L, { onClick: E }, {
  1137. default: X(() => [
  1138. $(S(m.$l("取消")), 1)
  1139. ]),
  1140. _: 1
  1141. }),
  1142. M(L, {
  1143. type: "primary",
  1144. loading: r.value,
  1145. onClick: P
  1146. }, {
  1147. default: X(() => [
  1148. $(S(m.$l("保存")), 1)
  1149. ]),
  1150. _: 1
  1151. }, 8, ["loading"])
  1152. ]),
  1153. default: X(() => [
  1154. M(Y, { gutter: 16 }, {
  1155. default: X(() => [
  1156. M(y, {
  1157. xs: 24,
  1158. md: 12,
  1159. style: H({ height: a.value + "px" })
  1160. }, {
  1161. default: X(() => [
  1162. M(j(ut), {
  1163. ref_key: "cropper",
  1164. ref: g,
  1165. img: n.value,
  1166. "auto-crop": !0,
  1167. fixed: !u.value,
  1168. "fixed-number": p.value,
  1169. "fixed-box": !1,
  1170. "center-box": !0,
  1171. "can-move": !0,
  1172. "can-scale": !0,
  1173. info: !0,
  1174. "info-true": !0,
  1175. full: !1,
  1176. "auto-crop-width": f.value,
  1177. "auto-crop-height": O.value,
  1178. "output-type": l.value,
  1179. "output-size": 0.92,
  1180. style: { width: "100%", height: "100%" },
  1181. onRealTime: B
  1182. }, null, 8, ["img", "fixed", "fixed-number", "auto-crop-width", "auto-crop-height", "output-type"])
  1183. ]),
  1184. _: 1
  1185. }, 8, ["style"]),
  1186. M(y, {
  1187. xs: 24,
  1188. md: 12,
  1189. style: H({ height: a.value + "px" })
  1190. }, {
  1191. default: X(() => [
  1192. x("div", dt, [
  1193. d.value.url ? (W(), I("div", {
  1194. key: 0,
  1195. class: "preview-box",
  1196. style: H(d.value.div)
  1197. }, [
  1198. x("img", {
  1199. src: d.value.url,
  1200. style: H(d.value.img),
  1201. alt: ""
  1202. }, null, 12, gt)
  1203. ], 4)) : (W(), I("span", ft, S(m.$l("拖动裁剪框调整范围")), 1))
  1204. ])
  1205. ]),
  1206. _: 1
  1207. }, 8, ["style"])
  1208. ]),
  1209. _: 1
  1210. })
  1211. ]),
  1212. _: 1
  1213. }, 8, ["value", "title", "width"]);
  1214. };
  1215. }
  1216. }, Ct = /* @__PURE__ */ Q(mt, [["__scopeId", "data-v-4d7c647d"]]);
  1217. export {
  1218. Ct as default
  1219. };