桌面/移动端事件支持

获取Konva最新的信息

我们可以使用 on() 方法给图形添加桌面/移动端事件。例如:我们可以使用 “mousedown touchstart” 的事件组合,同时在桌面端和移动端绑定mousedown事件,同理 mouseup 可以使用 “mouseup touchend” 事件组合,双击可以使用 “dblclick dbltap” 事件组合。

说明:观察 mousedown、mouseup、touchstart、touchend 事件在桌面端和移动端的执行情况。

Konva Desktop_and_Mobile Demoview raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Desktop and Mobile Events Support Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>

<body>
<div id="container"></div>
<script>
function writeMessage(message) {
text.text(message);
layer.draw();
}

var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight
});

var layer = new Konva.Layer();

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

var circle = new Konva.Circle({
x: stage.width() / 2,
y: stage.height() / 2 + 10,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4
});

/*
* mousedown and touchstart are desktop and
* mobile equivalents so they are often times
* used together
*/
circle.on('mousedown touchstart', function() {
writeMessage('Mousedown or touchstart');
});
/*
* mouseup and touchend are desktop and
* mobile equivalents so they are often times
* used together
*/
circle.on('mouseup touchend', function() {
writeMessage('Mouseup or touchend');
});

layer.add(circle);
layer.add(text);

// add the layer to the stage
stage.add(layer);
</script>
</body>
</html>