Rotate image with javascript

后端 未结 9 629
温柔的废话
温柔的废话 2020-11-27 04:17

I need to rotate an image with javascript in 90-degree intervals. I have tried a few libraries like jQuery rotate and Raphaël, but they have the same problem - The image is

9条回答
  •  有刺的猬
    2020-11-27 04:49

    Based on Anuga answer I have extended it to multiple images.

    Keep track of the rotation angle of the image as an attribute of the image.

    function rotate(image) {
      let rotateAngle = Number(image.getAttribute("rotangle")) + 90;
      image.setAttribute("style", "transform: rotate(" + rotateAngle + "deg)");
      image.setAttribute("rotangle", "" + rotateAngle);
    }
    .rotater {
      transition: all 0.3s ease;
      border: 0.0625em solid black;
      border-radius: 3.75em;
    }
    
    
    

    Edit

    Removed the modulo, looks strange.

提交回复
热议问题