canvas 等比例压缩图片

复制代码
// 图片压缩,默认同比例压缩
compressImg (URL, callback) {
  // 生成canvas
  let canvas = document.createElement('canvas')
  let ctx = canvas.getContext('2d')
  let image = new Image()
  image.setAttribute('crossOrigin', 'anonymous')
  image.src = URL
  let quality = 0.3
  image.onload = () => {
    canvas.width = image.width
    canvas.height = image.height
    ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
    let newImageData = canvas.toDataURL('image/jpeg', quality)
    callback(newImageData)
  }
}
复制代码

 

posted on   明启心动  阅读(1714)  评论(0编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示