feat: 🚀 event-page 所有缩放完成

This commit is contained in:
bunny 2024-07-25 08:52:54 +08:00
parent a29fb2bffa
commit e857c5e310
4 changed files with 6243 additions and 7803 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
{"version":1721834336660} {"version":1721867671704}

View File

@ -1,18 +1,57 @@
<script setup lang="ts"> <script lang="ts" setup>
import { useWindowSize } from '@vueuse/core'; import { useWindowSize } from '@vueuse/core';
import { onMounted, ref } from 'vue'; import { onMounted, ref } from 'vue';
import Konva from 'konva/lib'; import Konva from 'konva/lib';
import { Circle } from 'konva/lib/shapes/Circle'; import { Circle } from 'konva/lib/shapes/Circle';
import { Text } from 'konva/lib/shapes/Text'; import { Text } from 'konva/lib/shapes/Text';
import { Layer } from 'konva/lib/Layer'; import { Layer } from 'konva/lib/Layer';
import { Stage } from 'konva/lib/Stage';
const { width, height } = useWindowSize(); const { width, height } = useWindowSize();
const circle = ref<Circle>(); const circle = ref<Circle>();
const text = ref<Text>(); const text = ref<Text>();
const stage = ref<Stage>();
const layer = ref<Layer>(); const layer = ref<Layer>();
const SCALE_BY = 1.1;
/**
* * 缩放画布
*/
const zoomed = () => {
stage.value?.on('wheel', ev => {
if (!stage.value) return;
// XY
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,
};
//
const direction = ev.evt.deltaY > 0 ? -1 : 1;
//
const newScaleX = direction > 0 ? oldScaleX * SCALE_BY : oldScaleX / SCALE_BY;
const newScaleY = direction > 0 ? oldScaleY * SCALE_BY : oldScaleY / SCALE_BY;
//
stage.value!.scale({ x: newScaleX, y: newScaleY });
//
const newPosition = {
x: pointer!.x - mousePointTo.x * newScaleX,
y: pointer!.y - mousePointTo.y * newScaleY,
};
stage.value!.position(newPosition);
});
};
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 });
layer.value = new Konva.Layer({ draggable: true }); layer.value = new Konva.Layer({ draggable: true });
text.value = new Konva.Text({ text.value = new Konva.Text({
@ -24,8 +63,8 @@ const initial = () => {
layer.value.add(text.value); layer.value.add(text.value);
circle.value = new Konva.Circle({ circle.value = 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: '#adf', fill: '#adf',
stroke: 'black', stroke: 'black',
@ -42,7 +81,9 @@ const initial = () => {
}); });
layer.value.add(circle.value); layer.value.add(circle.value);
stage.add(layer.value); stage.value.add(layer.value);
zoomed();
}; };
/** /**
@ -68,10 +109,10 @@ onMounted(() => {
<template> <template>
<div class="container position-absolute start-0 top-6 z-1"> <div class="container position-absolute start-0 top-6 z-1">
<button type="button" class="btn btn-primary" @click="onStartListening">开始监听</button> <button class="btn btn-primary" type="button" @click="onStartListening">开始监听</button>
<button type="button" class="btn btn-danger" @click="onRemoveListening">移出监听</button> <button class="btn btn-danger" type="button" @click="onRemoveListening">移出监听</button>
</div> </div>
<div id="container"></div> <div id="container"></div>
</template> </template>
<style scoped lang="scss"></style> <style lang="scss" scoped></style>

View File

@ -1,24 +1,63 @@
<template> <template>
<div class="container"> <div class="container">
<button type="button" class="btn btn-primary" @click="onRemoveEvent">移出事件</button> <button class="btn btn-primary" type="button" @click="onRemoveEvent">移出事件</button>
</div> </div>
<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, ref } from 'vue'; import { onMounted, ref } from 'vue';
import Konva from 'konva/lib'; import Konva from 'konva/lib';
import { Text } from 'konva/lib/shapes/Text'; import { Text } from 'konva/lib/shapes/Text';
import { Circle } from 'konva/lib/shapes/Circle'; import { Circle } from 'konva/lib/shapes/Circle';
import { Stage } from 'konva/lib/Stage';
const { width, height } = useWindowSize(); const { width, height } = useWindowSize();
const circle = ref<Circle>(); const circle = ref<Circle>();
const text = ref<Text>(); const text = ref<Text>();
const count = ref(0); const count = ref(0);
const stage = ref<Stage>();
const SCALE_BY = 1.1;
/**
* * 缩放
*/
const zoomed = () => {
stage.value?.on('wheel', ev => {
if (!stage.value) {
return;
}
//
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,
};
//
const direction = ev.evt.deltaY > 0 ? -1 : 1;
//
const newScaleX = direction > 0 ? oldScaleX * SCALE_BY : oldScaleX / SCALE_BY;
const newScaleY = direction > 0 ? oldScaleY * SCALE_BY : oldScaleY / SCALE_BY;
stage.value?.scale({ x: newScaleX, y: newScaleY });
//
const newPosition = {
x: pointer!.x - mousePointTo.x * newScaleX,
y: pointer!.y - mousePointTo.y * newScaleY,
};
stage.value!.position(newPosition);
});
};
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({ draggable: true });
// //
@ -34,8 +73,8 @@ const initial = () => {
// //
circle.value = new Konva.Circle({ circle.value = new Konva.Circle({
x: stage.width() / 2, x: stage.value.width() / 2,
y: stage.height() / 2 + 10, y: stage.value.height() / 2 + 10,
radius: 70, radius: 70,
fill: 'green', fill: 'green',
stroke: 'black', stroke: 'black',
@ -51,7 +90,9 @@ const initial = () => {
count.value++; count.value++;
}); });
stage.add(layer); stage.value.add(layer);
zoomed();
}; };
// //