js canvas 旋转90度的整数倍

原文链接: js canvas 旋转90度的整数倍

上一篇: js 使用canvas 旋转 图片

下一篇: dell g7 重装win10

为了避免出现黑框

up-02d3dcdf31303d61a1a9afb3cc4a23b367c.png

效果如下

up-022a22b70fad411fdce418016c48179d76e.png

up-eeacf342e5f336604fab5b1f8d0c0a87fe7.png

up-80cff6f19284f3f7edaf67aa396107b5a60.png

up-8fe96b30812ace021877a1a1eeb0a759659.png

根据不同的方向,设置宽高和画笔位置等

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <title>Title</title>
  <style>
    .img {
      width: 300px;
      border: 1px solid black;
    }
  </style>
</head>
<body>
<img class="img" id="source" src="./t.jpg" alt=""/>
<canvas class="img" id="rotationCanvas"></canvas>
<img class="img" id="target" src="./t.jpg" alt=""/>

<button onclick="rotation()">旋转</button>
<script>
  let degree = 0;
  const sourceImg = document.getElementById('source');
  const targetImg = document.getElementById('target');

  const rotationCanvas = document.getElementById('rotationCanvas');

  function getSize(url) {
    return new Promise((resolve) => {
      let img = document.createElement('img');
      img.onload = () => {
        resolve({
          width: img.width,
          height: img.height,
        });
      };
      img.src = url;
    });
  }

  async function rotation() {
    let d = (degree * Math.PI) / 180;
    console.log(d);
    let image = sourceImg;
    let size = await getSize(sourceImg.src);
    console.log('size', size);
    const canvasWidth = size.width;
    const canvasHeight = size.height;
    // 旋转方向
    let direction = (degree / 90) % 4
    let surfaceContext = rotationCanvas.getContext('2d');
    if (direction === 0) {
      rotationCanvas.width = canvasWidth;
      rotationCanvas.height = canvasHeight;
      surfaceContext.clearRect(0, 0,  rotationCanvas.width, rotationCanvas.height);
      surfaceContext.save();
      surfaceContext.drawImage(image, 0, 0);
      surfaceContext.restore();
    } else if (direction === 1) {
      rotationCanvas.width = canvasHeight;
      rotationCanvas.height = canvasWidth;
      surfaceContext.clearRect(0, 0, rotationCanvas.width, rotationCanvas.height);
      surfaceContext.save();
      surfaceContext.translate(rotationCanvas.width  * 0.5, rotationCanvas.height * 0.5);
      surfaceContext.rotate(d);
      surfaceContext.drawImage(image, -rotationCanvas.height / 2, -rotationCanvas.width  / 2);
      surfaceContext.restore();
    } else if (direction === 2) {
      rotationCanvas.width = canvasWidth;
      rotationCanvas.height = canvasHeight;
      surfaceContext.clearRect(0, 0, rotationCanvas.width, rotationCanvas.height);
      surfaceContext.save();
      surfaceContext.translate(rotationCanvas.width * 0.5, rotationCanvas.height * 0.5);
      surfaceContext.rotate(d);
      surfaceContext.drawImage(image, -rotationCanvas.width / 2, -rotationCanvas.height / 2);
      surfaceContext.restore();
    } else if (direction === 3) {
      rotationCanvas.width = canvasHeight;
      rotationCanvas.height = canvasWidth;
      surfaceContext.clearRect(0, 0, rotationCanvas.width, rotationCanvas.height);
      surfaceContext.save();
      surfaceContext.translate(rotationCanvas.width * 0.5, rotationCanvas.height * 0.5);
      surfaceContext.rotate(d);
      surfaceContext.drawImage(image, -rotationCanvas.height / 2, - rotationCanvas.width / 2);
      surfaceContext.restore();
    }

    // surfaceContext.scale(.5, .5)
    targetImg.src = rotationCanvas.toDataURL();

    degree += 90;
  }

  rotation();
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值