fix: 🧩 画布缩放
This commit is contained in:
parent
67c49a26a4
commit
4e3b595694
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"recommendations": ["Vue.volar"]
|
|
||||||
}
|
|
|
@ -1 +1 @@
|
||||||
{"version":1721355798048}
|
{"version":1721834336660}
|
|
@ -2,16 +2,55 @@
|
||||||
<div id="container"></div>
|
<div id="container"></div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script lang="ts" setup>
|
||||||
import { useWindowSize } from '@vueuse/core';
|
import { useWindowSize } from '@vueuse/core';
|
||||||
import { onMounted } from 'vue';
|
import { onMounted, ref } from 'vue';
|
||||||
import Konva from 'konva/lib';
|
import Konva from 'konva/lib';
|
||||||
|
import { Stage } from 'konva/lib/Stage';
|
||||||
|
|
||||||
const { width, height } = useWindowSize();
|
const { width, height } = useWindowSize();
|
||||||
|
const stage = ref<Stage>();
|
||||||
|
const SCALE_BY = 1.1;
|
||||||
|
/**
|
||||||
|
* 缩放画布
|
||||||
|
*/
|
||||||
|
const zoomed = () => {
|
||||||
|
// 平台鼠标缩放
|
||||||
|
stage.value!.on('wheel', e => {
|
||||||
|
if (!stage.value) return;
|
||||||
|
|
||||||
|
// 当前缩放比例,获取X或者是Y都一样
|
||||||
|
const oldScaleX = stage.value!.scaleX();
|
||||||
|
const oldScaleY = stage.value!.scaleY();
|
||||||
|
|
||||||
|
// 当前鼠标位置
|
||||||
|
const pointer = stage.value!.getPointerPosition();
|
||||||
|
const mousePointTo = {
|
||||||
|
x: (pointer!.x - stage.value!.x()) / oldScaleX,
|
||||||
|
y: (pointer!.y - stage.value!.y()) / oldScaleY,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 判断是向上滚动鼠标还是向下滚动鼠标,向下滚动为正。向上滚动为负
|
||||||
|
let direction = e.evt.deltaY > 0 ? -1 : 1;
|
||||||
|
if (e.evt.ctrlKey) direction = -direction;
|
||||||
|
|
||||||
|
// 新增缩放大小X轴
|
||||||
|
const newScaleX = direction > 0 ? oldScaleX * SCALE_BY : oldScaleX / SCALE_BY;
|
||||||
|
// 新的缩放大小Y轴
|
||||||
|
const newScaleY = direction > 0 ? oldScaleY * SCALE_BY : oldScaleY / SCALE_BY;
|
||||||
|
stage.value!.scale({ x: newScaleX, y: newScaleY });
|
||||||
|
|
||||||
|
const newPos = {
|
||||||
|
x: pointer!.x - mousePointTo.x * newScaleX,
|
||||||
|
y: pointer!.y - mousePointTo.y * newScaleY,
|
||||||
|
};
|
||||||
|
stage.value!.position(newPos);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const initial = () => {
|
const initial = () => {
|
||||||
const stage = new Konva.Stage({ container: 'container', width: width.value, height: height.value });
|
stage.value = new Konva.Stage({ container: 'container', width: width.value, height: height.value });
|
||||||
const layer = new Konva.Layer({ draggable: true });
|
const layer = new Konva.Layer();
|
||||||
|
|
||||||
// 创建文本
|
// 创建文本
|
||||||
const text = new Konva.Text({
|
const text = new Konva.Text({
|
||||||
|
@ -26,8 +65,8 @@ const initial = () => {
|
||||||
|
|
||||||
// 创建元素
|
// 创建元素
|
||||||
const circle = new Konva.Circle({
|
const circle = new Konva.Circle({
|
||||||
x: stage.width() / 2,
|
x: stage.value!.width() / 2,
|
||||||
y: stage.height() / 2,
|
y: stage.value!.height() / 2,
|
||||||
radius: 70,
|
radius: 70,
|
||||||
fill: 'red',
|
fill: 'red',
|
||||||
stroke: 'black',
|
stroke: 'black',
|
||||||
|
@ -43,7 +82,8 @@ const initial = () => {
|
||||||
text.text('鼠标离开');
|
text.text('鼠标离开');
|
||||||
});
|
});
|
||||||
|
|
||||||
stage.add(layer);
|
zoomed();
|
||||||
|
stage.value!.add(layer);
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
|
Loading…
Reference in New Issue