HTML5 Canvas 形状调整大小吸附

在某些应用中,您可能希望在某些值附近吸附旋转。吸附使形状在提供的值附近变得“粘性”,并像四舍五入一样工作。
您可以通过 anchorDragBoundFunc 方法控制锚点位置的行为。

transformer.anchorDragBoundFunc(function (oldAbsPos, newAbsPos, event) {
// 限制 x 轴上的任何其他位置
return {
x: 0,
y: newAbsolutePosition.y,
};
});

在演示中,我们将尝试为调整大小实现吸附。有很多可能的实现方式。

说明:尝试调整形状的大小。您将看到变换器如何试图吸附到引导线。

Konva 形状变换和选择演示view raw
<!DOCTYPE html>
<html>
<head>
<!-- USE DEVELOPMENT VERSION -->
<script src="https://unpkg.com/konva@9.3.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Resize Snap Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>

<body>
<div id="container"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;

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

const layer = new Konva.Layer();
stage.add(layer);

const xSnaps = Math.round(stage.width() / 100);
const ySnaps = Math.round(stage.height() / 100);
const cellWidth = stage.width() / xSnaps;
const cellHeight = stage.height() / ySnaps;

for (var i = 0; i < xSnaps; i++) {
layer.add(
new Konva.Line({
x: i * cellWidth,
points: [0, 0, 0, stage.height()],
stroke: 'rgba(0, 0, 0, 0.2)',
strokeWidth: 1,
})
);
}

for (var i = 0; i < ySnaps; i++) {
layer.add(
new Konva.Line({
y: i * cellHeight,
points: [0, 0, stage.width(), 0],
stroke: 'rgba(0, 0, 0, 0.2)',
strokeWidth: 1,
})
);
}

const rect = new Konva.Rect({
x: 90,
y: 90,
width: 100,
height: 100,
fill: 'red',
draggable: true,
});
layer.add(rect);

const tr = new Konva.Transformer({
nodes: [rect],
anchorDragBoundFunc: function (oldPos, newPos, event) {
// oldPos - is old absolute position of the anchor
// newPos - is a new (possible) absolute position of the anchor based on pointer position
// it is possible that anchor will have a different absolute position after this function
// because every anchor has its own limits on position, based on resizing logic

// do not snap rotating point
if (tr.getActiveAnchor() === 'rotater') {
return newPos;
}

const dist = Math.sqrt(
Math.pow(newPos.x - oldPos.x, 2) + Math.pow(newPos.y - oldPos.y, 2)
);

// do not do any snapping with new absolute position (pointer position)
// is too far away from old position
if (dist > 10) {
return newPos;
}

const closestX = Math.round(newPos.x / cellWidth) * cellWidth;
const diffX = Math.abs(newPos.x - closestX);

const closestY = Math.round(newPos.y / cellHeight) * cellHeight;
const diffY = Math.abs(newPos.y - closestY);

const snappedX = diffX < 10;
const snappedY = diffY < 10;

// a bit different snap strategies based on snap direction
// we need to reuse old position for better UX
if (snappedX && !snappedY) {
return {
x: closestX,
y: oldPos.y,
};
} else if (snappedY && !snappedX) {
return {
x: oldPos.x,
y: closestY,
};
} else if (snappedX && snappedY) {
return {
x: closestX,
y: closestY,
};
}
return newPos;
},
});
layer.add(tr);
</script>
</body>
</html>