Skip to content

Commit 37f022f

Browse files
authored
Support Network Image (#13)
1 parent 89908af commit 37f022f

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

vue-playground/src/App.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
VRoundRect,
2929
VLine,
3030
VPoints,
31+
VImage,
3132
}"
3233
@error="(e: any) => void 0"
3334
@input="input"
@@ -74,9 +75,9 @@
7475
<v-image
7576
:x="0"
7677
:y="0"
77-
:image="logo"
78-
:width="100"
79-
:height="100"
78+
:image="'https://raw.githubusercontent.com/rustq/vue-skia/main/vue-playground/src/assets/logo.png'"
79+
:width="70"
80+
:height="70"
8081
></v-image>
8182
</v-surface>
8283
</template>
@@ -105,7 +106,6 @@ import code from "./code";
105106
import LoadingCode from "./loading-code";
106107
import "vue-live/style.css";
107108
import "prism-themes/themes/prism-night-owl.css";
108-
import logo from "./assets/raster.png";
109109
export default defineComponent({
110110
name: "App",
111111
components: {
@@ -132,11 +132,11 @@ export default defineComponent({
132132
VRoundRect,
133133
VLine,
134134
VPoints,
135+
VImage,
135136
code,
136137
LoadingCode,
137138
debug: false,
138139
error: undefined,
139-
logo,
140140
};
141141
},
142142
mounted() {
-9.27 KB
Binary file not shown.

vue-playground/src/code.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export default `<v-surface :width="360" :height="360">
1515
[98, 90],
1616
[138, 10],
1717
]" :style="'fill'" :strokeWidth="1" :color="'rgba(200, 255, 0, 0.7)'" />
18+
<v-image
19+
:x="0"
20+
:y="0"
21+
:image="'https://raw.githubusercontent.com/rustq/vue-skia/main/vue-playground/src/assets/logo.png'"
22+
:width="70"
23+
:height="70"
24+
></v-image>
1825
<v-group :x="200" :y="160" color="violet" :style="'stroke'" :invertClip="true">
1926
<template #clip>
2027
<v-circle :cx="8" :cy="68" :r="40" />

vue-skia-framework/components/VImage.vue

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
<template>
2-
<v-sk-image :image="base64String" :x="x" :y="y" :width="width" :height="height" />
1+
<template >
2+
<v-sk-image v-if="loaded" :image="base64String" :x="x" :y="y" :width="width" :height="height" />
33
</template>
44

55
<script lang="ts">
6-
import { defineComponent, PropType } from "vue";
6+
import { defineComponent, getCurrentInstance, PropType } from "vue";
7+
import { ComponentInternalInstanceWithSoftSkiaWASM } from "../type";
78
89
export default defineComponent({
910
name: "VImage",
11+
data() {
12+
return {
13+
loaded: false,
14+
base64String: ""
15+
}
16+
},
1017
props: {
1118
image: {
1219
type: String as PropType<string>,
@@ -29,10 +36,36 @@ export default defineComponent({
2936
required: true,
3037
}
3138
},
32-
setup(props){
33-
const base64String = props.image.replace("data:image/png;base64,", "")
34-
return {
35-
base64String
39+
methods: {
40+
reUpdateRoot(vm: ComponentInternalInstanceWithSoftSkiaWASM) {
41+
const instance = vm;
42+
var parent = instance.parent as ComponentInternalInstanceWithSoftSkiaWASM;
43+
while (!("_ssw_id" in parent)) {
44+
parent = (parent as ComponentInternalInstanceWithSoftSkiaWASM).parent as ComponentInternalInstanceWithSoftSkiaWASM;
45+
}
46+
parent.update()
47+
}
48+
},
49+
mounted() {
50+
const vm = getCurrentInstance() as ComponentInternalInstanceWithSoftSkiaWASM;
51+
if (this.image?.startsWith("data:image/png;base64")) {
52+
const base64String = this.image.replace("data:image/png;base64,", "")
53+
this.base64String = base64String;
54+
this.loaded = true;
55+
this.reUpdateRoot(vm);
56+
} else {
57+
fetch(this.image)
58+
.then((res) => res.blob())
59+
.then((blob) => {
60+
const reader = new FileReader();
61+
reader.onloadend = () => {
62+
const base64String = (reader.result as string).replace("data:image/png;base64,", "");
63+
this.base64String = base64String;
64+
this.loaded = true;
65+
this.reUpdateRoot(vm);
66+
};
67+
reader.readAsDataURL(blob);
68+
});
3669
}
3770
}
3871
});

0 commit comments

Comments
 (0)