繁体   English   中英

在KineticJS中使用touchmove移动图像

[英]Moving an image using touchmove in KineticJS

我想使用“ touchmove”事件移动图像,我知道我可以将dragging设置为true并使用它,但是需要使用“ touchmove”,以便以后可以添加更多内容(Zoom,Rotate等)。

问题是:-1-没有响应,经过几次尝试它只是随机出现。 2-当图像大于50x50时,什么也没有发生。

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<style>
  body {
    margin: 0px;
    padding: 0px;
  }
</style>
</head>
<body onmousedown="return false;">
<div id="container"></div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.2.min.js" >    </script>
<script defer="defer">
  var stage = new Kinetic.Stage({
    container: 'container',
    width: 250,
    height: 400
  });
  var layer = new Kinetic.Layer();
  function writeMessage(message) {
    text.setText(message);
    layer.draw();
  }

  var text = new Kinetic.Text({
    x: 10,
    y: 10,
    fontFamily: 'Calibri',
    fontSize: 24,
    text: '',
    fill: 'black'
  });       

  image=new Image();
  image.onload=function(){
  var map = new Kinetic.Image({
      x: 0,
      y: 0,
      image: image,
      width: 50,
      height: 50
  });

  map.on('touchstart',function() {
    writeMessage('Touchstart');
  });

  map.on('touchmove',function() {
    var touchPos = stage.getPointerPosition();
    var x = touchPos.x;
    var y = touchPos.y;
    var pos= {x:x,y:y};
    map.move(pos);
    writeMessage('x: ' + x + ', y: ' + y);
  });

  layer.add(map);
  stage.add(layer);

  };
  image.src='img/map-04.png';

  layer.add(text);   
</script>
</body>
</html>

提前致谢。

如果使用move功能,则必须传递dx和dy,也不要传递绝对位置。 您可以使用以下方法:

map.on('touchmove', function() {
  var pos = stage.getPointerPosition();
  map.position(pos);
  writeMessage('x: ' + pos.x + ', y: ' + pos.y);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM