小程序实现图片预览裁剪插件

前端之家收集整理的这篇文章主要介绍了小程序实现图片预览裁剪插件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在帮工作室做一个小程序,在换背景图的时候需要预览图片,并且需要裁剪成固定的尺寸。因为小程序不支持原生的dom操作,导致很多现有的插件都无法使用,所以花了半天专门做了一个小程序的预览裁剪插件。下面贴上代码效果图。

wxml:

  1. <canvas hidden='{{hide_canvas}}' id='cover-preview' bindtouchstart='canvas_start' bindtouchmove='canvas_move' bindtouchend='canvas_end' disable-scroll='true' canvas-id='cover-preview'>
  2. <cover-view catchtap='upload_bg' id='croper-sure'>确定</cover-view>
  3. <cover-view catchtap='cancel_croper' id='croper-cancel'>取消</cover-view>
  4. <cover-view id='croper'></cover-view>
  5. </canvas>

注意:canvas里面一定要用cover-view,否则无法覆盖canvas

js:

  1. const ctx = wx.createCanvasContext('cover-preview');
  2. var start_position = {};//移动图片时手指起始坐标
  3. var tempFilePath;//图片路径
  4. var tempWidth;//图片初始宽度
  5. var tempHeight;//图片初始高度
  6. var old_x = 0;//图片初始x坐标
  7. var old_y = 0;//图片初始y坐标
  8. var _touches = 1;//触屏的手指数
  9. var old_scale = 1;//原始放大倍数
  10. var new_scale = 1;//新的放大倍数
  11. var is_move = false;//是否移动
  12. var bg_url;
  13. Page({
  14. data: {
  15. hide_canvas:true,//绘图层显示控制变量
  16. },
  17. //选择并将图片输出到canvas
  18. change_cover:function(){
  19. var that = this;
  20. wx.showModal({
  21. title: '提示',content: '更改我的封面',confirmColor: '#39bae8',success: function (res) {
  22. if (res.confirm) {
  23.  
  24. wx.chooseImage({
  25. count: 1,// 默认9
  26. sizeType: ['original','compressed'],// 可以指定是原图还是压缩图,默认二者都有
  27. sourceType: ['album','camera'],// 可以指定来源是相册还是相机,默认二者都有
  28. success: function (res0) {
  29.  
  30. tempFilePath = res0.tempFilePaths[0];
  31. that.setData({
  32. hide_canvas: false,// edit_url: tempFilePath
  33. })
  34. wx.getImageInfo({
  35. src: tempFilePath,success: function (res) {
  36. // console.log(res.width)
  37. // console.log(res.height)
  38. tempWidth = res.width;
  39. tempHeight = res.height;
  40. ctx.drawImage(tempFilePath,375,res.height/res.width*375);
  41. ctx.draw();
  42. }
  43. })
  44.  
  45. }
  46. })
  47. } else if (res.cancel) {
  48. console.log('用户点击取消')
  49. }
  50. }
  51. })
  52. },//监听手指触摸事件,并判断是移动还是缩放,并记录初始状态
  53. canvas_start:function(e){
  54. // console.log(e);
  55. var touches = e.touches.length;
  56. if(touches == 1){
  57. _touches = 1;
  58. start_position = { x: e.touches[0].x,y: e.touches[0].y,timeStamp:e.timeStamp}
  59. }else if(touches == 2){
  60. _touches = 2;
  61. start_position = { x: e.touches[0].x,x1: e.touches[1].x,y1: e.touches[1].y,timeStamp: e.timeStamp }
  62. }else{
  63. _touches = 1;
  64. }
  65. },//监听手指移动事件,并做出相应调整
  66. canvas_move: function (e) {
  67. // console.log(e);
  68. var touches = e.touches.length;
  69. if (_touches == 1 && e.timeStamp - start_position.timeStamp > 150) {
  70. ctx.drawImage(tempFilePath,old_x + e.touches[0].x - start_position.x,old_y + e.touches[0].y - start_position.y,375 * new_scale,tempHeight / tempWidth * 375 * new_scale);
  71. ctx.draw();
  72. is_move = true;
  73. } else if (_touches == 2 && e.timeStamp - start_position.timeStamp > 150) {
  74. var change_x = Math.abs(Math.abs(e.touches[0].x - e.touches[1].x) - Math.abs(start_position.x - start_position.x1));
  75. var change_y = Math.abs(Math.abs(e.touches[0].y - e.touches[1].y) - Math.abs(start_position.y - start_position.y1));
  76. if(change_x - change_y > 10){
  77. old_scale = Math.abs(e.touches[0].x - e.touches[1].x) / Math.abs(start_position.x - start_position.x1);
  78. }else{
  79. old_scale = Math.abs(e.touches[0].y - e.touches[1].y) / Math.abs(start_position.y - start_position.y1);
  80. }
  81. ctx.drawImage(tempFilePath,old_x,old_y,375 * old_scale * new_scale,tempHeight / tempWidth * 375 * old_scale * new_scale);
  82. ctx.draw();
  83. is_move = true;
  84. }else{
  85. is_move = false;
  86. }
  87. },//监听手指离开动作,并保存当前状态数据
  88. canvas_end: function (e) {
  89. // console.log(e);
  90. if (_touches == 1 && is_move) {
  91. old_x = old_x + e.changedTouches[0].x - start_position.x;
  92. old_y = old_y + e.changedTouches[0].y - start_position.y;
  93. } else if (_touches == 2 && is_move) {
  94. new_scale = old_scale * new_scale;
  95. }
  96.  
  97. },//确定并上传背景图
  98. upload_bg:function(){
  99. var that = this;
  100. var screenWidth = wx.getSystemInfoSync().screenWidth;
  101. // console.log(screenWidth);
  102. wx.canvasToTempFilePath({
  103. x: 0,y: screenWidth / 750 * 400,width: screenWidth,height: screenWidth / 750 * 526,destWidth: screenWidth,screenHeight: screenWidth / 750 * 526,quality:1,canvasId: 'cover-preview',success: function (res) {
  104. that.setData({
  105. hide_canvas: true,})
  106. //res.tempFilePath即为生成图片路径
  107. console.log(res.tempFilePath)
  108.  
  109. }
  110. })
  111. },//取消图片预览编辑
  112. cancel_croper:function(){
  113. ctx.clearActions();
  114. this.setData({
  115. hide_canvas: true,// edit_url: tempFilePath
  116. })
  117. },})

wxss:

  1. #cover-preview{
  2. width: 100%;
  3. height: 100%;
  4. background-color: black;
  5. }
  6. #croper{
  7. width: 750rpx;
  8. height: 526rpx;
  9. position: absolute;
  10. top: 400rpx;
  11. left: 0;
  12. background-color: rgba(135,206,250,0.5);
  13. }
  14.  
  15. #croper-sure{
  16. width: 120rpx;
  17. height: 50rpx;
  18. border-radius: 10rpx;
  19. color: black;
  20. background-color: rgba(135,0.8);
  21. font-size: 40rpx;
  22. position: absolute;
  23. top: 946rpx;
  24. right: 10rpx;
  25. text-align: center
  26. }
  27. #croper-cancel{
  28. width: 120rpx;
  29. height: 50rpx;
  30. border-radius: 10rpx;
  31. color: black;
  32. background-color: rgba(135,0.8);
  33. font-size: 40rpx;
  34. position: absolute;
  35. top: 946rpx;
  36. right: 150rpx;
  37. text-align: center
  38. }

效果

小程序实现图片预览裁剪插件


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的JavaScript相关文章