image-cropper.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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: true
  51. },
  52. 'disable_height': {
  53. type: Boolean,
  54. value: true
  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: true
  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.chooseMedia({
  288. count: 1,
  289. mediaType: ['image'],
  290. sourceType: ['album'],
  291. // camera: 'back',
  292. success(res) {
  293. const tempFilePaths = res.tempFiles[0].tempFilePath;
  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. * 返回图片数据data
  325. */
  326. getImgData(getCallback) {
  327. this._draw(() => {
  328. wx.canvasGetImageData({
  329. canvasId: this.data.el,
  330. x: 0,
  331. y: 0,
  332. width: this.data.width * this.data.export_scale,
  333. height: Math.round(this.data.height * this.data.export_scale),
  334. destWidth: this.data.width * this.data.export_scale,
  335. destHeight: Math.round(this.data.height) * this.data.export_scale,
  336. quality: this.data.quality,
  337. success: (res) => {
  338. getCallback({
  339. data: res.data,
  340. width: this.data.width * this.data.export_scale,
  341. height: this.data.height * this.data.export_scale
  342. });
  343. }
  344. }, this)
  345. });
  346. },
  347. /**
  348. * 设置图片动画
  349. * {
  350. * x:10,//图片在原有基础上向下移动10px
  351. * y:10,//图片在原有基础上向右移动10px
  352. * angle:10,//图片在原有基础上旋转10deg
  353. * scale:0.5,//图片在原有基础上增加0.5倍
  354. * }
  355. */
  356. setTransform(transform) {
  357. if (!transform) return;
  358. if (!this.data.disable_rotate) {
  359. this.setData({
  360. angle: transform.angle ? this.data.angle + transform.angle : this.data.angle
  361. });
  362. }
  363. var scale = this.data.scale;
  364. if (transform.scale) {
  365. scale = this.data.scale + transform.scale;
  366. scale = scale <= this.data.min_scale ? this.data.min_scale : scale;
  367. scale = scale >= this.data.max_scale ? this.data.max_scale : scale;
  368. }
  369. this.data.scale = scale;
  370. let cutX = this.data.cut_left;
  371. let cutY = this.data.cut_top;
  372. if (transform.cutX) {
  373. this.setData({
  374. cut_left: cutX + transform.cutX
  375. });
  376. this.data.watch.cut_left(null, this);
  377. }
  378. if (transform.cutY) {
  379. this.setData({
  380. cut_top: cutY + transform.cutY
  381. });
  382. this.data.watch.cut_top(null, this);
  383. }
  384. this.data._img_top = transform.y ? this.data._img_top + transform.y : this.data._img_top;
  385. this.data._img_left = transform.x ? this.data._img_left + transform.x : this.data._img_left;
  386. //图像边缘检测,防止截取到空白
  387. this._imgMarginDetectionScale();
  388. //停止居中裁剪框,继续修改图片位置
  389. this._moveDuring();
  390. this.setData({
  391. scale: this.data.scale,
  392. _img_top: this.data._img_top,
  393. _img_left: this.data._img_left
  394. });
  395. !this.data._canvas_overflow && this._draw();
  396. //可以居中裁剪框了
  397. this._moveStop(); //结束操作
  398. },
  399. /**
  400. * 设置剪裁框位置
  401. */
  402. setCutXY(x, y) {
  403. this.setData({
  404. cut_top: y,
  405. cut_left: x
  406. });
  407. },
  408. /**
  409. * 设置剪裁框尺寸
  410. */
  411. setCutSize(w, h) {
  412. this.setData({
  413. width: w,
  414. height: h
  415. });
  416. this._computeCutSize();
  417. },
  418. /**
  419. * 设置剪裁框和图片居中
  420. */
  421. setCutCenter() {
  422. let cut_top = (this.data.info.windowHeight - this.data.height) * 0.5;
  423. let cut_left = (this.data.info.windowWidth - this.data.width) * 0.5;
  424. //顺序不能变
  425. this.setData({
  426. _img_top: this.data._img_top - this.data.cut_top + cut_top,
  427. cut_top: cut_top, //截取的框上边距
  428. _img_left: this.data._img_left - this.data.cut_left + cut_left,
  429. cut_left: cut_left, //截取的框左边距
  430. });
  431. },
  432. _setCutCenter() {
  433. let cut_top = (this.data.info.windowHeight - this.data.height) * 0.5;
  434. let cut_left = (this.data.info.windowWidth - this.data.width) * 0.5;
  435. this.setData({
  436. cut_top: cut_top, //截取的框上边距
  437. cut_left: cut_left, //截取的框左边距
  438. });
  439. },
  440. /**
  441. * 设置剪裁框宽度-即将废弃
  442. */
  443. setWidth(width) {
  444. this.setData({
  445. width: width
  446. });
  447. this._computeCutSize();
  448. },
  449. /**
  450. * 设置剪裁框高度-即将废弃
  451. */
  452. setHeight(height) {
  453. this.setData({
  454. height: height
  455. });
  456. this._computeCutSize();
  457. },
  458. /**
  459. * 是否锁定旋转
  460. */
  461. setDisableRotate(value) {
  462. this.data.disable_rotate = value;
  463. },
  464. /**
  465. * 是否限制移动
  466. */
  467. setLimitMove(value) {
  468. this.setData({
  469. _cut_animation: true,
  470. limit_move: !!value
  471. });
  472. },
  473. /**
  474. * 初始化图片,包括位置、大小、旋转角度
  475. */
  476. imgReset() {
  477. this.setData({
  478. scale: 1,
  479. angle: 0,
  480. _img_top: wx.getSystemInfoSync().windowHeight / 2,
  481. _img_left: wx.getSystemInfoSync().windowWidth / 2,
  482. })
  483. },
  484. /**
  485. * 加载(更换)图片
  486. */
  487. pushImg(src) {
  488. if (src) {
  489. this.setData({
  490. imgSrc: src
  491. });
  492. //发现是手动赋值直接返回,交给watch处理
  493. return;
  494. }
  495. // getImageInfo接口传入 src: '' 会导致内存泄漏
  496. if (!this.data.imgSrc) return;
  497. wx.getImageInfo({
  498. src: this.data.imgSrc,
  499. success: (res) => {
  500. this.data.imageObject = res;
  501. //图片非本地路径需要换成本地路径
  502. if (this.data.imgSrc.search(/tmp/) == -1) {
  503. this.setData({
  504. imgSrc: res.path
  505. });
  506. }
  507. //计算最后图片尺寸
  508. this._imgComputeSize();
  509. if (this.data.limit_move) {
  510. //限制移动,不留空白处理
  511. this._imgMarginDetectionScale();
  512. }
  513. this._draw();
  514. },
  515. fail: (err) => {
  516. this.setData({
  517. imgSrc: ''
  518. });
  519. }
  520. });
  521. },
  522. imageLoad(e) {
  523. setTimeout(() => {
  524. this.triggerEvent('imageload', this.data.imageObject);
  525. }, 1000)
  526. },
  527. /**
  528. * 设置图片放大缩小
  529. */
  530. setScale(scale) {
  531. if (!scale) return;
  532. this.setData({
  533. scale: scale
  534. });
  535. !this.data._canvas_overflow && this._draw();
  536. },
  537. /**
  538. * 设置图片旋转角度
  539. */
  540. setAngle(angle) {
  541. if (!angle) return;
  542. this.setData({
  543. _cut_animation: true,
  544. angle: angle
  545. });
  546. this._imgMarginDetectionScale();
  547. !this.data._canvas_overflow && this._draw();
  548. },
  549. _initCanvas() {
  550. //初始化canvas
  551. if (!this.data.ctx) {
  552. this.data.ctx = wx.createCanvasContext("image-cropper", this);
  553. }
  554. },
  555. /**
  556. * 根据开发者设置的图片目标尺寸计算实际尺寸
  557. */
  558. _initImageSize() {
  559. //处理宽高特殊单位 %>px
  560. if (this.data.INIT_IMGWIDTH && typeof this.data.INIT_IMGWIDTH == "string" && this.data.INIT_IMGWIDTH.indexOf("%") != -1) {
  561. let width = this.data.INIT_IMGWIDTH.replace("%", "");
  562. this.data.INIT_IMGWIDTH = this.data.img_width = this.data.info.windowWidth / 100 * width;
  563. }
  564. if (this.data.INIT_IMGHEIGHT && typeof this.data.INIT_IMGHEIGHT == "string" && this.data.INIT_IMGHEIGHT.indexOf("%") != -1) {
  565. let height = this.data.img_height.replace("%", "");
  566. this.data.INIT_IMGHEIGHT = this.data.img_height = this.data.info.windowHeight / 100 * height;
  567. }
  568. },
  569. /**
  570. * 检测剪裁框位置是否在允许的范围内(屏幕内)
  571. */
  572. _cutDetectionPosition() {
  573. let _cutDetectionPositionTop = () => {
  574. //检测上边距是否在范围内
  575. if (this.data.cut_top < 0) {
  576. this.setData({
  577. cut_top: 0
  578. });
  579. }
  580. if (this.data.cut_top > this.data.info.windowHeight - this.data.height) {
  581. this.setData({
  582. cut_top: this.data.info.windowHeight - this.data.height
  583. });
  584. }
  585. },
  586. _cutDetectionPositionLeft = () => {
  587. //检测左边距是否在范围内
  588. if (this.data.cut_left < 0) {
  589. this.setData({
  590. cut_left: 0
  591. });
  592. }
  593. if (this.data.cut_left > this.data.info.windowWidth - this.data.width) {
  594. this.setData({
  595. cut_left: this.data.info.windowWidth - this.data.width
  596. });
  597. }
  598. };
  599. //裁剪框坐标处理(如果只写一个参数则另一个默认为0,都不写默认居中)
  600. if (this.data.cut_top == null && this.data.cut_left == null) {
  601. this._setCutCenter();
  602. } else if (this.data.cut_top != null && this.data.cut_left != null) {
  603. _cutDetectionPositionTop();
  604. _cutDetectionPositionLeft();
  605. } else if (this.data.cut_top != null && this.data.cut_left == null) {
  606. _cutDetectionPositionTop();
  607. this.setData({
  608. cut_left: (this.data.info.windowWidth - this.data.width) / 2
  609. });
  610. } else if (this.data.cut_top == null && this.data.cut_left != null) {
  611. _cutDetectionPositionLeft();
  612. this.setData({
  613. cut_top: (this.data.info.windowHeight - this.data.height) / 2
  614. });
  615. }
  616. },
  617. /**
  618. * 检测canvas位置是否在允许的范围内(屏幕内)如果在屏幕外则不开启实时渲染
  619. * 如果只写一个参数则另一个默认为0,都不写默认超出屏幕外
  620. */
  621. _canvasDetectionPosition() {
  622. if (this.data.canvas_top == null && this.data.canvas_left == null) {
  623. this.data._canvas_overflow = false;
  624. this.setData({
  625. canvas_top: -5000,
  626. canvas_left: -5000
  627. });
  628. } else if (this.data.canvas_top != null && this.data.canvas_left != null) {
  629. if (this.data.canvas_top < -this.data.height || this.data.canvas_top > this.data.info.windowHeight) {
  630. this.data._canvas_overflow = true;
  631. } else {
  632. this.data._canvas_overflow = false;
  633. }
  634. } else if (this.data.canvas_top != null && this.data.canvas_left == null) {
  635. this.setData({
  636. canvas_left: 0
  637. });
  638. } else if (this.data.canvas_top == null && this.data.canvas_left != null) {
  639. this.setData({
  640. canvas_top: 0
  641. });
  642. if (this.data.canvas_left < -this.data.width || this.data.canvas_left > this.data.info.windowWidth) {
  643. this.data._canvas_overflow = true;
  644. } else {
  645. this.data._canvas_overflow = false;
  646. }
  647. }
  648. },
  649. /**
  650. * 图片边缘检测-位置
  651. */
  652. _imgMarginDetectionPosition(scale) {
  653. if (!this.data.limit_move) return;
  654. let left = this.data._img_left;
  655. let top = this.data._img_top;
  656. var scale = scale || this.data.scale;
  657. let img_width = this.data.img_width;
  658. let img_height = this.data.img_height;
  659. if (this.data.angle / 90 % 2) {
  660. img_width = this.data.img_height;
  661. img_height = this.data.img_width;
  662. }
  663. left = this.data.cut_left + img_width * scale / 2 >= left ? left : this.data.cut_left + img_width * scale / 2;
  664. 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;
  665. top = this.data.cut_top + img_height * scale / 2 >= top ? top : this.data.cut_top + img_height * scale / 2;
  666. 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;
  667. this.setData({
  668. _img_left: left,
  669. _img_top: top,
  670. scale: scale
  671. })
  672. },
  673. /**
  674. * 图片边缘检测-缩放
  675. */
  676. _imgMarginDetectionScale() {
  677. if (!this.data.limit_move) return;
  678. let scale = this.data.scale;
  679. let img_width = this.data.img_width;
  680. let img_height = this.data.img_height;
  681. if (this.data.angle / 90 % 2) {
  682. img_width = this.data.img_height;
  683. img_height = this.data.img_width;
  684. }
  685. if (img_width * scale < this.data.width) {
  686. scale = this.data.width / img_width;
  687. }
  688. if (img_height * scale < this.data.height) {
  689. scale = Math.max(scale, this.data.height / img_height);
  690. }
  691. this._imgMarginDetectionPosition(scale);
  692. },
  693. _setData(obj) {
  694. let data = {};
  695. for (var key in obj) {
  696. if (this.data[key] != obj[key]) {
  697. data[key] = obj[key];
  698. }
  699. }
  700. this.setData(data);
  701. return data;
  702. },
  703. /**
  704. * 计算图片尺寸
  705. */
  706. _imgComputeSize() {
  707. let img_width = this.data.img_width,
  708. img_height = this.data.img_height;
  709. if (!this.data.INIT_IMGHEIGHT && !this.data.INIT_IMGWIDTH) {
  710. //默认按图片最小边 = 对应裁剪框尺寸
  711. img_width = this.data.imageObject.width;
  712. img_height = this.data.imageObject.height;
  713. if (img_width / img_height > this.data.width / this.data.height) {
  714. img_height = this.data.height;
  715. img_width = this.data.imageObject.width / this.data.imageObject.height * img_height;
  716. } else {
  717. img_width = this.data.width;
  718. img_height = this.data.imageObject.height / this.data.imageObject.width * img_width;
  719. }
  720. } else if (this.data.INIT_IMGHEIGHT && !this.data.INIT_IMGWIDTH) {
  721. img_width = this.data.imageObject.width / this.data.imageObject.height * this.data.INIT_IMGHEIGHT;
  722. } else if (!this.data.INIT_IMGHEIGHT && this.data.INIT_IMGWIDTH) {
  723. img_height = this.data.imageObject.height / this.data.imageObject.width * this.data.INIT_IMGWIDTH;
  724. }
  725. this.setData({
  726. img_width: img_width,
  727. img_height: img_height
  728. });
  729. },
  730. //改变截取框大小
  731. _computeCutSize() {
  732. if (this.data.width > this.data.info.windowWidth) {
  733. this.setData({
  734. width: this.data.info.windowWidth,
  735. });
  736. } else if (this.data.width + this.data.cut_left > this.data.info.windowWidth) {
  737. this.setData({
  738. cut_left: this.data.info.windowWidth - this.data.cut_left,
  739. });
  740. };
  741. if (this.data.height > this.data.info.windowHeight) {
  742. this.setData({
  743. height: this.data.info.windowHeight,
  744. });
  745. } else if (this.data.height + this.data.cut_top > this.data.info.windowHeight) {
  746. this.setData({
  747. cut_top: this.data.info.windowHeight - this.data.cut_top,
  748. });
  749. } !this.data._canvas_overflow && this._draw();
  750. },
  751. //开始触摸
  752. _start(event) {
  753. this.data._flag_img_endtouch = false;
  754. if (event.touches.length == 1) {
  755. //单指拖动
  756. this.data._touch_img_relative[0] = {
  757. x: (event.touches[0].clientX - this.data._img_left),
  758. y: (event.touches[0].clientY - this.data._img_top)
  759. }
  760. } else {
  761. //双指放大
  762. let width = Math.abs(event.touches[0].clientX - event.touches[1].clientX);
  763. let height = Math.abs(event.touches[0].clientY - event.touches[1].clientY);
  764. this.data._touch_img_relative = [{
  765. x: (event.touches[0].clientX - this.data._img_left),
  766. y: (event.touches[0].clientY - this.data._img_top)
  767. }, {
  768. x: (event.touches[1].clientX - this.data._img_left),
  769. y: (event.touches[1].clientY - this.data._img_top)
  770. }];
  771. this.data._hypotenuse_length = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  772. } !this.data._canvas_overflow && this._draw();
  773. },
  774. _move_throttle() {
  775. //安卓需要节流
  776. if (this.data.info.platform == 'android') {
  777. clearTimeout(this.data.MOVE_THROTTLE);
  778. this.data.MOVE_THROTTLE = setTimeout(() => {
  779. this.data.MOVE_THROTTLE_FLAG = true;
  780. }, 1000 / 40)
  781. return this.data.MOVE_THROTTLE_FLAG;
  782. } else {
  783. this.data.MOVE_THROTTLE_FLAG = true;
  784. }
  785. },
  786. _move(event) {
  787. if (this.data._flag_img_endtouch || !this.data.MOVE_THROTTLE_FLAG) return;
  788. this.data.MOVE_THROTTLE_FLAG = false;
  789. this._move_throttle();
  790. this._moveDuring();
  791. if (event.touches.length == 1) {
  792. //单指拖动
  793. let left = (event.touches[0].clientX - this.data._touch_img_relative[0].x),
  794. top = (event.touches[0].clientY - this.data._touch_img_relative[0].y);
  795. //图像边缘检测,防止截取到空白
  796. this.data._img_left = left;
  797. this.data._img_top = top;
  798. this._imgMarginDetectionPosition();
  799. this.setData({
  800. _img_left: this.data._img_left,
  801. _img_top: this.data._img_top
  802. });
  803. } else {
  804. //双指放大
  805. let width = (Math.abs(event.touches[0].clientX - event.touches[1].clientX)),
  806. height = (Math.abs(event.touches[0].clientY - event.touches[1].clientY)),
  807. hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)),
  808. scale = this.data.scale * (hypotenuse / this.data._hypotenuse_length),
  809. current_deg = 0;
  810. scale = scale <= this.data.min_scale ? this.data.min_scale : scale;
  811. scale = scale >= this.data.max_scale ? this.data.max_scale : scale;
  812. //图像边缘检测,防止截取到空白
  813. this.data.scale = scale;
  814. this._imgMarginDetectionScale();
  815. //双指旋转(如果没禁用旋转)
  816. let _touch_img_relative = [{
  817. x: (event.touches[0].clientX - this.data._img_left),
  818. y: (event.touches[0].clientY - this.data._img_top)
  819. }, {
  820. x: (event.touches[1].clientX - this.data._img_left),
  821. y: (event.touches[1].clientY - this.data._img_top)
  822. }];
  823. if (!this.data.disable_rotate) {
  824. let first_atan = 180 / Math.PI * Math.atan2(_touch_img_relative[0].y, _touch_img_relative[0].x);
  825. let first_atan_old = 180 / Math.PI * Math.atan2(this.data._touch_img_relative[0].y, this.data._touch_img_relative[0].x);
  826. let second_atan = 180 / Math.PI * Math.atan2(_touch_img_relative[1].y, _touch_img_relative[1].x);
  827. let second_atan_old = 180 / Math.PI * Math.atan2(this.data._touch_img_relative[1].y, this.data._touch_img_relative[1].x);
  828. //当前旋转的角度
  829. let first_deg = first_atan - first_atan_old,
  830. second_deg = second_atan - second_atan_old;
  831. if (first_deg != 0) {
  832. current_deg = first_deg;
  833. } else if (second_deg != 0) {
  834. current_deg = second_deg;
  835. }
  836. }
  837. this.data._touch_img_relative = _touch_img_relative;
  838. this.data._hypotenuse_length = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
  839. //更新视图
  840. this.setData({
  841. angle: this.data.angle + current_deg,
  842. scale: this.data.scale
  843. });
  844. } !this.data._canvas_overflow && this._draw();
  845. },
  846. //结束操作
  847. _end(event) {
  848. this.data._flag_img_endtouch = true;
  849. this._moveStop();
  850. },
  851. //点击中间剪裁框处理
  852. _click(event) {
  853. if (!this.data.imgSrc) {
  854. //调起上传
  855. this.upload();
  856. return;
  857. }
  858. this._draw(() => {
  859. let x = event.detail ? event.detail.x : event.touches[0].clientX;
  860. let y = event.detail ? event.detail.y : event.touches[0].clientY;
  861. 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))) {
  862. //生成图片并回调
  863. wx.canvasToTempFilePath({
  864. width: this.data.width * this.data.export_scale,
  865. height: Math.round(this.data.height * this.data.export_scale),
  866. destWidth: this.data.width * this.data.export_scale,
  867. destHeight: Math.round(this.data.height) * this.data.export_scale,
  868. fileType: 'png',
  869. quality: this.data.quality,
  870. canvasId: this.data.el,
  871. success: (res) => {
  872. this.triggerEvent('tapcut', {
  873. url: res.tempFilePath,
  874. width: this.data.width * this.data.export_scale,
  875. height: this.data.height * this.data.export_scale
  876. });
  877. }
  878. }, this)
  879. }
  880. });
  881. },
  882. //渲染
  883. _draw(callback) {
  884. if (!this.data.imgSrc) return;
  885. let draw = () => {
  886. //图片实际大小
  887. let img_width = this.data.img_width * this.data.scale * this.data.export_scale;
  888. let img_height = this.data.img_height * this.data.scale * this.data.export_scale;
  889. //canvas和图片的相对距离
  890. var xpos = this.data._img_left - this.data.cut_left;
  891. var ypos = this.data._img_top - this.data.cut_top;
  892. //旋转画布
  893. this.data.ctx.translate(xpos * this.data.export_scale, ypos * this.data.export_scale);
  894. this.data.ctx.rotate(this.data.angle * Math.PI / 180);
  895. this.data.ctx.drawImage(this.data.imgSrc, -img_width / 2, -img_height / 2, img_width, img_height);
  896. this.data.ctx.draw(false, () => {
  897. callback && callback();
  898. });
  899. }
  900. if (this.data.ctx.width != this.data.width || this.data.ctx.height != this.data.height) {
  901. //优化拖动裁剪框,所以必须把宽高设置放在离用户触发渲染最近的地方
  902. this.setData({
  903. _canvas_height: this.data.height,
  904. _canvas_width: this.data.width,
  905. }, () => {
  906. //延迟40毫秒防止点击过快出现拉伸或裁剪过多
  907. setTimeout(() => {
  908. draw();
  909. }, 40);
  910. });
  911. } else {
  912. draw();
  913. }
  914. },
  915. //裁剪框处理
  916. _cutTouchMove(e) {
  917. if (this.data._flag_cut_touch && this.data.MOVE_THROTTLE_FLAG) {
  918. if (this.data.disable_ratio && (this.data.disable_width || this.data.disable_height)) return;
  919. //节流
  920. this.data.MOVE_THROTTLE_FLAG = false;
  921. this._move_throttle();
  922. let width = this.data.width,
  923. height = this.data.height,
  924. cut_top = this.data.cut_top,
  925. cut_left = this.data.cut_left,
  926. size_correct = () => {
  927. width = width <= this.data.max_width ? width >= this.data.min_width ? width : this.data.min_width : this.data.max_width;
  928. height = height <= this.data.max_height ? height >= this.data.min_height ? height : this.data.min_height : this.data.max_height;
  929. },
  930. size_inspect = () => {
  931. 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) {
  932. size_correct();
  933. return false;
  934. } else {
  935. size_correct();
  936. return true;
  937. }
  938. };
  939. 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));
  940. switch (this.data.CUT_START.corner) {
  941. case 1:
  942. width = this.data.CUT_START.width + this.data.CUT_START.x - e.touches[0].clientX;
  943. if (this.data.disable_ratio) {
  944. height = width / (this.data.width / this.data.height)
  945. }
  946. if (!size_inspect()) return;
  947. cut_left = this.data.CUT_START.cut_left - (width - this.data.CUT_START.width);
  948. break
  949. case 2:
  950. width = this.data.CUT_START.width + this.data.CUT_START.x - e.touches[0].clientX;
  951. if (this.data.disable_ratio) {
  952. height = width / (this.data.width / this.data.height)
  953. }
  954. if (!size_inspect()) return;
  955. cut_top = this.data.CUT_START.cut_top - (height - this.data.CUT_START.height)
  956. cut_left = this.data.CUT_START.cut_left - (width - this.data.CUT_START.width)
  957. break
  958. case 3:
  959. width = this.data.CUT_START.width - this.data.CUT_START.x + e.touches[0].clientX;
  960. if (this.data.disable_ratio) {
  961. height = width / (this.data.width / this.data.height)
  962. }
  963. if (!size_inspect()) return;
  964. cut_top = this.data.CUT_START.cut_top - (height - this.data.CUT_START.height);
  965. break
  966. case 4:
  967. width = this.data.CUT_START.width - this.data.CUT_START.x + e.touches[0].clientX;
  968. if (this.data.disable_ratio) {
  969. height = width / (this.data.width / this.data.height)
  970. }
  971. if (!size_inspect()) return;
  972. break
  973. }
  974. if (!this.data.disable_width && !this.data.disable_height) {
  975. this.setData({
  976. width: width,
  977. cut_left: cut_left,
  978. height: height,
  979. cut_top: cut_top,
  980. })
  981. } else if (!this.data.disable_width) {
  982. this.setData({
  983. width: width,
  984. cut_left: cut_left
  985. })
  986. } else if (!this.data.disable_height) {
  987. this.setData({
  988. height: height,
  989. cut_top: cut_top
  990. })
  991. }
  992. this._imgMarginDetectionScale();
  993. }
  994. },
  995. _cutTouchStart(e) {
  996. let currentX = e.touches[0].clientX;
  997. let currentY = e.touches[0].clientY;
  998. let cutbox_top4 = this.data.cut_top + this.data.height - 30;
  999. let cutbox_bottom4 = this.data.cut_top + this.data.height + 20;
  1000. let cutbox_left4 = this.data.cut_left + this.data.width - 30;
  1001. let cutbox_right4 = this.data.cut_left + this.data.width + 30;
  1002. let cutbox_top3 = this.data.cut_top - 30;
  1003. let cutbox_bottom3 = this.data.cut_top + 30;
  1004. let cutbox_left3 = this.data.cut_left + this.data.width - 30;
  1005. let cutbox_right3 = this.data.cut_left + this.data.width + 30;
  1006. let cutbox_top2 = this.data.cut_top - 30;
  1007. let cutbox_bottom2 = this.data.cut_top + 30;
  1008. let cutbox_left2 = this.data.cut_left - 30;
  1009. let cutbox_right2 = this.data.cut_left + 30;
  1010. let cutbox_top1 = this.data.cut_top + this.data.height - 30;
  1011. let cutbox_bottom1 = this.data.cut_top + this.data.height + 30;
  1012. let cutbox_left1 = this.data.cut_left - 30;
  1013. let cutbox_right1 = this.data.cut_left + 30;
  1014. if (currentX > cutbox_left4 && currentX < cutbox_right4 && currentY > cutbox_top4 && currentY < cutbox_bottom4) {
  1015. this._moveDuring();
  1016. this.data._flag_cut_touch = true;
  1017. this.data._flag_img_endtouch = true;
  1018. this.data.CUT_START = {
  1019. width: this.data.width,
  1020. height: this.data.height,
  1021. x: currentX,
  1022. y: currentY,
  1023. corner: 4
  1024. }
  1025. } else if (currentX > cutbox_left3 && currentX < cutbox_right3 && currentY > cutbox_top3 && currentY < cutbox_bottom3) {
  1026. this._moveDuring();
  1027. this.data._flag_cut_touch = true;
  1028. this.data._flag_img_endtouch = true;
  1029. this.data.CUT_START = {
  1030. width: this.data.width,
  1031. height: this.data.height,
  1032. x: currentX,
  1033. y: currentY,
  1034. cut_top: this.data.cut_top,
  1035. cut_left: this.data.cut_left,
  1036. corner: 3
  1037. }
  1038. } else if (currentX > cutbox_left2 && currentX < cutbox_right2 && currentY > cutbox_top2 && currentY < cutbox_bottom2) {
  1039. this._moveDuring();
  1040. this.data._flag_cut_touch = true;
  1041. this.data._flag_img_endtouch = true;
  1042. this.data.CUT_START = {
  1043. width: this.data.width,
  1044. height: this.data.height,
  1045. cut_top: this.data.cut_top,
  1046. cut_left: this.data.cut_left,
  1047. x: currentX,
  1048. y: currentY,
  1049. corner: 2
  1050. }
  1051. } else if (currentX > cutbox_left1 && currentX < cutbox_right1 && currentY > cutbox_top1 && currentY < cutbox_bottom1) {
  1052. this._moveDuring();
  1053. this.data._flag_cut_touch = true;
  1054. this.data._flag_img_endtouch = true;
  1055. this.data.CUT_START = {
  1056. width: this.data.width,
  1057. height: this.data.height,
  1058. cut_top: this.data.cut_top,
  1059. cut_left: this.data.cut_left,
  1060. x: currentX,
  1061. y: currentY,
  1062. corner: 1
  1063. }
  1064. }
  1065. },
  1066. _cutTouchEnd(e) {
  1067. this._moveStop();
  1068. this.data._flag_cut_touch = false;
  1069. },
  1070. //停止移动时需要做的操作
  1071. _moveStop() {
  1072. //清空之前的自动居中延迟函数并添加最新的
  1073. clearTimeout(this.data.TIME_CUT_CENTER);
  1074. this.data.TIME_CUT_CENTER = setTimeout(() => {
  1075. //动画启动
  1076. if (!this.data._cut_animation) {
  1077. this.setData({
  1078. _cut_animation: true
  1079. });
  1080. }
  1081. this.setCutCenter();
  1082. }, 1000)
  1083. //清空之前的背景变化延迟函数并添加最新的
  1084. clearTimeout(this.data.TIME_BG);
  1085. this.data.TIME_BG = setTimeout(() => {
  1086. if (this.data._flag_bright) {
  1087. this.setData({
  1088. _flag_bright: false
  1089. });
  1090. }
  1091. }, 2000)
  1092. },
  1093. //移动中
  1094. _moveDuring() {
  1095. //清空之前的自动居中延迟函数
  1096. clearTimeout(this.data.TIME_CUT_CENTER);
  1097. //清空之前的背景变化延迟函数
  1098. clearTimeout(this.data.TIME_BG);
  1099. //高亮背景
  1100. if (!this.data._flag_bright) {
  1101. this.setData({
  1102. _flag_bright: true
  1103. });
  1104. }
  1105. },
  1106. //监听器
  1107. _watcher() {
  1108. Object.keys(this.data).forEach(v => {
  1109. this._observe(this.data, v, this.data.watch[v]);
  1110. })
  1111. },
  1112. _observe(obj, key, watchFun) {
  1113. var val = obj[key];
  1114. Object.defineProperty(obj, key, {
  1115. configurable: true,
  1116. enumerable: true,
  1117. set: (value) => {
  1118. val = value;
  1119. watchFun && watchFun(val, this);
  1120. },
  1121. get() {
  1122. 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) {
  1123. let ret = parseFloat(parseFloat(val).toFixed(3));
  1124. if (typeof val == "string" && val.indexOf("%") != -1) {
  1125. ret += '%';
  1126. }
  1127. return ret;
  1128. }
  1129. return val;
  1130. }
  1131. })
  1132. },
  1133. _preventTouchMove() { }
  1134. }
  1135. })