function stretchAndRotateImage(img, stretchWidthBeforeRotate, stretchHeightBeforeRotate, rotationAngle, showProcess = true) {
const stretchCanvas = document.createElement('canvas')
const ctx = stretchCanvas.getContext('2d')
stretchCanvas.width = stretchWidthBeforeRotate
stretchCanvas.height = stretchHeightBeforeRotate
ctx.drawImage(img, 0, 0, stretchWidthBeforeRotate, stretchHeightBeforeRotate)
if (showProcess) {
const div = document.createElement('div')
div.appendChild(document.createTextNode('拉伸'))
document.body.appendChild(div)
document.body.appendChild(stretchCanvas)
}
const radians = (rotationAngle * Math.PI) / 180
const sin = Math.sin(radians)
const cos = Math.cos(radians)
const width = Math.abs(stretchWidthBeforeRotate * cos) + Math.abs(stretchHeightBeforeRotate * sin)
const height = Math.abs(stretchWidthBeforeRotate * sin) + Math.abs(stretchHeightBeforeRotate * cos)
const rotatedCanvas = document.createElement('canvas')
const rotatedCtx = rotatedCanvas.getContext('2d')
rotatedCanvas.width = width
rotatedCanvas.height = height
rotatedCtx.translate(width / 2, height / 2)
rotatedCtx.rotate((rotationAngle * Math.PI) / 180)
rotatedCtx.drawImage(stretchCanvas, -stretchWidthBeforeRotate / 2, -stretchHeightBeforeRotate / 2)
if (showProcess) {
const div = document.createElement('div')
div.appendChild(document.createTextNode('旋转'))
document.body.appendChild(div)
document.body.appendChild(rotatedCanvas)
}
return rotatedCanvas
}
function loadImage(uri) {
return new Promise((resolve, reject) => {
const img = new Image()
img.crossOrigin = 'anonymous'
img.onload = () => resolve(img)
img.onerror = reject
img.src = uri
})
}
(async function () {
const img = await loadImage('https://static-yh.yonghui.cn/app/fe-store-job-mgnt/shelf-price-tag-image-process.png')
const canvas = stretchAndRotateImage(img, 500, 500, 30)
})()
canvas {
border: 1px solid red;
}
console