我想创建一个Map,用户应该使用触摸事件滚动它,使用触摸手势放大和缩小。 这里使用touchstart将拖动标志设置为true,然后我使用touchmove计算坐标中的delta并相应地移动图层(地图图层),最后我使用touchend将拖动标志设置为false。
问题是它无法正常工作,到目前为止,几个小时的敲打不能让它发挥作用。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<link rel="stylesheet" href="core/jquery.mobile-1.4.2.css" />
<script type="text/javascript" src="core/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="core/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript" src="javascript/map.js"></script>
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id="main" data-role="page">
<div data-role="header">
<h1>Header of #main</h1>
</div>
<div id="content" data-role="content">
<script type="text/javascript" src="core/kinetic-v5.0.1.min.js"> </script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'content',
width: 300,
height: 200
});
var layer = new Kinetic.Layer();
var imageObj = new Image();
imageObj.onload = function() {
var yoda = new Kinetic.Image({
x: 0,
y: 0,
image: imageObj,
width: 106,
height: 118
});
layer.add(yoda);
stage.add(layer);
};
imageObj.src = 'image/map-04.png'
var dragging = false,
lastX = 0,
lastY = 0;
imageObj.on('touchstart', function() {
var touchPos = stage.getPointerPosition();
dragging = true;
lastX = touchPos.x;
lastY= touchPos.y;
});
imageObj.on('touchmove', function() {
var touchPos = stage.getPointerPosition();
if(dragging){
var dx = touchPos.x - lastX;
var dy = touchPos.y - lastY;
layer.translate(dx,dy);
lastX = touchPos.x;
lastY = touchPos.y;
}
});
imageObj.on('touchend', function() {
dragging = false;
});
stage.add(layer);
</script>
</div>
<div data-role="footer">
<h4>Footer of #main Page</h4>
</div>
</div>
</body>
</html>
提前感谢您的帮助。
答案 0 :(得分:1)
你的imgObject不是kineticjs图像,yoda是kineticjs图像,这里是一个有一些变化的修复:
http://jsbin.com/miqoxese/1/edit
<script type="text/javascript" src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.1.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'content',
width: 300,
height: 200
});
var layer = new Kinetic.Layer();
var imageObj = new Image();
imageObj.onload = function() {
var yoda = new Kinetic.Image({
x: 0,
y: 0,
image: imageObj,
width: 106,
height: 118,
draggable: true
});
yoda.on('dragstart', function(){
console.log("dragging"); // see console for result
});
layer.add(yoda);
stage.add(layer);
};
imageObj.src = 'http://www.clker.com/cliparts/8/9/9/d/11949855741697952186small_house_01.svg.med.png';
// stage.add(layer); // no need to add again
</script>
现在你需要将你的touchstart touchend等事件附加到yoda,而不是imgObject,它是DOM的一部分,而不是画布。