Skip to content

Commit 9b60583

Browse files
committed
优化图像长宽获取模式
1 parent 1b4c801 commit 9b60583

File tree

3 files changed

+14
-33
lines changed

3 files changed

+14
-33
lines changed

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { useWindowSize } from "@vueuse/core";
4343
const { width: windowWidth } = useWindowSize();
4444
const toSize = (percent: number) => (percent / 100) * windowWidth.value;
4545
46-
const drawerMinSize = computed(() => 450);
46+
const drawerMinSize = computed(() => 460);
4747
const drawerMaxSize = computed(() => toSize(75));
4848
4949
const restrictRange = (x: number) =>

src/graph/graph.vue

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ import { onMounted, onUnmounted, ref, watch, WatchHandle } from "vue";
3030
import { throttle } from "lodash-es";
3131
import type { FunctionPlotDatum } from "function-plot";
3232
import { getFnType } from "../consts";
33-
const { width, height } = defineProps<{
34-
width: number;
35-
height: number;
36-
}>();
3733
3834
import { useProfile } from "@/states";
3935
const profile = useProfile();
4036
4137
import emitter from "@/mitt";
4238
const fullUpdateState = defineModel<boolean>();
4339
44-
const plotRef = ref<HTMLDivElement | null>(null);
40+
const plotRef = ref<HTMLDivElement>();
4541
const errorMsg = ref<string | undefined>(undefined);
4642
4743
function findError(graphData: FunctionPlotDatum[]) {
@@ -54,6 +50,9 @@ function findError(graphData: FunctionPlotDatum[]) {
5450
return 0;
5551
}
5652
53+
import { useElementSize } from "@vueuse/core";
54+
const { width, height } = useElementSize(plotRef);
55+
5756
let unwatchHandler: WatchHandle | null = null;
5857
const emit = defineEmits(["require-post-update"]);
5958
import functionPlot from "function-plot";
@@ -71,8 +70,8 @@ onMounted(() => {
7170
functionPlot({
7271
target: plotRef.value,
7372
data: graphData,
74-
width: width - 20,
75-
height: height - 20,
73+
width: width.value,
74+
height: height.value,
7675
annotations: profile.getOriginalAnnotaion(),
7776
...profile.getOriginalOptions(),
7877
});
@@ -85,8 +84,8 @@ onMounted(() => {
8584
if (!fullUpdateState.value) emitter.emit("require-full-update", "error");
8685
errorMsg.value = (e as Error).message;
8786
}
88-
}, 200);
89-
unwatchHandler = watch([() => width, () => height, profile], handleUpdate, {
87+
}, 250);
88+
unwatchHandler = watch([width, height, profile], handleUpdate, {
9089
immediate: true,
9190
});
9291
});
@@ -99,12 +98,10 @@ onUnmounted(() => {
9998
<style>
10099
#graphRender {
101100
position: absolute;
102-
top: 0;
103-
right: 0;
104-
left: 0;
105-
bottom: 0;
106-
width: fit-content;
107-
height: fit-content;
101+
top: 10px;
102+
right: 10px;
103+
left: 10px;
104+
bottom: 10px;
108105
margin: auto;
109106
color: black;
110107
user-select: none;

src/graph/index.vue

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<template>
22
<div id="graph" ref="shellRef">
33
<Graph
4-
:width="graphWidth"
5-
:height="graphHeight"
64
:key="graphKey"
75
v-model="fullUpdateState"
86
@require-post-update="handlePostUpdate"
@@ -12,27 +10,13 @@
1210

1311
<script setup lang="ts">
1412
import Graph from "./graph.vue";
15-
import { onMounted, ref } from "vue";
16-
import { throttle } from "lodash-es";
13+
import { ref } from "vue";
1714
18-
const graphWidth = ref(0),
19-
graphHeight = ref(0);
2015
const graphKey = ref(0);
2116
const fullUpdateState = ref(false);
22-
2317
const shellRef = ref<HTMLDivElement>();
24-
onMounted(() => {
25-
const observer = new ResizeObserver(
26-
throttle(() => {
27-
graphWidth.value = shellRef.value!.clientWidth;
28-
graphHeight.value = shellRef.value!.clientHeight;
29-
}, 180)
30-
);
31-
observer.observe(shellRef.value!);
32-
});
3318
3419
import emitter from "@/mitt";
35-
3620
emitter.on("require-full-update", (str) => {
3721
fullUpdateState.value = true;
3822
graphKey.value++;

0 commit comments

Comments
 (0)