-->

Kineticjs: Rotate an Image with a two Finger Touch

2019-06-07 07:00发布

问题:

I want to use Kineticjs on a mobile device to rotate an image with a two finger rotation gesture.

Displaying an image is described here. Which is basically this:

<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.min.js"></script>
<script defer="defer">
  var stage = new Kinetic.Stage({
    container: 'container',
    width: 578,
    height: 200
  });
  var layer = new Kinetic.Layer();

  var imageObj = new Image();
  imageObj.onload = function() {
    var yoda = new Kinetic.Image({
      x: 200,
      y: 50,
      image: imageObj,
      width: 106,
      height: 118
    });

    // add the shape to the layer
    layer.add(yoda);

    // add the layer to the stage
    stage.add(layer);
  };
  imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/yoda.jpg';

</script>

Adding touch events can be done with

imageObj.on('touchmove', function() {
       // do something
});

How can I rotate an image with a two finger touch gesture around the center of the image?

回答1:

You can use HammerJS with KineticJS (from Kinetic v5.1.0) for gesture events:

  var startRotate = 0;
  var hammertime = Hammer(image);
  hammertime.on("transformstart", function(e) {
    startRotate = image.rotation();
  }).on("transform", function(e) {
    image.rotation(startRotate + e.gesture.rotation);
    layer.draw();
  });

Demo