gpt4 book ai didi

javascript - 如何使用 KonvaJS 将单击事件从上层的形状传播到下层的图像?

转载 作者:行者123 更新时间:2023-11-29 10:34:59 24 4
gpt4 key购买 nike

Disclaimer: it may be considered this post is a duplicate of this post but I have demonstrated my need specifically.

我的 KonvaJS 中有一个案例我需要将点击事件从矩形(Upper Layer 的子项)传播到添加到Lower Layer 的多个图像的应用程序。 p>

请注意,我在 Lower layer 中有超过 50 个图像和形状之间的对象,所以我现在如何才能知道 Lower Layer 中的目标对象是什么。

请举个例子来说明我的需要:

 var width = window.innerWidth;
var height = window.innerHeight;

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

var lowerLayer = new Konva.Layer();
var upperLayer = new Konva.Layer();

//lion
var lionImage = new Image();
lionImage.onload = function() {

var lion = new Konva.Image({
x: 50,
y: 50,
image: lionImage,
width: 106,
height: 118
});

// add the shape to the layer
lowerLayer.add(lion);
stage.draw();

lion.on("click", function() {
alert("you clicked the lion");
});

};
lionImage.src = 'http://konvajs.github.io/assets/lion.png';

//monkey
var monkeyImage = new Image();
monkeyImage.onload = function() {

var monkey = new Konva.Image({
x: 200,
y: 50,
image: monkeyImage,
width: 106,
height: 118
});

// add the shape to the layer
lowerLayer.add(monkey);
stage.draw();

monkey.on("click", function() {
alert("you clicked the monkey");
});
};
monkeyImage.src = 'http://konvajs.github.io/assets/monkey.png';

var upperTransparentBox = new Konva.Rect({
x: 0,
y: 0,
height: stage.height(),
width: stage.width(),
fill: 'transparent',
draggable: false,
name: 'upperTransparentBox'
});
upperTransparentBox.on("click", function() {
alert("you clicked the upper Transparent Box");
});
upperLayer.add(upperTransparentBox);

// add the layer to the stage
stage.add(lowerLayer);
stage.add(upperLayer);
<!DOCTYPE html>
<html>

<head>
<script src="https://cdn.rawgit.com/konvajs/konva/1.0.2/konva.min.js"></script>
<meta charset="utf-8">
<title>Konva Image Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #F0F0F0;
}
</style>
</head>

<body>
<div id="container"></div>
</body>

</html>

最佳答案

从技术上讲,可以在任何节点上手动触发 click 事件。但我认为这是一种反模式。您可以使用“getIntersection()”函数找到一个交集,然后使用节点执行您需要的操作。

 var width = window.innerWidth;
var height = window.innerHeight;

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

var lowerLayer = new Konva.Layer();
var upperLayer = new Konva.Layer();

//lion
var lionImage = new Image();
lionImage.onload = function() {

var lion = new Konva.Image({
x: 50,
y: 50,
name: 'lion',
image: lionImage,
width: 106,
height: 118
});

// add the shape to the layer
lowerLayer.add(lion);
stage.draw();

lion.on("click", function() {
alert("you clicked the lion");
});

};
lionImage.src = 'http://konvajs.github.io/assets/lion.png';

//monkey
var monkeyImage = new Image();
monkeyImage.onload = function() {

var monkey = new Konva.Image({
x: 200,
y: 50,
name: 'monkey',
image: monkeyImage,
width: 106,
height: 118
});

// add the shape to the layer
lowerLayer.add(monkey);
stage.draw();

monkey.on("click", function() {
alert("you clicked the monkey");
});
};
monkeyImage.src = 'http://konvajs.github.io/assets/monkey.png';

var upperTransparentBox = new Konva.Rect({
x: 0,
y: 0,
height: stage.height(),
width: stage.width(),
fill: 'transparent',
draggable: false,
name: 'upperTransparentBox'
});
upperTransparentBox.on("click", function() {
var target = lowerLayer.getIntersection(stage.getPointerPosition());
if (target) {
alert('clicked on ' + target.name());
}
});
upperLayer.add(upperTransparentBox);

// add the layer to the stage
stage.add(lowerLayer);
stage.add(upperLayer);
<!DOCTYPE html>
<html>

<head>
<script src="https://cdn.rawgit.com/konvajs/konva/1.0.2/konva.min.js"></script>
<meta charset="utf-8">
<title>Konva Image Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #F0F0F0;
}
</style>
</head>

<body>
<div id="container"></div>
</body>

</html>

关于javascript - 如何使用 KonvaJS 将单击事件从上层的形状传播到下层的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com