Skip to content

Commit cedfbdf

Browse files
committed
图像绘制
1 parent 1ce0ad5 commit cedfbdf

5 files changed

Lines changed: 121 additions & 14 deletions

File tree

src/App.vue

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1+
<template>
2+
<Navbar />
3+
<div id="content">
4+
<div id="editor" @click="graphData.push({ fn: 'sin(x)' })">{{ graphData }}</div>
5+
<Graph :graphData="graphData" />
6+
</div>
7+
</template>
8+
19
<script setup lang="ts">
210
import Navbar from "./components/nav.vue";
3-
import Editor from "./components/editor.vue";
411
import Graph from "./components/graph.vue";
5-
</script>
12+
import type { FunctionPlotDatum } from "function-plot";
613
7-
<template>
8-
<Navbar />
9-
<Editor />
10-
<Graph />
11-
</template>
14+
import { reactive, ref, watch } from "vue";
15+
const graphData = ref([{ fn: 'x^2' }]);
16+
const cnt = ref(0);
17+
watch(graphData, () => cnt.value++,{deep:true});
18+
</script>
1219

1320
<style>
21+
#app {
22+
position: fixed;
23+
margin: 0;
24+
padding: 0;
25+
top: 0;
26+
bottom: 0;
27+
left: 0;
28+
right: 0;
29+
display: flex;
30+
flex-wrap: nowrap;
31+
flex-direction: column;
32+
}
33+
#content {
34+
width: 100vw;
35+
flex-grow: 1;
36+
display: flex;
37+
}
38+
#navbar {
39+
height: 50px;
40+
width: 100vw;
41+
background: #fff3;
42+
position: relative;
43+
}
1444
45+
#editor {
46+
width: 40vw;
47+
}
48+
#graph {
49+
width: 60vw;
50+
position: relative;
51+
}
1552
</style>

src/components/editor.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
<div id="editor"></div>
33
</template>
44
<style>
5-
#editor {
6-
height: 80px;
7-
}
85
</style>

src/components/graph.vue

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
11
<template>
2-
<div id="graph"></div>
3-
</template>
2+
<div id="graph" ref="shellRef">
3+
{{ width }},{{ height }},{{ cnt }}
4+
<div id="graphRender" ref="plotRef"></div>
5+
</div>
6+
</template>
7+
8+
<script setup lang="ts">
9+
import { onMounted, onUnmounted, ref, watch, watchEffect } from "vue";
10+
import functionPlot from "function-plot";
11+
import type { FunctionPlotDatum } from "function-plot";
12+
const { graphData } = defineProps<{ graphData: FunctionPlotDatum[] }>();
13+
const shellRef = ref<HTMLDivElement | null>(null);
14+
const plotRef = ref<HTMLDivElement | null>(null);
15+
const width = ref(0),
16+
height = ref(0);
17+
const cnt = ref(0);
18+
const handleResize = () => {
19+
if (shellRef.value) {
20+
width.value = shellRef.value.clientWidth;
21+
height.value = shellRef.value.clientHeight;
22+
}
23+
};
24+
onMounted(() => {
25+
handleResize();
26+
window.addEventListener("resize", handleResize);
27+
watch(
28+
[width, height, () => graphData],
29+
() => {
30+
cnt.value++
31+
if (plotRef.value && width.value && height.value) {
32+
functionPlot({
33+
data: JSON.parse(JSON.stringify(graphData)),
34+
target: plotRef.value,
35+
width: width.value,
36+
height: height.value,
37+
});
38+
cnt.value++;
39+
}
40+
},
41+
{ immediate: true, deep: true }
42+
);
43+
});
44+
onUnmounted(() => window.removeEventListener("resize", handleResize));
45+
watch(
46+
() => graphData,
47+
() => cnt.value++
48+
);
49+
</script>
50+
51+
<style>
52+
#graphRender {
53+
position: absolute;
54+
top: 0;
55+
right: 0;
56+
left: 0;
57+
bottom: 0;
58+
}
59+
</style>

src/components/nav.vue

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
<template>
2-
<div id="navbar"></div>
3-
</template>
2+
<div id="navbar">
3+
<span class="nav-title">function-plot-GUI</span>
4+
</div>
5+
</template>
6+
7+
<style>
8+
.nav-title {
9+
display: block;
10+
position: absolute;
11+
top: 0;
12+
bottom: 0;
13+
left: 15px;
14+
height: fit-content;
15+
text-align: left;
16+
margin: auto;
17+
font-size: 20px;
18+
font-weight: bold;
19+
}
20+
</style>

src/public.css

Whitespace-only changes.

0 commit comments

Comments
 (0)