如何从当前位置翻转画布

时间:2017-10-01 18:00:15

标签: jquery css canvas rotation flip

我正在尝试向画布添加翻转和旋转选项但是在旋转之后,如果我翻转,它会从页面加载位置而不是当前旋转位置翻转。

这是我用于翻转和旋转的jQuery代码:

    var k = 0;
            $('#flip_image_btn').click(function(){
                var j = $('.dithered-image');
                k += 180;
                $('.dithered-image').css({
                    "transform":"rotatey(" + k + "deg)",
                    "transitionDuration":"0.5s"
                });

            });


            var rotation = 0;
            $("#rotate_image_clockwise_btn").click(function(){ 
                rotation = rotation+18;                 
                $('.dithered-image').css("transform", "rotate("+rotation+"deg)");
            });
            $("#rotate_image_anticlockwise_btn").click(function(){ 
                rotation = rotation-18;                 
                $('.dithered-image').css("transform", "rotate("+rotation+"deg)");
            });

有没有办法翻转图像,而不是kt返回其加载位置然后翻转?

这里有一个显示正在发生的事情的小提琴 https://jsfiddle.net/muugzans/1/

提前致谢

1 个答案:

答案 0 :(得分:0)

你需要合并它们,你正在做的是覆盖转换另一个转换

window.onload = function() {
  /*var c = document.getElementById("myCanvas");
  var ctx = c("2d");

  ctx.drawImage(img, 10, 10);*/
}
var k = 0;
var rotation = 0;
$('#flip_image_btn').click(function() {
  var j = $('.dithered-image');
  k += 180;
  $('.dithered-image').css({
    "transform": "rotate(" + rotation + "deg) rotatey(" + k + "deg)",
    "transitionDuration": "0.5s"
  });

});



$("#rotate_image_clockwise_btn").click(function() {
  rotation = rotation + 18;
  $('.dithered-image').css("transform", "rotate(" + rotation + "deg) rotatey(" + k + "deg)");
});
$("#rotate_image_anticlockwise_btn").click(function() {
  rotation = rotation - 18;
  $('.dithered-image').css("transform", "rotate(" + rotation + "deg) rotatey(" + k + "deg)");
});
.dithered-image {
  max-width: 550px;
  max-height: 350px;
  width: 100%;
  top: 130px;
  left: 150px;
}

.flipped {
  -moz-transform: scaleX(-1);
  -o-transform: scaleX(-1);
  -webkit-transform: scaleX(-1);
  transform: scaleX(-1);
  filter: FlipH;
  -ms-filter: "FlipH";
}

#myCanvas {
  width: 150px;
  height: auto;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<input type="button" class="button" value="Flip Image" id="flip_image_btn" />
<button style="font-size:24px" id="rotate_image_clockwise_btn">rotate</button>

<img class="dithered-image" id="myCanvas" src="https://cdn.pixabay.com/photo/2016/09/01/10/23/image-1635747_1280.jpg" width="100" />