clip.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // pages/clip/clip.js
  2. const HOST = require('../../utils/const.js').apiUrl;
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imageUrl: '',
  9. cropperW: '',
  10. cropperH: '',
  11. img_ratio: '',
  12. IMG_W: '',
  13. IMG_H: '',
  14. clipImage: '',
  15. left: '',
  16. top: '',
  17. clipW: 200
  18. },
  19. //点击取消
  20. cancel: function () {
  21. wx.navigateBack();
  22. },
  23. //拖拽事件
  24. move: function ({ detail }) {
  25. this.setData({
  26. left: detail.x * 2,
  27. top: detail.y * 2
  28. })
  29. },
  30. //缩放事件
  31. scale: function ({detail}) {
  32. console.log(detail.scale)
  33. this.setData({
  34. clipW: 200 * detail.scale
  35. })
  36. },
  37. //生成图片
  38. getImageInfo: function () {
  39. wx.showLoading({
  40. title: '图片生成中...',
  41. })
  42. const img_ratio = this.data.img_ratio;
  43. const canvasW = (this.data.clipW / this.data.cropperW) * this.data.IMG_W
  44. const canvasH = (this.data.clipW / this.data.cropperH) * this.data.IMG_H
  45. const canvasL = (this.data.left / this.data.cropperW) * this.data.IMG_W
  46. const canvasT = (this.data.top / this.data.cropperH) * this.data.IMG_H
  47. // 将图片写入画布
  48. const ctx = wx.createCanvasContext('myCanvas');
  49. //绘制图像到画布
  50. ctx.save(); // 先保存状态 已便于画完圆再用
  51. ctx.beginPath(); //开始绘制
  52. ctx.clearRect(0, 0, 1000, 1000)
  53. //先画个圆
  54. ctx.arc(this.data.clipW / 2, this.data.clipW / 2, this.data.clipW / 2, 0, 2 * Math.PI, false)
  55. ctx.clip();//画了圆 再剪切 原始画布中剪切任意形状和尺寸。一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内
  56. // ctx.setFillStyle('#EEEEEE')
  57. // ctx.fill()
  58. ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.clipW, this.data.clipW); // 推进去图片
  59. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  60. ctx.draw(true, () => {
  61. // 获取画布要裁剪的位置和宽度
  62. wx.canvasToTempFilePath({
  63. x: 0,
  64. y: 0,
  65. width: this.data.clipW,
  66. height: this.data.clipW,
  67. destWidth: this.data.clipW,
  68. destHeight: this.data.clipW,
  69. quality: 0.5,
  70. canvasId: 'myCanvas',
  71. success: (res) => {
  72. wx.hideLoading()
  73. this.setData({
  74. clipImage: res.tempFilePath
  75. })
  76. //上传文件
  77. wx.uploadFile({
  78. url: HOST + 'wx/file/upload',
  79. filePath: res.tempFilePath,
  80. name: 'uploadfile_ant',
  81. header: {
  82. "Content-Type": "multipart/form-data"
  83. },
  84. success: (res) => {
  85. console.log(res);
  86. const data = JSON.parse(res.data);
  87. if (data.success) {
  88. wx.navigateTo({
  89. url: '/pages/setName/setName?imageUrl=' + data.data
  90. })
  91. }
  92. },
  93. fail: function (res) {
  94. wx.hideToast();
  95. wx.showModal({
  96. title: '错误提示',
  97. content: '上传图片失败'
  98. })
  99. }
  100. });
  101. //成功获得地址的地方
  102. // wx.previewImage({
  103. // current: '', // 当前显示图片的http链接
  104. // urls: [res.tempFilePath] // 需要预览的图片http链接列表
  105. // })
  106. }
  107. })
  108. })
  109. },
  110. /**
  111. * 生命周期函数--监听页面加载
  112. */
  113. onLoad: function (options) {
  114. console.log(options.imageUrl);
  115. const imageUrl = options.imageUrl;
  116. this.setData({
  117. imageUrl
  118. })
  119. wx.getImageInfo({
  120. src: imageUrl,
  121. success: (res) => {
  122. console.log(res);
  123. //图片实际款高
  124. const width = res.width;
  125. const height = res.height;
  126. //图片宽高比例
  127. const img_ratio = width / height
  128. this.setData({
  129. img_ratio,
  130. IMG_W: width,
  131. IMG_H: height,
  132. })
  133. if (img_ratio >= 1) {
  134. //宽比较大,横着显示
  135. this.setData({
  136. cropperW: 750,
  137. cropperH: 750 / img_ratio,
  138. })
  139. } else {
  140. //竖着显示
  141. this.setData({
  142. cropperW: 750 * img_ratio,
  143. cropperH: 750
  144. })
  145. }
  146. }
  147. })
  148. },
  149. /**
  150. * 生命周期函数--监听页面初次渲染完成
  151. */
  152. onReady: function () {
  153. },
  154. /**
  155. * 生命周期函数--监听页面显示
  156. */
  157. onShow: function () {
  158. },
  159. /**
  160. * 生命周期函数--监听页面隐藏
  161. */
  162. onHide: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面卸载
  166. */
  167. onUnload: function () {
  168. },
  169. /**
  170. * 页面相关事件处理函数--监听用户下拉动作
  171. */
  172. onPullDownRefresh: function () {
  173. },
  174. /**
  175. * 页面上拉触底事件的处理函数
  176. */
  177. onReachBottom: function () {
  178. },
  179. /**
  180. * 用户点击右上角分享
  181. */
  182. onShareAppMessage: function () {
  183. }
  184. })