image-cropper.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. Component({
  2. properties: {
  3. /**
  4. * 图片路径
  5. */
  6. 'imgSrc': {
  7. type: String
  8. },
  9. /**
  10. * 裁剪框高度
  11. */
  12. 'height': {
  13. type: Number,
  14. value: 200
  15. },
  16. /**
  17. * 裁剪框宽度
  18. */
  19. 'width': {
  20. type: Number,
  21. value: 200
  22. },
  23. /**
  24. * 裁剪框最小尺寸
  25. */
  26. 'min_width': {
  27. type: Number,
  28. value: 100
  29. },
  30. 'min_height': {
  31. type: Number,
  32. value: 100
  33. },
  34. /**
  35. * 裁剪框最大尺寸
  36. */
  37. 'max_width': {
  38. type: Number,
  39. value: 300
  40. },
  41. 'max_height': {
  42. type: Number,
  43. value: 300
  44. },
  45. /**
  46. * 裁剪框禁止拖动
  47. */
  48. 'disable_width': {
  49. type: Boolean,
  50. value: false
  51. },
  52. 'disable_height': {
  53. type: Boolean,
  54. value: false
  55. },
  56. /**
  57. * 锁定裁剪框比例
  58. */
  59. 'disable_ratio': {
  60. type: Boolean,
  61. value: false
  62. },
  63. /**
  64. * 生成的图片尺寸相对剪裁框的比例
  65. */
  66. 'export_scale': {
  67. type: Number,
  68. value: 3
  69. },
  70. /**
  71. * 生成的图片质量0-1
  72. */
  73. 'quality': {
  74. type: Number,
  75. value: 1
  76. },
  77. 'cut_top': {
  78. type: Number,
  79. value: null
  80. },
  81. 'cut_left': {
  82. type: Number,
  83. value: null
  84. },
  85. /**
  86. * canvas上边距(不设置默认不显示)
  87. */
  88. 'canvas_top': {
  89. type: Number,
  90. value: null
  91. },
  92. /**
  93. * canvas左边距(不设置默认不显示)
  94. */
  95. 'canvas_left': {
  96. type: Number,
  97. value: null
  98. },
  99. /**
  100. * 图片宽度
  101. */
  102. 'img_width': {
  103. type: null,
  104. value: null
  105. },
  106. /**
  107. * 图片高度
  108. */
  109. 'img_height': {
  110. type: null,
  111. value: null
  112. },
  113. /**
  114. * 图片缩放比
  115. */
  116. 'scale': {
  117. type: Number,
  118. value: 1
  119. },
  120. /**
  121. * 图片旋转角度
  122. */
  123. 'angle': {
  124. type: Number,
  125. value: 0
  126. },
  127. /**
  128. * 最小缩放比
  129. */
  130. 'min_scale': {
  131. type: Number,
  132. value: 0.5
  133. },
  134. /**
  135. * 最大缩放比
  136. */
  137. 'max_scale': {
  138. type: Number,
  139. value: 2
  140. },
  141. /**
  142. * 是否禁用旋转
  143. */
  144. 'disable_rotate': {
  145. type: Boolean,
  146. value: false
  147. },
  148. /**
  149. * 是否限制移动范围(剪裁框只能在图片内)
  150. */
  151. 'limit_move': {
  152. type: Boolean,
  153. value: false
  154. }
  155. },
  156. data: {
  157. el: 'image-cropper', //暂时无用
  158. info: wx.getSystemInfoSync(),
  159. MOVE_THROTTLE: null, //触摸移动节流settimeout
  160. MOVE_THROTTLE_FLAG: true, //节流标识
  161. INIT_IMGWIDTH: 0, //图片设置尺寸,此值不变(记录最初设定的尺寸)
  162. INIT_IMGHEIGHT: 0, //图片设置尺寸,此值不变(记录最初设定的尺寸)
  163. TIME_BG: null, //背景变暗延时函数
  164. TIME_CUT_CENTER: null,
  165. _touch_img_relative: [{
  166. x: 0,
  167. y: 0
  168. }], //鼠标和图片中心的相对位置
  169. _flag_cut_touch: false, //是否是拖动裁剪框
  170. _hypotenuse_length: 0, //双指触摸时斜边长度
  171. _flag_img_endtouch: false, //是否结束触摸
  172. _flag_bright: true, //背景是否亮
  173. _canvas_overflow: true, //canvas缩略图是否在屏幕外面
  174. _canvas_width: 200,
  175. _canvas_height: 200,
  176. origin_x: 0.5, //图片旋转中心
  177. origin_y: 0.5, //图片旋转中心
  178. _cut_animation: false, //是否开启图片和裁剪框过渡
  179. _img_top: wx.getSystemInfoSync().windowHeight / 2, //图片上边距
  180. _img_left: wx.getSystemInfoSync().windowWidth / 2, //图片左边距
  181. watch: {
  182. //监听截取框宽高变化
  183. width(value, that) {
  184. if (value < that.data.min_width) {
  185. that.setData({
  186. width: that.data.min_width
  187. });
  188. }
  189. that._computeCutSize();
  190. },
  191. height(value, that) {
  192. if (value < that.data.min_height) {
  193. that.setData({
  194. height: that.data.min_height
  195. });
  196. }
  197. that._computeCutSize();
  198. },
  199. angle(value, that) {
  200. //停止居中裁剪框,继续修改图片位置
  201. that._moveStop();
  202. if (that.data.limit_move) {
  203. if (that.data.angle % 90) {
  204. that.setData({
  205. angle: Math.round(that.data.angle / 90) * 90
  206. });
  207. return;
  208. }
  209. }
  210. },
  211. _cut_animation(value, that) {
  212. //开启过渡300毫秒之后自动关闭
  213. clearTimeout(that.data._cut_animation_time);
  214. if (value) {
  215. that.data._cut_animation_time = setTimeout(() => {
  216. that.setData({
  217. _cut_animation: false
  218. });
  219. }, 300)
  220. }
  221. },
  222. limit_move(value, that) {
  223. if (value) {
  224. if (that.data.angle % 90) {
  225. that.setData({
  226. angle: Math.round(that.data.angle / 90) * 90
  227. });
  228. }
  229. that._imgMarginDetectionScale();
  230. !that.data._canvas_overflow && that._draw();
  231. }
  232. },
  233. canvas_top(value, that) {
  234. that._canvasDetectionPosition();
  235. },
  236. canvas_left(value, that) {
  237. that._canvasDetectionPosition();
  238. },
  239. imgSrc(value, that) {
  240. that.pushImg();
  241. },
  242. cut_top(value, that) {
  243. that._cutDetectionPosition();
  244. if (that.data.limit_move) {
  245. !that.data._canvas_overflow && that._draw();
  246. }
  247. },
  248. cut_left(value, that) {
  249. that._cutDetectionPosition();
  250. if (that.data.limit_move) {
  251. !that.data._canvas_overflow && that._draw();
  252. }
  253. }
  254. }
  255. },
  256. attached() {
  257. this.data.info = wx.getSystemInfoSync();
  258. //启用数据监听
  259. this._watcher();
  260. this.data.INIT_IMGWIDTH = this.data.img_width;
  261. this.data.INIT_IMGHEIGHT = this.data.img_height;
  262. this.setData({
  263. _canvas_height: this.data.height,
  264. _canvas_width: this.data.width,
  265. });
  266. this._initCanvas();
  267. this.data.imgSrc && (this.data.imgSrc = this.data.imgSrc);
  268. //根据开发者设置的图片目标尺寸计算实际尺寸
  269. this._initImageSize();
  270. //设置裁剪框大小>设置图片尺寸>绘制canvas
  271. this._computeCutSize();
  272. //检查裁剪框是否在范围内
  273. this._cutDetectionPosition();
  274. //检查canvas是否在范围内
  275. this._canvasDetectionPosition();
  276. //初始化完成
  277. this.triggerEvent('load', {
  278. cropper: this
  279. });
  280. },
  281. methods: {
  282. /**
  283. * 上传图片
  284. */
  285. upload() {
  286. let that = this;
  287. wx.chooseImage({
  288. count: 1,
  289. sizeType: ['original', 'compressed'],
  290. sourceType: ['album', 'camera'],
  291. success(res) {
  292. // console.log(res);
  293. const tempFilePaths = res.tempFilePaths[0];
  294. that.pushImg(tempFilePaths);
  295. wx.showLoading({
  296. title: '加载中...'
  297. })
  298. }
  299. })
  300. },
  301. /**
  302. * 返回图片信息
  303. */
  304. getImg(getCallback) {
  305. this._draw(() => {
  306. wx.canvasToTempFilePath({
  307. width: this.data.width * this.data.export_scale,
  308. height: Math.round(this.data.height * this.data.export_scale),
  309. destWidth: this.data.width * this.data.export_scale,
  310. destHeight: Math.round(this.data.height) * this.data.export_scale,
  311. fileType: 'png',
  312. quality: this.data.quality,
  313. canvasId: this.data.el,
  314. success: (res) => {
  315. getCallback({
  316. url: res.tempFilePath,
  317. width: this.data.width * this.data.export_scale,
  318. height: this.data.height * this.data.export_scale
  319. });
  320. }
  321. }, this)
  322. });
  323. },
  324. /**
  325. * 设置图片动画
  326. * {
  327. * x:10,//图片在原有基础上向下移动10px
  328. * y:10,//图片在原有基础上向右移动10px
  329. * angle:10,//图片在原有基础上旋转10deg
  330. * scale:0.5,//图片在原有基础上增加0.5倍
  331. * }
  332. */
  333. setTransform(transform) {
  334. if (!transform) return;
  335. if (!this.data.disable_rotate) {
  336. this.setData({
  337. angle: transform.angle ? this.data.angle + transform.angle : this.data.angle
  338. });
  339. }
  340. var scale = this.data.scale;
  341. if (transform.scale) {
  342. scale = this.data.scale + transform.scale;
  343. scale = scale <= this.data.min_scale ? this.data.min_scale : scale;
  344. scale = scale >= this.data.max_scale ? this.data.max_scale : scale;
  345. }
  346. this.data.scale = scale;
  347. let cutX = this.data.cut_left;
  348. let cutY = this.data.cut_top;
  349. if (transform.cutX) {
  350. this.setData({
  351. cut_left: cutX + transform.cutX
  352. });
  353. this.data.watch.cut_left(null, this);
  354. }
  355. if (transform.cutY) {
  356. this.setData({
  357. cut_top: cutY + transform.cutY
  358. });
  359. this.data.watch.cut_top(null, this);
  360. }
  361. this.data._img_top = transform.y ? this.data._img_top + transform.y : this.data._img_top;
  362. this.data._img_left = transform.x ? this.data._img_left + transform.x : this.data._img_left;
  363. //图像边缘检测,防止截取到空白
  364. this._imgMarginDetectionScale();
  365. //停止居中裁剪框,继续修改图片位置
  366. this._moveDuring();
  367. this.setData({
  368. scale: this.data.scale,
  369. _img_top: this.data._img_top,
  370. _img_left: this.data._img_left
  371. });
  372. !this.data._canvas_overflow && this._draw();
  373. //可以居中裁剪框了
  374. this._moveStop(); //结束操作
  375. },
  376. /**
  377. * 设置剪裁框位置
  378. */
  379. setCutXY(x, y) {
  380. this.setData({
  381. cut_top: y,
  382. cut_left: x
  383. });
  384. },
  385. /**
  386. * 设置剪裁框尺寸
  387. */
  388. setCutSize(w, h) {
  389. this.setData({
  390. width: w,
  391. height: h
  392. });
  393. this._computeCutSize();
  394. },
  395. /**
  396. * 设置剪裁框和图片居中
  397. */
  398. setCutCenter() {
  399. let cut_top = (this.data.info.windowHeight - this.data.height) * 0.5;
  400. let cut_left = (this.data.info.windowWidth - this.data.width) * 0.5;
  401. //顺序不能变
  402. this.setData({
  403. _img_top: this.data._img_top - this.data.cut_top + cut_top,
  404. cut_top: cut_top, //截取的框上边距
  405. _img_left: this.data._img_left - this.data.cut_left + cut_left,
  406. cut_left: cut_left, //截取的框左边距
  407. });
  408. },
  409. _setCutCenter() {
  410. let cut_top = (this.data.info.windowHeight - this.data.height) * 0.5;
  411. let cut_left = (this.data.info.windowWidth - this.data.width) * 0.5;
  412. this.setData({
  413. cut_top: cut_top, //截取的框上边距
  414. cut_left: cut_left, //截取的框左边距
  415. });
  416. },
  417. /**
  418. * 设置剪裁框宽度-即将废弃
  419. */
  420. setWidth(width) {
  421. this.setData({
  422. width: width
  423. });
  424. this._computeCutSize();
  425. },
  426. /**
  427. * 设置剪裁框高度-即将废弃
  428. */
  429. setHeight(height) {
  430. this.setData({
  431. height: height
  432. });
  433. this._computeCutSize();
  434. },
  435. /**
  436. * 是否锁定旋转
  437. */
  438. setDisableRotate(value) {
  439. this.data.disable_rotate = value;
  440. },
  441. /**
  442. * 是否限制移动
  443. */
  444. setLimitMove(value) {
  445. this.setData({
  446. _cut_animation: true,
  447. limit_move: !!value
  448. });
  449. },
  450. /**
  451. * 初始化图片,包括位置、大小、旋转角度
  452. */
  453. imgReset() {
  454. this.setData({
  455. scale: 1,
  456. angle: 0,
  457. _img_top: wx.getSystemInfoSync().windowHeight / 2,
  458. _img_left: wx.getSystemInfoSync().windowWidth / 2,
  459. })
  460. },
  461. /**
  462. * 加载(更换)图片
  463. */
  464. pushImg(src) {
  465. if (src) {
  466. this.setData({
  467. imgSrc: src
  468. });
  469. //发现是手动赋值直接返回,交给watch处理
  470. return;
  471. }
  472. // getImageInfo接口传入 src: '' 会导致内存泄漏
  473. if (!this.data.imgSrc) return;
  474. wx.getImageInfo({
  475. src: this.data.imgSrc,
  476. success: (res) => {
  477. this.data.imageObject = res;
  478. //图片非本地路径需要换成本地路径
  479. if (this.data.imgSrc.search(/tmp/) == -1) {
  480. this.setData({
  481. imgSrc: res.path
  482. });
  483. }
  484. //计算最后图片尺寸
  485. this._imgComputeSize();
  486. if (this.data.limit_move) {
  487. //限制移动,不留空白处理
  488. this._imgMarginDetectionScale();
  489. }
  490. this._draw();
  491. },
  492. fail: (err) => {
  493. this.setData({
  494. imgSrc: ''
  495. });
  496. }
  497. });
  498. },
  499. imageLoad(e) {
  500. setTimeout(() => {
  501. this.triggerEvent('imageload', this.data.imageObject);
  502. }, 1000)
  503. },
  504. /**
  505. * 设置图片放大缩小
  506. */
  507. setScale(scale) {
  508. if (!scale) return;
  509. this.setData({
  510. scale: scale
  511. });
  512. !this.data._canvas_overflow && this._draw();
  513. },
  514. /**
  515. * 设置图片旋转角度
  516. */
  517. setAngle(angle) {
  518. if (!angle) return;
  519. this.setData({
  520. _cut_animation: true,
  521. angle: angle
  522. });
  523. this._imgMarginDetectionScale();
  524. !this.data._canvas_overflow && this._draw();
  525. },
  526. _initCanvas() {
  527. //初始化canvas
  528. if (!this.data.ctx) {
  529. this.data.ctx = wx.createCanvasContext("image-cropper", this);
  530. }
  531. },
  532. /**
  533. * 根据开发者设置的图片目标尺寸计算实际尺寸
  534. */
  535. _initImageSize() {
  536. //处理宽高特殊单位 %>px
  537. if (this.data.INIT_IMGWIDTH && typeof this.data.INIT_IMGWIDTH == "string" && this.data.INIT_IMGWIDTH.indexOf("%") != -1) {
  538. let width = this.data.INIT_IMGWIDTH.replace("%", "");
  539. this.data.INIT_IMGWIDTH = this.data.img_width = this.data.info.windowWidth / 100 * width;
  540. }
  541. if (this.data.INIT_IMGHEIGHT && typeof this.data.INIT_IMGHEIGHT == "string" && this.data.INIT_IMGHEIGHT.indexOf("%") != -1) {
  542. let height = this.data.img_height.replace("%", "");
  543. this.data.INIT_IMGHEIGHT = this.data.img_height = this.data.info.windowHeight / 100 * height;
  544. }
  545. },
  546. /**
  547. * 检测剪裁框位置是否在允许的范围内(屏幕内)
  548. */
  549. _cutDetectionPosition() {
  550. let _cutDetectionPositionTop = () => {
  551. //检测上边距是否在范围内
  552. if (this.data.cut_top < 0) {
  553. this.setData({
  554. cut_top: 0
  555. });
  556. }
  557. if (this.data.cut_top > this.data.info.windowHeight - this.data.height) {
  558. this.setData({
  559. cut_top: this.data.info.windowHeight - this.data.height
  560. });
  561. }
  562. },
  563. _cutDetectionPositionLeft = () => {
  564. //检测左边距是否在范围内
  565. if (this.data.cut_left < 0) {
  566. this.setData({
  567. cut_left: 0
  568. });
  569. }
  570. if (this.data.cut_left > this.data.info.windowWidth - this.data.width) {
  571. this.setData({
  572. cut_left: this.data.info.windowWidth - this.data.width
  573. });
  574. }
  575. };
  576. //裁剪框坐标处理(如果只写一个参数则另一个默认为0,都不写默认居中)
  577. if (this.data.cut_top == null && this.data.cut_left == null) {
  578. this._setCutCenter();
  579. } else if (this.data.cut_top != null && this.data.cut_left != null) {
  580. _cutDetectionPositionTop();
  581. _cutDetectionPositionLeft();
  582. } else if (this.data.cut_top != null && this.data.cut_left == null) {
  583. _cutDetectionPositionTop();
  584. this.setData({
  585. cut_left: (this.data.info.windowWidth - this.data.width) / 2
  586. });
  587. } else if (this.data.cut_top == null && this.data.cut_left != null) {
  588. _cutDetectionPositionLeft();
  589. this.setData({
  590. cut_top: (this.data.info.windowHeight - this.data.height) / 2
  591. });
  592. }
  593. },
  594. /**
  595. * 检测canvas位置是否在允许的范围内(屏幕内)如果在屏幕外则不开启实时渲染
  596. * 如果只写一个参数则另一个默认为0,都不写默认超出屏幕外
  597. */
  598. _canvasDetectionPosition() {
  599. if (this.data.canvas_top == null && this.data.canvas_left == null) {
  600. this.data._canvas_overflow = false;
  601. this.setData({
  602. canvas_top: -5000,
  603. canvas_left: -5000
  604. });
  605. } else if (this.data.canvas_top != null && this.data.canvas_left != null) {
  606. if (this.data.canvas_top < -this.data.height || this.data.canvas_top > this.data.info.windowHeight) {
  607. this.data._canvas_overflow = true;
  608. } else {
  609. this.data._canvas_overflow = false;
  610. }
  611. } else if (this.data.canvas_top != null && this.data.canvas_left == null) {
  612. this.setData({
  613. canvas_left: 0
  614. });
  615. } else if (this.data.canvas_top == null && this.data.canvas_left != null) {
  616. this.setData({
  617. canvas_top: 0
  618. });
  619. if (this.data.canvas_left < -this.data.width || this.data.canvas_left > this.data.info.windowWidth) {
  620. this.data._canvas_overflow = true;
  621. } else {
  622. this.data._canvas_overflow = false;
  623. }
  624. }
  625. },
  626. /**
  627. * 图片边缘检测-位置
  628. */
  629. _imgMarginDetectionPosition(scale) {
  630. if (!this.data.limit_move) return;
  631. let left = this.data._img_left;
  632. let top = this.data._img_top;
  633. var scale = scale || this.data.scale;
  634. let img_width = this.data.img_width;
  635. let img_height = this.data.img_height;
  636. if (this.data.angle / 90 % 2) {
  637. img_width = this.data.img_height;
  638. img_height = this.data.img_width;
  639. }
  640. left = this.data.cut_left + img_width * scale / 2 >= left ? left : this.data.cut_left + img_width * scale / 2;
  641. left = this.data.cut_left + this.data.width - img_width * scale / 2 <= left ? left : this.data.cut_left + this.data.width - img_width * scale / 2;
  642. top = this.data.cut_top + img_height * scale / 2 >= top ? top : this.data.cut_top + img_height * scale / 2;
  643. top = this.data.cut_top + this.data.height - img_height * scale / 2 <= top ? top : this.data.cut_top + this.data.height - img_height * scale / 2;
  644. this.setData({
  645. _img_left: left,
  646. _img_top: top,
  647. scale: scale
  648. })
  649. },
  650. /**
  651. * 图片边缘检测-缩放
  652. */
  653. _imgMarginDetectionScale() {
  654. if (!this.data.limit_move) return;
  655. let scale = this.data.scale;
  656. let img_width = this.data.img_width;
  657. let img_height = this.data.img_height;
  658. if (this.data.angle / 90 % 2) {
  659. img_width = this.data.img_height;
  660. img_height = this.data.img_width;
  661. }
  662. if (img_width * scale < this.data.width) {
  663. scale = this.data.width / img_width;
  664. }
  665. if (img_height * scale < this.data.height) {
  666. scale = Math.max(scale, this.data.height / img_height);
  667. }
  668. this._imgMarginDetectionPosition(scale);
  669. },
  670. _setData(obj) {
  671. let data = {};
  672. for (var key in obj) {
  673. if (this.data[key] != obj[key]) {
  674. data[key] = obj[key];
  675. }
  676. }
  677. this.setData(data);
  678. return data;
  679. },
  680. /**
  681. * 计算图片尺寸
  682. */
  683. _imgComputeSize() {
  684. let img_width = this.data.img_width,
  685. img_height = this.data.img_height;
  686. if (!this.data.INIT_IMGHEIGHT && !this.data.INIT_IMGWIDTH) {
  687. //默认按图片最小边 = 对应裁剪框尺寸
  688. img_width = this.data.imageObject.width;
  689. img_height = this.data.imageObject.height;
  690. if (img_width / img_height > this.data.width / this.data.height) {
  691. img_height = this.data.height;
  692. img_width = this.data.imageObject.width / this.data.imageObject.height * img_height;
  693. } else {
  694. img_width = this.data.width;
  695. img_height = this.data.imageObject.height / this.data.imageObject.width * img_width;
  696. }
  697. } else if (this.data.INIT_IMGHEIGHT && !this.data.INIT_IMGWIDTH) {
  698. img_width = this.data.imageObject.width / this.data.imageObject.height * this.data.INIT_IMGHEIGHT;
  699. } else if (!this.data.INIT_IMGHEIGHT && this.data.INIT_IMGWIDTH) {
  700. img_height = this.data.imageObject.height / this.data.imageObject.width * this.data.INIT_IMGWIDTH;
  701. }
  702. this.setData({
  703. img_width: img_width,
  704. img_height: img_height
  705. });
  706. },
  707. //改变截取框大小
  708. _computeCutSize() {
  709. if (this.data.width > this.data.info.windowWidth) {
  710. this.setData({
  711. width: this.data.info.windowWidth,
  712. });
  713. } else if (this.data.width + this.data.cut_left > this.data.info.windowWidth) {
  714. this.setData({
  715. cut_left: this.data.info.windowWidth - this.data.cut_left,
  716. });
  717. };
  718. if (this.data.height > this.data.info.windowHeight) {
  719. this.setData({
  720. height: this.data.info.windowHeight,
  721. });
  722. } else if (this.data.height + this.data.cut_top > this.data.info.windowHeight) {
  723. this.setData({
  724. cut_top: this.data.info.windowHeight - this.data.cut_top,
  725. });
  726. }!this.data._canvas_overflow && this._draw();
  727. },
  728. //开始触摸
  729. _start(event) {
  730. this.data._flag_img_endtouch = false;
  731. if (event.touches.length == 1) {
  732. //单指拖动
  733. this.data._touch_img_relative[0] = {
  734. x: (event.touches[0].clientX - this.data._img_left),
  735. y: (event.touches[0].clientY - this.data._img_top)
  736. }
  737. } else {
  738. //双指放大
  739. let width = Math.abs(event.touches[0].clientX - event.touches[1].clientX);
  740. let height = Math.abs(event.touches[0].clientY - event.touches[1].clientY);
  741. this.data._touch_img_relative = [{
  742. x: (event.touches[0].clientX - this.data._img_left),
  743. y: (event.touches[0].clientY - this.data._img_top)
  744. }, {
  745. x: (event.touches[1].clientX - this.data._img_left),
  746. y: (event.touches[1].clientY - this.data._img_top)
  747. }];
  748. this.data._hypotenuse_length = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  749. }!this.data._canvas_overflow && this._draw();
  750. },
  751. _move_throttle() {
  752. //安卓需要节流
  753. if (this.data.info.platform == 'android') {
  754. clearTimeout(this.data.MOVE_THROTTLE);
  755. this.data.MOVE_THROTTLE = setTimeout(() => {
  756. this.data.MOVE_THROTTLE_FLAG = true;
  757. }, 1000 / 40)
  758. return this.data.MOVE_THROTTLE_FLAG;
  759. } else {
  760. this.data.MOVE_THROTTLE_FLAG = true;
  761. }
  762. },
  763. _move(event) {
  764. if (this.data._flag_img_endtouch || !this.data.MOVE_THROTTLE_FLAG) return;
  765. this.data.MOVE_THROTTLE_FLAG = false;
  766. this._move_throttle();
  767. this._moveDuring();
  768. if (event.touches.length == 1) {
  769. //单指拖动
  770. let left = (event.touches[0].clientX - this.data._touch_img_relative[0].x),
  771. top = (event.touches[0].clientY - this.data._touch_img_relative[0].y);
  772. //图像边缘检测,防止截取到空白
  773. this.data._img_left = left;
  774. this.data._img_top = top;
  775. this._imgMarginDetectionPosition();
  776. this.setData({
  777. _img_left: this.data._img_left,
  778. _img_top: this.data._img_top
  779. });
  780. } else {
  781. //双指放大
  782. let width = (Math.abs(event.touches[0].clientX - event.touches[1].clientX)),
  783. height = (Math.abs(event.touches[0].clientY - event.touches[1].clientY)),
  784. hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)),
  785. scale = this.data.scale * (hypotenuse / this.data._hypotenuse_length),
  786. current_deg = 0;
  787. scale = scale <= this.data.min_scale ? this.data.min_scale : scale;
  788. scale = scale >= this.data.max_scale ? this.data.max_scale : scale;
  789. //图像边缘检测,防止截取到空白
  790. this.data.scale = scale;
  791. this._imgMarginDetectionScale();
  792. //双指旋转(如果没禁用旋转)
  793. let _touch_img_relative = [{
  794. x: (event.touches[0].clientX - this.data._img_left),
  795. y: (event.touches[0].clientY - this.data._img_top)
  796. }, {
  797. x: (event.touches[1].clientX - this.data._img_left),
  798. y: (event.touches[1].clientY - this.data._img_top)
  799. }];
  800. if (!this.data.disable_rotate) {
  801. let first_atan = 180 / Math.PI * Math.atan2(_touch_img_relative[0].y, _touch_img_relative[0].x);
  802. let first_atan_old = 180 / Math.PI * Math.atan2(this.data._touch_img_relative[0].y, this.data._touch_img_relative[0].x);
  803. let second_atan = 180 / Math.PI * Math.atan2(_touch_img_relative[1].y, _touch_img_relative[1].x);
  804. let second_atan_old = 180 / Math.PI * Math.atan2(this.data._touch_img_relative[1].y, this.data._touch_img_relative[1].x);
  805. //当前旋转的角度
  806. let first_deg = first_atan - first_atan_old,
  807. second_deg = second_atan - second_atan_old;
  808. if (first_deg != 0) {
  809. current_deg = first_deg;
  810. } else if (second_deg != 0) {
  811. current_deg = second_deg;
  812. }
  813. }
  814. this.data._touch_img_relative = _touch_img_relative;
  815. this.data._hypotenuse_length = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  816. //更新视图
  817. this.setData({
  818. angle: this.data.angle + current_deg,
  819. scale: this.data.scale
  820. });
  821. }!this.data._canvas_overflow && this._draw();
  822. },
  823. //结束操作
  824. _end(event) {
  825. this.data._flag_img_endtouch = true;
  826. this._moveStop();
  827. },
  828. //点击中间剪裁框处理
  829. _click(event) {
  830. if (!this.data.imgSrc) {
  831. //调起上传
  832. this.upload();
  833. return;
  834. }
  835. this._draw(() => {
  836. let x = event.detail ? event.detail.x : event.touches[0].clientX;
  837. let y = event.detail ? event.detail.y : event.touches[0].clientY;
  838. if ((x >= this.data.cut_left && x <= (this.data.cut_left + this.data.width)) && (y >= this.data.cut_top && y <= (this.data.cut_top + this.data.height))) {
  839. //生成图片并回调
  840. wx.canvasToTempFilePath({
  841. width: this.data.width * this.data.export_scale,
  842. height: Math.round(this.data.height * this.data.export_scale),
  843. destWidth: this.data.width * this.data.export_scale,
  844. destHeight: Math.round(this.data.height) * this.data.export_scale,
  845. fileType: 'png',
  846. quality: this.data.quality,
  847. canvasId: this.data.el,
  848. success: (res) => {
  849. this.triggerEvent('tapcut', {
  850. url: res.tempFilePath,
  851. width: this.data.width * this.data.export_scale,
  852. height: this.data.height * this.data.export_scale
  853. });
  854. }
  855. }, this)
  856. }
  857. });
  858. },
  859. //渲染
  860. _draw(callback) {
  861. if (!this.data.imgSrc) return;
  862. let draw = () => {
  863. //图片实际大小
  864. let img_width = this.data.img_width * this.data.scale * this.data.export_scale;
  865. let img_height = this.data.img_height * this.data.scale * this.data.export_scale;
  866. //canvas和图片的相对距离
  867. var xpos = this.data._img_left - this.data.cut_left;
  868. var ypos = this.data._img_top - this.data.cut_top;
  869. //旋转画布
  870. this.data.ctx.translate(xpos * this.data.export_scale, ypos * this.data.export_scale);
  871. this.data.ctx.rotate(this.data.angle * Math.PI / 180);
  872. this.data.ctx.drawImage(this.data.imgSrc, -img_width / 2, -img_height / 2, img_width, img_height);
  873. this.data.ctx.draw(false, () => {
  874. callback && callback();
  875. });
  876. }
  877. if (this.data.ctx.width != this.data.width || this.data.ctx.height != this.data.height) {
  878. //优化拖动裁剪框,所以必须把宽高设置放在离用户触发渲染最近的地方
  879. this.setData({
  880. _canvas_height: this.data.height,
  881. _canvas_width: this.data.width,
  882. }, () => {
  883. //延迟40毫秒防止点击过快出现拉伸或裁剪过多
  884. setTimeout(() => {
  885. draw();
  886. }, 40);
  887. });
  888. } else {
  889. draw();
  890. }
  891. },
  892. //裁剪框处理
  893. _cutTouchMove(e) {
  894. if (this.data._flag_cut_touch && this.data.MOVE_THROTTLE_FLAG) {
  895. if (this.data.disable_ratio && (this.data.disable_width || this.data.disable_height)) return;
  896. //节流
  897. this.data.MOVE_THROTTLE_FLAG = false;
  898. this._move_throttle();
  899. let width = this.data.width,
  900. height = this.data.height,
  901. cut_top = this.data.cut_top,
  902. cut_left = this.data.cut_left,
  903. size_correct = () => {
  904. width = width <= this.data.max_width ? width >= this.data.min_width ? width : this.data.min_width : this.data.max_width;
  905. height = height <= this.data.max_height ? height >= this.data.min_height ? height : this.data.min_height : this.data.max_height;
  906. },
  907. size_inspect = () => {
  908. if ((width > this.data.max_width || width < this.data.min_width || height > this.data.max_height || height < this.data.min_height) && this.data.disable_ratio) {
  909. size_correct();
  910. return false;
  911. } else {
  912. size_correct();
  913. return true;
  914. }
  915. };
  916. height = this.data.CUT_START.height + ((this.data.CUT_START.corner > 1 && this.data.CUT_START.corner < 4 ? 1 : -1) * (this.data.CUT_START.y - e.touches[0].clientY));
  917. switch (this.data.CUT_START.corner) {
  918. case 1:
  919. width = this.data.CUT_START.width + this.data.CUT_START.x - e.touches[0].clientX;
  920. if (this.data.disable_ratio) {
  921. height = width / (this.data.width / this.data.height)
  922. }
  923. if (!size_inspect()) return;
  924. cut_left = this.data.CUT_START.cut_left - (width - this.data.CUT_START.width);
  925. break
  926. case 2:
  927. width = this.data.CUT_START.width + this.data.CUT_START.x - e.touches[0].clientX;
  928. if (this.data.disable_ratio) {
  929. height = width / (this.data.width / this.data.height)
  930. }
  931. if (!size_inspect()) return;
  932. cut_top = this.data.CUT_START.cut_top - (height - this.data.CUT_START.height)
  933. cut_left = this.data.CUT_START.cut_left - (width - this.data.CUT_START.width)
  934. break
  935. case 3:
  936. width = this.data.CUT_START.width - this.data.CUT_START.x + e.touches[0].clientX;
  937. if (this.data.disable_ratio) {
  938. height = width / (this.data.width / this.data.height)
  939. }
  940. if (!size_inspect()) return;
  941. cut_top = this.data.CUT_START.cut_top - (height - this.data.CUT_START.height);
  942. break
  943. case 4:
  944. width = this.data.CUT_START.width - this.data.CUT_START.x + e.touches[0].clientX;
  945. if (this.data.disable_ratio) {
  946. height = width / (this.data.width / this.data.height)
  947. }
  948. if (!size_inspect()) return;
  949. break
  950. }
  951. if (!this.data.disable_width && !this.data.disable_height) {
  952. this.setData({
  953. width: width,
  954. cut_left: cut_left,
  955. height: height,
  956. cut_top: cut_top,
  957. })
  958. } else if (!this.data.disable_width) {
  959. this.setData({
  960. width: width,
  961. cut_left: cut_left
  962. })
  963. } else if (!this.data.disable_height) {
  964. this.setData({
  965. height: height,
  966. cut_top: cut_top
  967. })
  968. }
  969. this._imgMarginDetectionScale();
  970. }
  971. },
  972. _cutTouchStart(e) {
  973. let currentX = e.touches[0].clientX;
  974. let currentY = e.touches[0].clientY;
  975. let cutbox_top4 = this.data.cut_top + this.data.height - 30;
  976. let cutbox_bottom4 = this.data.cut_top + this.data.height + 20;
  977. let cutbox_left4 = this.data.cut_left + this.data.width - 30;
  978. let cutbox_right4 = this.data.cut_left + this.data.width + 30;
  979. let cutbox_top3 = this.data.cut_top - 30;
  980. let cutbox_bottom3 = this.data.cut_top + 30;
  981. let cutbox_left3 = this.data.cut_left + this.data.width - 30;
  982. let cutbox_right3 = this.data.cut_left + this.data.width + 30;
  983. let cutbox_top2 = this.data.cut_top - 30;
  984. let cutbox_bottom2 = this.data.cut_top + 30;
  985. let cutbox_left2 = this.data.cut_left - 30;
  986. let cutbox_right2 = this.data.cut_left + 30;
  987. let cutbox_top1 = this.data.cut_top + this.data.height - 30;
  988. let cutbox_bottom1 = this.data.cut_top + this.data.height + 30;
  989. let cutbox_left1 = this.data.cut_left - 30;
  990. let cutbox_right1 = this.data.cut_left + 30;
  991. if (currentX > cutbox_left4 && currentX < cutbox_right4 && currentY > cutbox_top4 && currentY < cutbox_bottom4) {
  992. this._moveDuring();
  993. this.data._flag_cut_touch = true;
  994. this.data._flag_img_endtouch = true;
  995. this.data.CUT_START = {
  996. width: this.data.width,
  997. height: this.data.height,
  998. x: currentX,
  999. y: currentY,
  1000. corner: 4
  1001. }
  1002. } else if (currentX > cutbox_left3 && currentX < cutbox_right3 && currentY > cutbox_top3 && currentY < cutbox_bottom3) {
  1003. this._moveDuring();
  1004. this.data._flag_cut_touch = true;
  1005. this.data._flag_img_endtouch = true;
  1006. this.data.CUT_START = {
  1007. width: this.data.width,
  1008. height: this.data.height,
  1009. x: currentX,
  1010. y: currentY,
  1011. cut_top: this.data.cut_top,
  1012. cut_left: this.data.cut_left,
  1013. corner: 3
  1014. }
  1015. } else if (currentX > cutbox_left2 && currentX < cutbox_right2 && currentY > cutbox_top2 && currentY < cutbox_bottom2) {
  1016. this._moveDuring();
  1017. this.data._flag_cut_touch = true;
  1018. this.data._flag_img_endtouch = true;
  1019. this.data.CUT_START = {
  1020. width: this.data.width,
  1021. height: this.data.height,
  1022. cut_top: this.data.cut_top,
  1023. cut_left: this.data.cut_left,
  1024. x: currentX,
  1025. y: currentY,
  1026. corner: 2
  1027. }
  1028. } else if (currentX > cutbox_left1 && currentX < cutbox_right1 && currentY > cutbox_top1 && currentY < cutbox_bottom1) {
  1029. this._moveDuring();
  1030. this.data._flag_cut_touch = true;
  1031. this.data._flag_img_endtouch = true;
  1032. this.data.CUT_START = {
  1033. width: this.data.width,
  1034. height: this.data.height,
  1035. cut_top: this.data.cut_top,
  1036. cut_left: this.data.cut_left,
  1037. x: currentX,
  1038. y: currentY,
  1039. corner: 1
  1040. }
  1041. }
  1042. },
  1043. _cutTouchEnd(e) {
  1044. this._moveStop();
  1045. this.data._flag_cut_touch = false;
  1046. },
  1047. //停止移动时需要做的操作
  1048. _moveStop() {
  1049. //清空之前的自动居中延迟函数并添加最新的
  1050. clearTimeout(this.data.TIME_CUT_CENTER);
  1051. this.data.TIME_CUT_CENTER = setTimeout(() => {
  1052. //动画启动
  1053. if (!this.data._cut_animation) {
  1054. this.setData({
  1055. _cut_animation: true
  1056. });
  1057. }
  1058. this.setCutCenter();
  1059. }, 1000)
  1060. //清空之前的背景变化延迟函数并添加最新的
  1061. clearTimeout(this.data.TIME_BG);
  1062. this.data.TIME_BG = setTimeout(() => {
  1063. if (this.data._flag_bright) {
  1064. this.setData({
  1065. _flag_bright: false
  1066. });
  1067. }
  1068. }, 2000)
  1069. },
  1070. //移动中
  1071. _moveDuring() {
  1072. //清空之前的自动居中延迟函数
  1073. clearTimeout(this.data.TIME_CUT_CENTER);
  1074. //清空之前的背景变化延迟函数
  1075. clearTimeout(this.data.TIME_BG);
  1076. //高亮背景
  1077. if (!this.data._flag_bright) {
  1078. this.setData({
  1079. _flag_bright: true
  1080. });
  1081. }
  1082. },
  1083. //监听器
  1084. _watcher() {
  1085. Object.keys(this.data).forEach(v => {
  1086. this._observe(this.data, v, this.data.watch[v]);
  1087. })
  1088. },
  1089. _observe(obj, key, watchFun) {
  1090. var val = obj[key];
  1091. Object.defineProperty(obj, key, {
  1092. configurable: true,
  1093. enumerable: true,
  1094. set: (value) => {
  1095. val = value;
  1096. watchFun && watchFun(val, this);
  1097. },
  1098. get() {
  1099. if (val && '_img_top|img_left||width|height|min_width|max_width|min_height|max_height|export_scale|cut_top|cut_left|canvas_top|canvas_left|img_width|img_height|scale|angle|min_scale|max_scale'.indexOf(key) != -1) {
  1100. let ret = parseFloat(parseFloat(val).toFixed(3));
  1101. if (typeof val == "string" && val.indexOf("%") != -1) {
  1102. ret += '%';
  1103. }
  1104. return ret;
  1105. }
  1106. return val;
  1107. }
  1108. })
  1109. },
  1110. _preventTouchMove() {}
  1111. }
  1112. })