降低画质:
list () { // 降低画质测试
return this.imageList.map(item => {
if (item.link) return item
item.link = ''
const vm = this
let oImg = new Image()
oImg.setAttribute('crossOrigin', 'anonymous')
oImg.onload = function () {
// 等比例缩放scalingImage(原图宽度,原图高度, 希望最大宽度, 希望最大高度)
const {width, height} = vm.scalingImage(this.width, this.height, 300, 800)
let canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
let ctx = canvas.getContext('2d')
ctx.drawImage(oImg, 0, 0, width, height)
const src = canvas.toDataURL('image/png', 1) // 降低画质0 ~ 1 可选
item.link = src
}
oImg.src = item.url
return item
})
}
等比例缩放宽高尺寸:*此计算函数 来源于网络
scalingImage (原图宽度,原图高度, 希望最大宽度, 希望最大高度)
scalingImage(imgWidth, imgHeight, containerWidth, containerHeight) {
var containerRatio = containerWidth / containerHeight
var imgRatio = imgWidth / imgHeight
if (imgRatio > containerRatio) {
imgWidth = containerWidth
imgHeight = containerWidth / imgRatio
} else if (imgRatio < containerRatio) {
imgHeight = containerHeight
imgWidth = containerHeight * imgRatio
} else {
imgWidth = containerWidth
imgHeight = containerHeight
}
return { width: imgWidth, height: imgHeight }
}