image-cropper.js 45 KB

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