如何在 Konva.js 中创建碰撞
How to create collisions in Konva.js
有人知道如何使用 Konva.js 在 Vue.js 中创建图层碰撞吗?例如我有一个正方形,中间是一个小点。我想要的是在正方形内阻挡点。我们可以将 dto 拖来拖去,但我们不能将它拖出正方形。如果能得到帮助,我将不胜感激 :D
var vm = this;
const stage = vm.$refs.stage.getStage();
var layer = new Konva.Layer();
var group = new Konva.Group({
x: 100,
y: 100,
draggable: false
});
var text = new Konva.Text({
x: 10,
y: 10,
fontFamily: "Calibri",
fontSize: 24,
text: "",
fill: "black"
});
var rect = new Konva.Rect({
clearBeforeDraw: true,
x: 50,
y: 50,
width: 100,
height: 50,
fill: "green",
stroke: "black"
});
var circle = new Konva.Circle({
clearBeforeDraw: true,
x: this.x,
y: this.y,
radius: 10,
fill: "red",
stroke: "black",
strokeWidth: 4,
containment: rect,
draggable: true,
name: "fillShape"
});
circle.on("dragmove", function() {
that.x = this.getX();
that.y = this.getY();
});
group.add(rect, circle);
layer.add(group);
stage.add(layer);
在圆形对象中添加dragBoundFunc并限制矩形的绝对宽度和高度
var circle = new Konva.Circle({
clearBeforeDraw: true,
x: 75,
y: 75,
radius: 10,
fill: "red",
stroke: "black",
strokeWidth: 4,
containment: rect,
draggable: true,
name: "fillShape",
dragBoundFunc: function(pos) {
const x = Math.min(250-12, Math.max(pos.x, 150+12));
const y = Math.min(200-12, Math.max(pos.y, 150+12));
return {x, y};
}
});
有人知道如何使用 Konva.js 在 Vue.js 中创建图层碰撞吗?例如我有一个正方形,中间是一个小点。我想要的是在正方形内阻挡点。我们可以将 dto 拖来拖去,但我们不能将它拖出正方形。如果能得到帮助,我将不胜感激 :D
var vm = this;
const stage = vm.$refs.stage.getStage();
var layer = new Konva.Layer();
var group = new Konva.Group({
x: 100,
y: 100,
draggable: false
});
var text = new Konva.Text({
x: 10,
y: 10,
fontFamily: "Calibri",
fontSize: 24,
text: "",
fill: "black"
});
var rect = new Konva.Rect({
clearBeforeDraw: true,
x: 50,
y: 50,
width: 100,
height: 50,
fill: "green",
stroke: "black"
});
var circle = new Konva.Circle({
clearBeforeDraw: true,
x: this.x,
y: this.y,
radius: 10,
fill: "red",
stroke: "black",
strokeWidth: 4,
containment: rect,
draggable: true,
name: "fillShape"
});
circle.on("dragmove", function() {
that.x = this.getX();
that.y = this.getY();
});
group.add(rect, circle);
layer.add(group);
stage.add(layer);
在圆形对象中添加dragBoundFunc并限制矩形的绝对宽度和高度
var circle = new Konva.Circle({
clearBeforeDraw: true,
x: 75,
y: 75,
radius: 10,
fill: "red",
stroke: "black",
strokeWidth: 4,
containment: rect,
draggable: true,
name: "fillShape",
dragBoundFunc: function(pos) {
const x = Math.min(250-12, Math.max(pos.x, 150+12));
const y = Math.min(200-12, Math.max(pos.y, 150+12));
return {x, y};
}
});