Chinaunix首页 | 论坛 | 博客
  • 博客访问: 957087
  • 博文数量: 210
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2070
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 21:54
文章分类

全部博文(210)

文章存档

2020年(2)

2019年(18)

2018年(27)

2017年(5)

2016年(53)

2015年(88)

2014年(17)

分类: JavaScript

2019-05-07 14:39:53


点击(此处)折叠或打开

  1. <view class='img' bindtap='img'>上传文件</view>
  2. <canvas class='canvas' canvas-id='myCancas' style="width:{{width}}px;height:{{height}}px;background:red;"></canvas>
  3. <view class='img_box'><image wx:for="{{arr}}" src="{{item}}" mode='widthFix'></image></view>


点击(此处)折叠或打开

  1. page{
  2.   position: relative;
  3. }
  4. .img_box{
  5.   /* display: flex;
  6.   justify-items: center;
  7.   align-items: center; */
  8.   text-align: center;
  9. }
  10. .canvas{
  11.  position:absolute;
  12.   top:-999999999999999999999rpx;
  13.   left:0;

  14. }


点击(此处)折叠或打开

  1. // pages/upfile/upfile.js
  2. const ctx = wx.createCanvasContext("myCancas", this)
  3. Page({

  4.   /**
  5.    * 页面的初始数据
  6.    */
  7.   data: {
  8.    arr:[]
  9.   },

  10.   /**
  11.    * 生命周期函数--监听页面加载
  12.    */
  13.   onLoad: function (options) {

  14.   },

  15.   /**
  16.    * 生命周期函数--监听页面初次渲染完成
  17.    */
  18.   onReady: function () {

  19.   },

  20.   /**
  21.    * 生命周期函数--监听页面显示
  22.    */
  23.   onShow: function () {

  24.   },

  25.   /**
  26.    * 生命周期函数--监听页面隐藏
  27.    */
  28.   onHide: function () {

  29.   },

  30.   /**
  31.    * 生命周期函数--监听页面卸载
  32.    */
  33.   onUnload: function () {

  34.   },

  35.   /**
  36.    * 页面相关事件处理函数--监听用户下拉动作
  37.    */
  38.   onPullDownRefresh: function () {

  39.   },

  40.   /**
  41.    * 页面上拉触底事件的处理函数
  42.    */
  43.   onReachBottom: function () {

  44.   },

  45.   /**
  46.    * 用户点击右上角分享
  47.    */
  48.   onShareAppMessage: function () {

  49.   },
  50.   img:function(e){
  51.     var that =this
  52.     wx.chooseImage({
  53.       success: function(res) {
  54.         console.log(res.tempFilePaths)
  55.         var img_arr = res.tempFilePaths
  56.         that.canvas_show(0,img_arr)
  57.       }
  58.     })
  59.   },
  60.   canvas_show: function (index, img_arr){
  61.     var that = this;
  62.    wx.showLoading({
  63.      title: '加载中',
  64.      icon:'loading'
  65.    })
  66.     if (index < img_arr.length){
  67.       wx.getImageInfo({
  68.         src: img_arr[index],
  69.         success: function (res) {
  70.           console.log("获取的图片结果", res)
  71.           var originHeight = res.height,
  72.             originWidth = res.width;
  73.           var maxWidth = 800,
  74.             maxHeight = 400;
  75.           // 目标尺寸
  76.           var targetWidth = originWidth,
  77.             targetHeight = originHeight;
  78.           //等比例压缩,如果宽度大于高度,则宽度优先,否则高度优先
  79.           if (originWidth > maxWidth || originHeight > maxHeight) {
  80.             if (originWidth / originHeight > maxWidth / maxHeight) {
  81.               // 要求宽度*(原生图片比例)=新图片尺寸
  82.               targetWidth = maxWidth;
  83.               targetHeight = Math.round(maxWidth * (originHeight / originWidth));
  84.             } else {
  85.               targetHeight = maxHeight;
  86.               targetWidth = Math.round(maxHeight * (originWidth / originHeight));
  87.             }
  88.           }
  89.           that.setData({
  90.             width: targetWidth,
  91.             height: targetHeight
  92.           },()=>{
  93.             setTimeout(()=>{
  94.               ctx.drawImage(img_arr[index], 0, 0, res.width, res.height, 0, 0, targetWidth, targetHeight);
  95.               ctx.draw(false, function (res) {
  96.                 console.log(res)
  97.                 //输出图片文件
  98.                 new Promise((resolve, reject) => {
  99.                   wx.canvasToTempFilePath({
  100.                     canvasId: 'myCancas',
  101.                     success: function (res) {
  102.                       // console.log("生成图片的结果", res.tempFilePath)
  103.                       resolve(res.tempFilePath)
  104.                     }
  105.                   }, this)
  106.                 }).then(res => {
  107.                   console.log("生成图片的结果", res)
  108.                   that.setData({
  109.                     arr: that.data.arr.concat(res)
  110.                   })
  111.                   
  112.                   index = index + 1;
  113.                   that.canvas_show(index, img_arr)
  114.                   wx.hideLoading()
  115.                 })
  116.               })

  117.             },1000)
  118.            
  119.           })
  120.         }
  121.       })
  122.      
  123.     }


  124.   }
  125. })

阅读(1401) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~