index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // cropper/cropper.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. /**
  8. * @type number
  9. * @description 组件裁剪显示区域的最大比例,如果裁剪的图片过长,则做限制,默认最大宽高比例为 宽640 / 高960 (宽高比例)
  10. * @example 1 如果CROPPER_WIDTH宽度是720px,那么裁剪区域的高度也就是 CROPPER_WIDTH / cropperRatio 为 720px;
  11. */
  12. cropperRatio: {
  13. type: Number,
  14. value: 0.7
  15. },
  16. /**
  17. * @type number
  18. * @description 初始化的裁剪比例
  19. * @example 0 默认初始化的裁剪区域宽高为图片的宽高,且裁剪比例不固定
  20. * @example 0.5 宽高比例固定,且宽和高的比例为 1 : 2 的比例
  21. * @example 2 宽高比例固定,且宽和高的比例为 2 : 1 的比例
  22. */
  23. cutRatio: {
  24. type: Number,
  25. value: 1
  26. },
  27. /**
  28. * @type string
  29. * @description 需要裁剪的图片地址
  30. */
  31. imageSrc: {
  32. type: String,
  33. value: ''
  34. },
  35. /**
  36. * @type number
  37. * @description 裁剪区域的宽度
  38. */
  39. cropperWidth: {
  40. type: Number,
  41. value: 720
  42. },
  43. /**
  44. * @type number
  45. * @description 最小裁剪的范围
  46. */
  47. minCropperW: {
  48. type: Number,
  49. value: 100
  50. }
  51. },
  52. /**
  53. * 组件的初始数据
  54. */
  55. data: {
  56. /**
  57. * @type boolean
  58. * @description 图片在进行网络请求完成之后显示,showImg用于控制图片显示时机
  59. */
  60. showImg: false,
  61. /**
  62. * @
  63. */
  64. // 动态的宽高
  65. cropperW: null,
  66. cropperH: null,
  67. // 图片缩放值
  68. scaleP: 0,
  69. // 裁剪框 宽高
  70. cutL: 0,
  71. cutT: 0,
  72. cutB: 0,
  73. cutR: 0,
  74. qualityWidth: null,
  75. innerAspectRadio: null,
  76. filePath: null
  77. },
  78. /**
  79. * 组件的方法列表
  80. */
  81. methods: {
  82. close() {
  83. wx.hideLoading()
  84. this.triggerEvent('close')
  85. },
  86. /**
  87. * 初始化变量信息
  88. */
  89. initStaticData() {
  90. this.drag = {
  91. CUT_L: null, // 初始化拖拽元素的left值
  92. CUT_T: null, // ...top值
  93. CUT_R: null, // ...right值
  94. CUT_B: null, // ...bottom值
  95. CUT_W: null, // 初始化拖拽元素的宽度
  96. CUT_H: null, // 初始化拖拽元素的高度
  97. IS_TOUCH_CONTENT: false, // 是否是可拖动的状态(拖拽裁剪框)
  98. IS_TOUCH_SIDE: false, // 是否可以拖拽边框
  99. IS_NO_DRAG: false,
  100. // 拖拽区域的时候设置
  101. TOUCH_OFFSET_X: null, // 手按下相对于裁剪框左边的距离
  102. TOUCH_OFFSET_Y: null, // 手按下相对于裁剪框上边的距离
  103. TOUCH_MAX_MOVE_SECTION_X: null, // 移动区域的时候移动的x方向最大区间
  104. TOUCH_MAX_MOVE_SECTION_Y: null, // 移动区域的时候移动的y方向最大区间
  105. MOVE_PAGE_X: null, // 手移动的时候x的位置
  106. MOVE_PAGE_Y: null, // 手移动的时候Y的位置
  107. SPACE_TOP_POSITION: null,
  108. SPACE_LEFT_POSITION: null,
  109. SPACE_RIGHT_POSITION: null,
  110. SPACE_BOTTOM_POSITION: null
  111. }
  112. this.conf = {
  113. // 图片比例
  114. IMG_RATIO: null,
  115. // 图片实际宽高
  116. IMG_REAL_W: null, // 图片实际的宽度
  117. IMG_REAL_H: null, // 图片实际的高度
  118. // 裁剪除黑色区域以内的高度
  119. CROPPER_HEIGHT: null, // 图片背景区域宽度
  120. CROPPER_WIDTH: null, // 图标背景区域高度
  121. // 设置最小裁剪宽度高度
  122. CUT_MIN_W: null, // 最小限制多宽
  123. CUT_MIN_H: null, // 最小限制多高
  124. // 裁剪图片区域的信息
  125. // CROPPER_IMG_W: null, // 也就是 data.cropperW
  126. // CROPPER_IMG_H: null, // 也就是 data.cropperH
  127. // 移动的比例
  128. DRAG_MOVE_RATIO: 750 / wx.getSystemInfoSync().windowWidth, //移动时候的比例,
  129. INIT_DRAG_POSITION: 0, // 初始化屏幕宽度和裁剪区域的宽度之差,用于设置初始化裁剪的宽度
  130. DRAW_IMAGE_W: null, // 设置生成的图片宽度
  131. // 最大可显示得图片宽度,需要设定最大值,否则安卓部分机器会闪退, 控制qualityWidth的最大值
  132. MAX_QW: 2550,
  133. /**
  134. * 最小裁剪宽度 由于设置了裁剪的UI样式,裁剪的宽高必须要有最小宽度,这个宽度是裁剪长或者宽的最短一方的宽度
  135. * 如 400 200
  136. * 那么如果只能设置为100的时候
  137. * 那么最小缩放到200 100的效果,之后只能放大不能缩小
  138. */
  139. MIN_CROPPER_DIS: 100
  140. }
  141. },
  142. /**
  143. * 选择本地图片
  144. * 基于底部中间的按钮的点击事件
  145. */
  146. getImage() {
  147. const _this = this
  148. wx.chooseImage({
  149. success: function (res) {
  150. _this.setData({
  151. isShowImg: false,
  152. filePath: res.tempFilePaths[0],
  153. })
  154. _this.loadImage(_this.data.filePath)
  155. },
  156. })
  157. },
  158. /**
  159. * 初始化加载图片
  160. */
  161. loadImage(src) {
  162. const _this = this
  163. wx.showLoading({
  164. title: '图片加载中...',
  165. })
  166. // console.log(this.properties.imageSrc)
  167. wx.getImageInfo({
  168. src: src ? src : this.properties.imageSrc,
  169. success: function (res) {
  170. /**
  171. * 获取图片真实宽高
  172. * 设置DRAW_IMAGE_W
  173. */
  174. _this.conf.DRAW_IMAGE_W = _this.conf.IMG_REAL_W = res.width
  175. _this.conf.IMG_REAL_H = res.height
  176. _this.conf.IMG_RATIO = Number((_this.conf.IMG_REAL_W / _this.conf.IMG_REAL_H).toFixed(5))
  177. _this.conf.CROPPER_HEIGHT = Math.ceil(_this.properties.cropperWidth / _this.conf.IMG_RATIO)
  178. const scaleP = Number((_this.conf.IMG_REAL_W / _this.properties.cropperWidth).toFixed(5))
  179. const qualityWidth = _this.conf.DRAW_IMAGE_W > _this.conf.MAX_QW ? _this.conf.MAX_QW : _this.conf.DRAW_IMAGE_W
  180. // const MIN_RANG
  181. const p = _this.initPosition()
  182. // 根据图片的宽高显示不同的效果 保证图片可以正常显示 (横屏)
  183. // console.log(_this.conf.IMG_RATIO)
  184. // console.log(_this.conf)
  185. // console.log(_this.drag)
  186. // console.log(_this.data)
  187. // console.log(p)
  188. if (_this.conf.IMG_RATIO >= 1) {
  189. _this.conf.CROPPER_WIDTH = _this.properties.cropperWidth
  190. _this.setData ({
  191. cropperW: _this.properties.cropperWidth,
  192. cropperH: _this.conf.CROPPER_HEIGHT,
  193. // 初始化left right
  194. cutL: p.left,
  195. cutT: p.top,
  196. cutR: p.right,
  197. cutB: p.bottom,
  198. // 图片缩放值
  199. scaleP,
  200. qualityWidth,
  201. innerAspectRadio: _this.conf.IMG_RATIO,
  202. filePath: res.path
  203. })
  204. } else {
  205. // 竖屏初始化
  206. _this.setData ({
  207. cropperW: _this.conf.CROPPER_WIDTH,
  208. cropperH: _this.conf.CROPPER_HEIGHT,
  209. // 初始化left right
  210. cutL: p.left,
  211. cutT: p.top,
  212. cutR: p.right,
  213. cutB: p.bottom,
  214. // 图片缩放值
  215. scaleP,
  216. qualityWidth,
  217. innerAspectRadio: _this.conf.IMG_RATIO,
  218. filePath: res.path
  219. })
  220. }
  221. // 设置裁剪最小限制
  222. _this.setMinCutInfo()
  223. _this.setData({
  224. showImg: true
  225. })
  226. wx.hideLoading()
  227. }
  228. })
  229. },
  230. /**
  231. * 点击完成裁剪图片并返回图片信息
  232. * width 宽度
  233. * height 高度
  234. * url 图片的临时存储地址
  235. */
  236. getImageInfo() {
  237. const _this = this
  238. wx.showLoading({
  239. title: '图片生成中...',
  240. })
  241. this.drag.IS_NO_DRAG = true
  242. // 将图片写入画布
  243. const ctx = wx.createCanvasContext('wxCropperCanvas', _this)
  244. const w = this.data.qualityWidth
  245. const h = Math.ceil(this.data.qualityWidth / this.data.innerAspectRadio)
  246. ctx.drawImage(_this.data.filePath, 0, 0, w, h)
  247. ctx.draw(true, () => {
  248. // 获取画布要裁剪的位置和宽度 均为百分比 * 画布中图片的宽度 保证了在微信小程序中裁剪的图片模糊 位置不对的问题
  249. const canvasW = Math.ceil(((_this.data.cropperW - _this.data.cutL - _this.data.cutR) / _this.data.cropperW) * w)
  250. const canvasH = Math.ceil(((_this.data.cropperH - _this.data.cutT - _this.data.cutB) / _this.data.cropperH) * h)
  251. const canvasL = Math.ceil((_this.data.cutL / _this.data.cropperW) * w)
  252. const canvasT = Math.ceil((_this.data.cutT / _this.data.cropperH) * h)
  253. // console.log(canvasW, canvasH, canvasL, canvasT)
  254. // 生成图片
  255. wx.canvasToTempFilePath({
  256. x: canvasL,
  257. y: canvasT,
  258. width: canvasW,
  259. height: canvasH,
  260. destWidth: canvasW,
  261. destHeight: canvasH,
  262. quality: 0.9,
  263. canvasId: 'wxCropperCanvas',
  264. success: function (res) {
  265. // console.log(res.tempFilePath)
  266. const img = {
  267. path: res.tempFilePath,
  268. width: canvasW,
  269. height: canvasH
  270. }
  271. _this.triggerEvent('close', img)
  272. },
  273. complete: function () {
  274. // 结束之后可拖拽放大缩小
  275. // 关闭loading
  276. wx.hideLoading()
  277. _this.drag.IS_NO_DRAG = false
  278. }
  279. }, _this)
  280. })
  281. },
  282. /**
  283. * 设置最小裁剪宽度高度限制
  284. */
  285. setMinCutInfo() {
  286. this.conf.CUT_MIN_W = this.properties.minCropperW
  287. if (this.properties.cutRatio) {
  288. this.conf.CUT_MIN_H = this.conf.CUT_MIN_W / this.properties.cutRatio
  289. // console.log('this.conf.CUT_MIN_H', this.conf.CUT_MIN_H)
  290. return
  291. }
  292. this.conf.CUT_MIN_H = this.conf.CUT_MIN_W
  293. // console.log('this.conf.CUT_MIN_H', this.conf.CUT_MIN_H)
  294. },
  295. /**
  296. * 初始化裁剪位置
  297. * 需要 cutRatio 来判断
  298. * @return 返回裁剪的left, right, top bottom的值
  299. */
  300. initPosition() {
  301. // 定义返回的对象
  302. const left = 0,
  303. right = 0,
  304. top = 0,
  305. bottom = 0
  306. // cutRatio为0 且为横行 则为不等比裁剪
  307. if (this.properties.cutRatio === 0 && this.conf.IMG_RATIO >= 1) {
  308. return { left, right, top, bottom }
  309. }
  310. // 如果图片宽度大于等于高度(横向)
  311. if (this.conf.IMG_RATIO >= 1) {
  312. // 获取基本宽度
  313. // 图片显示区域比裁剪比例大的时候
  314. if (this.conf.IMG_RATIO >= this.properties.cutRatio) {
  315. // left的值
  316. let leftRight = Math.ceil((this.properties.cropperWidth - (this.conf.CROPPER_HEIGHT * this.properties.cutRatio)) / 2)
  317. return {
  318. left: leftRight,
  319. right: leftRight,
  320. top: 0,
  321. bottom: 0
  322. }
  323. }
  324. // 否则
  325. const bottomTop = Math.ceil((this.conf.CROPPER_HEIGHT - (this.properties.cropperWidth / this.properties.cutRatio)) / 2)
  326. return {
  327. left: 0,
  328. right: 0,
  329. top: bottomTop,
  330. bottom: bottomTop
  331. }
  332. }
  333. // 如果图片宽度小于高度 (竖向)
  334. // const r = _this.properties.cropperRatio > _this.conf.IMG_RATIO ? _this.properties.cropperRatio : _this.conf.IMG_RATIO
  335. if (this.properties.cropperRatio > this.conf.IMG_RATIO) {
  336. this.conf.CROPPER_WIDTH = this.properties.cropperWidth / this.properties.cropperRatio * this.conf.IMG_RATIO
  337. this.conf.CROPPER_HEIGHT = this.properties.cropperWidth / this.properties.cropperRatio
  338. } else {
  339. this.conf.CROPPER_WIDTH = this.properties.cropperWidth
  340. this.conf.CROPPER_HEIGHT = this.properties.cropperWidth / this.conf.IMG_RATIO
  341. }
  342. // 定义四个位置 如果不比例裁剪
  343. if (this.properties.cutRatio === 0) return { left, right, top, bottom }
  344. // 否则
  345. if (this.conf.IMG_RATIO >= this.properties.cutRatio) {
  346. const leftRight = Math.ceil((this.conf.CROPPER_WIDTH - (this.conf.CROPPER_HEIGHT * this.properties.cutRatio)) / 2)
  347. return {
  348. left: leftRight,
  349. right: leftRight,
  350. top: 0,
  351. bottom: 0
  352. }
  353. }
  354. const bottomTop = Math.ceil((this.conf.CROPPER_HEIGHT - (this.conf.CROPPER_WIDTH / this.properties.cutRatio)) / 2)
  355. return {
  356. left: 0,
  357. right: 0,
  358. top: bottomTop,
  359. bottom: bottomTop
  360. }
  361. },
  362. /**
  363. * 裁剪框的拖动事件
  364. */
  365. contentDragStart(e) {
  366. if (this.drag.IS_NO_DRAG) return
  367. this.drag.IS_TOUCH_CONTENT = true
  368. this.drag.TOUCH_OFFSET_X = (e.touches[0].pageX * this.conf.DRAG_MOVE_RATIO - this.data.cutL)
  369. this.drag.TOUCH_OFFSET_Y = (e.touches[0].pageY * this.conf.DRAG_MOVE_RATIO - this.data.cutT)
  370. /**
  371. * 获取可移动的最大值 xy方向
  372. */
  373. const cc = this.cropperCurrentInfo()
  374. this.drag.TOUCH_MAX_MOVE_SECTION_X = cc.x
  375. this.drag.TOUCH_MAX_MOVE_SECTION_Y = cc.y
  376. },
  377. /**
  378. * 获取裁剪区域信息
  379. */
  380. cropperCurrentInfo() {
  381. const x = this.data.cutL + this.data.cutR
  382. const y = this.data.cutT + this.data.cutB
  383. // 获取拖拽元素的宽高
  384. this.drag.CUT_W = this.data.cropperW - x
  385. this.drag.CUT_H = this.data.cropperH - y
  386. // 返回x, y
  387. return {
  388. x,
  389. y
  390. }
  391. },
  392. /**
  393. * 裁剪框拖动
  394. */
  395. contentDragMove(e) {
  396. if (this.drag.IS_NO_DRAG) return
  397. if (!this.drag.IS_TOUCH_CONTENT) return
  398. const MOVE_X = e.touches[0].pageX * this.conf.DRAG_MOVE_RATIO - this.drag.TOUCH_OFFSET_X
  399. const MOVE_Y = e.touches[0].pageY * this.conf.DRAG_MOVE_RATIO - this.drag.TOUCH_OFFSET_Y
  400. const drag_x = Math.min(this.drag.TOUCH_MAX_MOVE_SECTION_X, Math.max(0, MOVE_X))
  401. const drag_y = Math.min(this.drag.TOUCH_MAX_MOVE_SECTION_Y, Math.max(0, MOVE_Y))
  402. this.setData({
  403. cutL: Math.ceil(drag_x),
  404. cutR: Math.ceil(this.data.cropperW - this.drag.CUT_W - drag_x),
  405. cutT: Math.ceil(drag_y),
  406. cutB: Math.ceil((this.data.cropperH - this.drag.CUT_H - drag_y))
  407. })
  408. // 需要初始化
  409. this.drag.TOUCH_OFFSET_X = (e.touches[0].pageX * this.conf.DRAG_MOVE_RATIO - this.data.cutL)
  410. this.drag.TOUCH_OFFSET_Y = (e.touches[0].pageY * this.conf.DRAG_MOVE_RATIO - this.data.cutT)
  411. },
  412. /**
  413. * 裁剪框拖动结束
  414. */
  415. contentTouchEnd() {
  416. this.drag.IS_TOUCH_CONTENT = false
  417. },
  418. /**
  419. * 裁剪框4个方向的拖拽
  420. */
  421. sideDragStart(e) {
  422. if (this.drag.IS_NO_DRAG) return
  423. this.drag.IS_TOUCH_SIDE = true
  424. this.drag.MOVE_PAGE_X = e.touches[0].pageX
  425. this.drag.MOVE_PAGE_Y = e.touches[0].pageY
  426. // 初始化设置
  427. this.conf.CUT_T = this.data.cutT
  428. this.conf.CUT_L = this.data.cutL
  429. this.conf.CUT_R = this.data.cutR
  430. this.conf.CUT_B = this.data.cutB
  431. // 初始化最大移动区域
  432. this.drag.SPACE_TOP_POSITION = this.conf.CROPPER_HEIGHT - this.conf.CUT_B - this.conf.CUT_MIN_H
  433. this.drag.SPACE_BOTTOM_POSITION = this.conf.CROPPER_HEIGHT - this.conf.CUT_T - this.conf.CUT_MIN_H
  434. this.drag.SPACE_RIGHT_POSITION = this.conf.CROPPER_WIDTH - this.conf.CUT_L - this.conf.CUT_MIN_W
  435. this.drag.SPACE_LEFT_POSITION = this.conf.CROPPER_WIDTH - this.conf.CUT_R - this.conf.CUT_MIN_W
  436. },
  437. /**
  438. * 拖拽中
  439. */
  440. sideDragMove(e) {
  441. if (this.drag.IS_NO_DRAG) return
  442. if (!this.drag.IS_TOUCH_SIDE) return
  443. const type = e.target.dataset.drag
  444. if (this.properties.cutRatio === 0) {
  445. this.sideDragMoveDefault(e, type)
  446. } else {
  447. this.sideDragMoveConst(e, type)
  448. }
  449. },
  450. /**
  451. * 拖拽结束
  452. */
  453. sideDragEnd() {
  454. this.drag.IS_TOUCH_SIDE = false
  455. // console.log('sideDragEnd')
  456. },
  457. /**
  458. * 开始拖拽
  459. * 等比例的拖拽方式
  460. */
  461. sideDragMoveConst(e, type) {
  462. const xLength = (e.touches[0].pageX - this.drag.MOVE_PAGE_X) * this.conf.DRAG_MOVE_RATIO
  463. const yLength = (e.touches[0].pageY - this.drag.MOVE_PAGE_Y) * this.conf.DRAG_MOVE_RATIO
  464. switch (type) {
  465. case 'top':
  466. let top = this.conf.CUT_T + yLength
  467. top = Math.ceil(top >= this.drag.SPACE_TOP_POSITION ? this.drag.SPACE_TOP_POSITION : top)
  468. let topL = this.conf.CUT_L + yLength * this.properties.cutRatio
  469. topL = Math.ceil(topL >= this.drag.SPACE_LEFT_POSITION ? this.drag.SPACE_LEFT_POSITION : topL)
  470. if (topL < 0) {
  471. if (this.data.cutT <= 0) return
  472. if (this.data.cutL >= 0) return
  473. this.setData({
  474. cutL: 0
  475. })
  476. return
  477. }
  478. if (top <= 0) {
  479. this.setData({
  480. cutT: 0
  481. })
  482. return
  483. }
  484. this.setData({
  485. cutT: top,
  486. cutL: topL
  487. })
  488. break
  489. case 'left':
  490. let left = this.conf.CUT_L + xLength
  491. left = Math.ceil(left >= this.drag.SPACE_LEFT_POSITION ? this.drag.SPACE_LEFT_POSITION : left)
  492. let leftB = this.conf.CUT_B + xLength / this.properties.cutRatio
  493. leftB = Math.ceil(leftB >= this.drag.SPACE_BOTTOM_POSITION ? this.drag.SPACE_BOTTOM_POSITION : leftB)
  494. // console.log(leftB)
  495. // console.log(left)
  496. if (leftB < 0) {
  497. if (this.data.cutL <= 0) return
  498. if (this.data.cutB >= 0) return
  499. this.setData({
  500. cutB: 0
  501. })
  502. return
  503. }
  504. if (left <= 0) {
  505. this.setData({
  506. cutL: 0
  507. })
  508. return
  509. }
  510. this.setData({
  511. cutL: left,
  512. cutB: leftB
  513. })
  514. break
  515. case 'bottom':
  516. let bottom = this.conf.CUT_B - yLength
  517. bottom = Math.ceil(bottom >= this.drag.SPACE_BOTTOM_POSITION ? this.drag.SPACE_BOTTOM_POSITION : bottom)
  518. let bottomR = this.conf.CUT_R - yLength * this.properties.cutRatio
  519. bottomR = Math.ceil(bottomR >= this.drag.SPACE_RIGHT_POSITION ? this.drag.SPACE_RIGHT_POSITION : bottomR)
  520. if (bottomR < 0) {
  521. if (this.data.cutB <= 0) return
  522. if (this.data.cutR >= 0) return
  523. this.setData({
  524. cutR: 0
  525. })
  526. return
  527. }
  528. if (bottom <= 0) {
  529. this.setData({
  530. cutB: 0
  531. })
  532. return
  533. }
  534. this.setData({
  535. cutR: bottomR,
  536. cutB: bottom
  537. })
  538. break
  539. case 'right':
  540. let right = this.conf.CUT_R - xLength
  541. right = Math.ceil(right >= this.drag.SPACE_RIGHT_POSITION ? this.drag.SPACE_RIGHT_POSITION : right)
  542. let rightT = this.conf.CUT_T - xLength / this.properties.cutRatio
  543. rightT = Math.ceil(rightT >= this.drag.SPACE_TOP_POSITION ? this.drag.SPACE_TOP_POSITION : rightT)
  544. if (rightT < 0) {
  545. if (this.data.cutR <= 0) return
  546. if (this.data.cutT >= 0) return
  547. this.setData({
  548. cutT: 0
  549. })
  550. return
  551. }
  552. if (right <= 0) {
  553. this.setData({
  554. cutR: 0
  555. })
  556. return
  557. }
  558. this.setData({
  559. cutR: right,
  560. cutT: rightT
  561. })
  562. break
  563. }
  564. },
  565. /**
  566. * 非等比例拖拽的操作
  567. */
  568. sideDragMoveDefault(e, type) {
  569. const xLength = (e.touches[0].pageX - this.drag.MOVE_PAGE_X) * this.conf.DRAG_MOVE_RATIO
  570. const yLength = (e.touches[0].pageY - this.drag.MOVE_PAGE_Y) * this.conf.DRAG_MOVE_RATIO
  571. switch (type) {
  572. case 'top':
  573. let top = this.conf.CUT_T + yLength
  574. top = top <= 0 ? 0 : top
  575. top = Math.ceil(top >= this.drag.SPACE_TOP_POSITION ? this.drag.SPACE_TOP_POSITION : top)
  576. this.setData({
  577. cutT: top
  578. })
  579. break
  580. case 'bottom':
  581. let bottom = this.conf.CUT_B - yLength
  582. bottom = bottom <= 0 ? 0 : bottom
  583. bottom = Math.ceil(bottom >= this.drag.SPACE_BOTTOM_POSITION ? this.drag.SPACE_BOTTOM_POSITION : bottom)
  584. this.setData({
  585. cutB: bottom
  586. })
  587. break
  588. case 'right':
  589. let right = this.conf.CUT_R - xLength
  590. right = right <= 0 ? 0 : right
  591. right = Math.ceil(right >= this.drag.SPACE_RIGHT_POSITION ? this.drag.SPACE_RIGHT_POSITION : right)
  592. this.setData({
  593. cutR: right
  594. })
  595. break
  596. case 'left':
  597. let left = this.conf.CUT_L + xLength
  598. left = left <= 0 ? 0 : left
  599. left = Math.ceil(left >= this.drag.SPACE_LEFT_POSITION ? this.drag.SPACE_LEFT_POSITION : left)
  600. this.setData({
  601. cutL: left
  602. })
  603. break
  604. case 'rightBottom':
  605. let rightBottomR = this.conf.CUT_R - xLength
  606. rightBottomR = rightBottomR <= 0 ? 0 : rightBottomR
  607. rightBottomR = Math.ceil(rightBottomR >= this.drag.SPACE_RIGHT_POSITION ? this.drag.SPACE_RIGHT_POSITION : rightBottomR)
  608. let rightBottomB = this.conf.CUT_B - yLength
  609. rightBottomB = rightBottomB <= 0 ? 0 : rightBottomB
  610. rightBottomB = Math.ceil(rightBottomB >= this.drag.SPACE_BOTTOM_POSITION ? this.drag.SPACE_BOTTOM_POSITION : rightBottomB)
  611. this.setData({
  612. cutB: rightBottomB,
  613. cutR: rightBottomR
  614. })
  615. break
  616. default:
  617. break
  618. }
  619. },
  620. },
  621. created: function () {
  622. this.initStaticData()
  623. // console.log(this.drag)
  624. // console.log(this.conf)
  625. // console.log(this.data)
  626. // console.log(this.conf.DRAG_MOVE_RATIO)
  627. },
  628. attached: function () {
  629. // console.log('attached')
  630. this.loadImage()
  631. },
  632. ready: function () {
  633. // console.log('ready')
  634. },
  635. moved: function () {
  636. // console.log('moved')
  637. },
  638. detached: function () {
  639. // console.log('detached')
  640. }
  641. })