docs: 验证渲染系统可以嵌入文档

This commit is contained in:
AncTe committed 2026-10-29 20:26:02 +08:00
1 parent e05cc55b95
commit 3cc0fa9bf6
6 files changed
+340 -1

No files matched your search

@@ -0,0 +1,50 @@
<template>
<container :width="480" :height="320">
<g-rect
:x="40"
:y="40"
:width="160"
:height="90"
:fillStyle="color"
strokeStyle="#9dc0ff"
:lineWidth="2"
@click="onClick"
/>
<g-circle
:x="320"
:y="110"
:radius="48"
fillStyle="#e0a42f"
strokeStyle="#ffe3a3"
:lineWidth="2"
@click="onClick"
/>
<text
:x="40"
:y="200"
:text="'点击次数:' + count"
fillStyle="#e8f0ff"
/>
<text
:x="40"
:y="240"
:text="'点击颜色:' + color"
fillStyle="#8fb6ff"
/>
</container>
</template>
<script setup lang="ts">
import { ref } from 'vue';
/** 点击次数 */
const count = ref<number>(0);
/** 矩形当前的填充色,点击后轮换 */
const color = ref<string>('#2f6fed');
/** 点击图形时更新计数与颜色 */
function onClick(): void {
count.value++;
color.value = color.value === '#2f6fed' ? '#2fbf71' : '#2f6fed';
}
</script>
@@ -0,0 +1,120 @@
<template>
<container :width="480" :height="320">
<text :x="40" :y="24" text="打开 UI" fillStyle="#e8f0ff" />
<g-rect
:x="40"
:y="40"
:width="150"
:height="56"
fillStyle="#2f6fed"
strokeStyle="#9dc0ff"
:lineWidth="2"
@click="openUi"
/>
<text :x="240" :y="24" text="关闭 UI" fillStyle="#e8f0ff" />
<g-rect
:x="240"
:y="40"
:width="150"
:height="56"
fillStyle="#3a2f5f"
strokeStyle="#b09dff"
:lineWidth="2"
@click="closeUi"
/>
<text
:x="40"
:y="140"
:text="'栈长度:' + stackSize"
fillStyle="#8fb6ff"
/>
<text
:x="40"
:y="180"
:text="active ? '已打开' : '未打开'"
fillStyle="#8fb6ff"
/>
<UIRenderer />
</container>
</template>
<script setup lang="ts">
import { computed, defineComponent, h, onUnmounted } from 'vue';
import {
UIController,
GameUI,
IUIDefaultPropsBase,
IUIInstance
} from '@motajs/system';
const controller = new UIController<IUIDefaultPropsBase>();
controller.setPropsBaseCreator((instance, ctrl) => ({
instance,
controller: ctrl
}));
/** 当前打开的 UI 实例,打开与关闭必须复用同一个变量 */
let instance: IUIInstance | null = null;
/** 最小 UI 组件,用渲染函数构造,不依赖运行时模板编译器 */
const DemoPanel = defineComponent({
name: 'PocDemoPanel',
setup() {
return () =>
h('container', { x: 240, y: 140, width: 220, height: 150 }, [
h('g-rect', {
x: 0,
y: 0,
width: 220,
height: 150,
fillStyle: '#1d2b45',
strokeStyle: '#7aa2ff',
lineWidth: 2
}),
h('text', {
x: 16,
y: 40,
text: '这是一个最小 UI',
fillStyle: '#e8f0ff'
}),
h('text', {
x: 16,
y: 80,
text: '由 UIController 打开',
fillStyle: '#8fb6ff'
})
]);
}
});
const ui = new GameUI('poc-minimal-ui', DemoPanel);
/** 把 controller.render() 挂进同一棵渲染树 */
const UIRenderer = defineComponent({
name: 'PocUIRenderer',
setup() {
return () => controller.render();
}
});
const active = computed<boolean>(() => controller.active.value);
const stackSize = computed<number>(() => controller.stack.length);
/** 打开最小 UI,已打开时不重复入栈 */
function openUi(): void {
if (instance) return;
instance = controller.open(ui, {});
}
/** 关闭最小 UI,复用打开时保存的实例 */
function closeUi(): void {
if (instance) {
controller.close(instance);
instance = null;
}
}
onUnmounted(() => {
controller.closeAll();
});
</script>
+84
View File
@@ -0,0 +1,84 @@
<template>
<div :style="{ background: props.background, overflow: 'hidden' }">
<canvas ref="canvasRef"></canvas>
</div>
</template>
<script setup lang="ts">
import { Component, onMounted, onUnmounted, ref } from 'vue';
/** demo 模块的导出形态,只要求默认导出组件 */
interface DemoModule {
default: Component;
}
/** 动态导入一个 demo 组件的加载函数 */
type DemoLoader = () => Promise<DemoModule>;
/** 包装组件的外部参数 */
interface RenderDemoProps {
/** 动态导入 demo 组件的加载函数 */
loader: DemoLoader;
/** 逻辑宽度 */
width?: number;
/** 逻辑高度 */
height?: number;
/** 缩放比例 */
scale?: number;
/** 画布容器背景色 */
background?: string;
}
/** 可清理的 app 引用,仅声明清理所需的 unmount */
interface AppHandle {
unmount(): void;
}
const props = withDefaults(defineProps<RenderDemoProps>(), {
width: 480,
height: 320,
scale: 1,
background: '#101418'
});
const canvasRef = ref<HTMLCanvasElement | null>(null);
/** 组件是否已经卸载,用于拦截动态导入完成后的竞态 */
let disposed = false;
/** 渲染器的清理入口,在渲染器创建后、挂载 app 之前赋值 */
let dispose: (() => void) | null = null;
onMounted(async () => {
const { MotaRenderer } = await import('@motajs/render');
const { createRendererFor } = await import('@motajs/render-vue');
const demo = await props.loader();
if (disposed || !canvasRef.value) return;
const canvas = canvasRef.value;
const renderer = new MotaRenderer({
canvas,
width: props.width,
height: props.height
});
let appRef: AppHandle | null = null;
dispose = () => {
if (appRef) {
appRef.unmount();
}
renderer.destroy();
};
if (props.scale !== 1) {
renderer.setScale(props.scale);
}
const app = createRendererFor(renderer).createApp(demo.default);
appRef = app;
app.mount(renderer);
// 一个包装实例只服务一个 demo,loader 变更不触发重挂载
});
onUnmounted(() => {
disposed = true;
dispose?.();
dispose = null;
});
</script>