canvas 旋轉圖片

// 橫屏寫的,堅屏展示,需旋轉270度

rotateBase64Img(src: any, callback: any) {
    const canvas = document.createElement('canvas')
      const ctx: any = canvas.getContext('2d')
      const image = new Image()
      image.crossOrigin = 'anonymous'
      image.src = src
      image.onload = function () {
        const max = image.height
        canvas.width = max
        canvas.height = max
        // ctx.fillStyle = "#999";
        // ctx.fillRect(0, 0, max, max);
        ctx.translate(max / 2, max / 2); // 移動中心點到正中心
        ctx.rotate(270 * Math.PI / 180);
        ctx.translate(-max / 2, -max / 2); // 移回到原處
        ctx.drawImage(image, max / 4, 0)
        const imgData = ctx.getImageData(0, max / 4, max, max / 4 * 3)
        canvas.height = max / 2
        ctx.clearRect(0, 0, max, max)
        ctx.putImageData(imgData, 0, 0)
        callback(canvas.toDataURL())
      }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章