page: 📄 设置最大限度拖拽

This commit is contained in:
bunny 2024-07-15 16:59:01 +08:00
parent f4d93e2afe
commit 10ed9a8701
2 changed files with 47 additions and 2 deletions

View File

@ -1,6 +1,4 @@
```sh ```sh
transform events
resize limits
rotation snaps rotation snaps
resize snaps resize snaps
stop transform stop transform

View File

@ -0,0 +1,47 @@
<script setup lang="ts">
import { useWindowSize } from '@vueuse/core';
import { onMounted } from 'vue';
import Konva from 'konva/lib';
const { width, height } = useWindowSize();
const MAX_WIDTH = 200;
const initial = () => {
const stage = new Konva.Stage({ container: 'container', width: width.value, height: height.value });
const layer = new Konva.Layer();
stage.add(layer);
const rect = new Konva.Rect({
x: 160,
y: 60,
width: 100,
height: 90,
fill: 'red',
name: 'rect',
stroke: 'black',
draggable: true,
});
layer.add(rect);
const tr = new Konva.Transformer({
boundBoxFunc: function (oldBoundBox, newBoundBox) {
if (Math.abs(newBoundBox.width) > MAX_WIDTH) {
return oldBoundBox;
}
return newBoundBox;
},
});
layer.add(tr);
tr.nodes([rect]);
};
onMounted(() => {
initial();
});
</script>
<template>
<div id="container"></div>
</template>