前端——Base64处理

获取本地的图片处理成base64

getBase64(url, callback){
      var canvas = document.createElement("canvas");   //创建canvas DOM元素,并设置其宽高和图片一样
      var ctx = canvas.getContext("2d");
      var img = new Image;
      img.crossOrigin = 'Anonymous';
      img.src = url;
      console.log(url)
      img.onload = function () {
          canvas.height = 60; //指定画板的高度,自定义
          canvas.width = 60; //指定画板的宽度,自定义
          ctx.drawImage(img, 0, 0, 60, 60); //参数可自定义
          var dataURL = canvas.toDataURL();
          callback.call(this, dataURL); //回掉函数获取Base64编码
          canvas = null;
      };
    },

调用:

shareGame(){
      this.getBase64('../assets/logo.png',function (base64){
        console.log(base64)
          let image = base64,
      })
    },

你可能感兴趣的:(前端javascript)