From c6f1755a59c0dc91bcd78087f5d5e768de675699 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Thu, 26 Nov 2026 17:04:37 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E9=87=8D=E5=86=99=E6=96=87=E5=AD=97?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demos/ui/builtin-scrolltext-demo.tsx | 22 +- .../ui/builtin-scrolltext-methods-demo.tsx | 107 ++++ .../ui/builtin-scrolltext-style-demo.tsx | 67 ++ .../ui/builtin-textcontent-basic-demo.tsx | 28 + .../demos/ui/builtin-textcontent-demo.tsx | 37 +- .../ui/builtin-textcontent-height-demo.tsx | 59 ++ .../ui/builtin-textcontent-methods-demo.tsx | 84 +++ .../ui/builtin-textcontent-style-demo.tsx | 65 ++ .../demos/ui/text-demo-materials.ts | 60 ++ docs/render-guide/advanced/builtin-text.md | 604 ++++++++++++++---- 10 files changed, 980 insertions(+), 153 deletions(-) create mode 100644 docs/.vitepress/components/demos/ui/builtin-scrolltext-methods-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/builtin-scrolltext-style-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/builtin-textcontent-basic-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/builtin-textcontent-height-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/builtin-textcontent-methods-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/builtin-textcontent-style-demo.tsx create mode 100644 docs/.vitepress/components/demos/ui/text-demo-materials.ts diff --git a/docs/.vitepress/components/demos/ui/builtin-scrolltext-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-scrolltext-demo.tsx index e3d20ba..2dc953c 100644 --- a/docs/.vitepress/components/demos/ui/builtin-scrolltext-demo.tsx +++ b/docs/.vitepress/components/demos/ui/builtin-scrolltext-demo.tsx @@ -1,8 +1,16 @@ -import { defineComponent, onUnmounted, ref } from 'vue'; +import { defineComponent, markRaw, onUnmounted, ref } from 'vue'; import { ScrollText, ScrollTextExpose } from '../../../../../packages-user/client-base/src/components/misc'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#7ee787' + }) +); const NOTICE = [ '公告:今晚八点维护', @@ -13,13 +21,12 @@ const NOTICE = [ .join('\n') .repeat(3); -// 滚动文字:内容在固定视口里向上滚动,超出视口的部分不显示 +// 基本滚动:内容在固定视口里向上滚动,滚到底后停顿再从头开始 export default defineComponent(() => { const scroll = ref(); let timer: ReturnType | undefined; const restart = () => { - // 组件触发 scrollEnd 后会立刻暂停,等一小段可见停顿后再恢复 timer = setTimeout(() => { timer = undefined; scroll.value?.rescroll(); @@ -35,15 +42,16 @@ export default defineComponent(() => { - + + diff --git a/docs/.vitepress/components/demos/ui/builtin-scrolltext-methods-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-scrolltext-methods-demo.tsx new file mode 100644 index 0000000..ac27c21 --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-scrolltext-methods-demo.tsx @@ -0,0 +1,107 @@ +import { defineComponent, markRaw, ref } from 'vue'; +import { + ScrollText, + ScrollTextExpose +} from '../../../../../packages-user/client-base/src/components/misc'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#e8f0ff' + }) +); + +const NOTICE = [ + '暂停、继续与变速都通过组件实例控制。', + '改变速度只影响之后的滚动,不重算已经滚过的位置。', + '回到起点会调用 rescroll,然后再调用 resume。', + '滚到底时触发 onScrollEnd,组件会停在底部。' +] + .join('\n') + .repeat(3); + +// 实例方法:暂停、继续、变速与回到起点,全部调用真实组件方法 +export default defineComponent(() => { + const scroll = ref(); + const speed = ref(70); + const status = ref('滚动中'); + + const pause = () => { + scroll.value?.pause(); + status.value = '已暂停'; + }; + + const resume = () => { + scroll.value?.resume(); + status.value = '滚动中'; + }; + + const toggleSpeed = () => { + speed.value = speed.value === 70 ? 140 : 70; + scroll.value?.setSpeed(speed.value); + }; + + const backToStart = () => { + scroll.value?.rescroll(); + scroll.value?.resume(); + status.value = '滚动中'; + }; + + return () => ( + + + + + + + + (status.value = '已到底')} + /> + + + + + + + + + + + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/builtin-scrolltext-style-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-scrolltext-style-demo.tsx new file mode 100644 index 0000000..672df8b --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-scrolltext-style-demo.tsx @@ -0,0 +1,67 @@ +import { Font } from '@motajs/render'; +import { defineComponent, markRaw, onUnmounted, ref } from 'vue'; +import { + ScrollText, + ScrollTextExpose +} from '../../../../../packages-user/client-base/src/components/misc'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const TYPER = markRaw( + new TextContentTyper({ + font: new Font('Verdana', 18), + lineHeight: 10, + fillStyle: '#e8f0ff', + strokeStyle: '#58c4dd', + strokeWidth: 2 + }) +); + +const NOTICE = [ + '字体:Verdana 18px。', + '行距:lineHeight 为 10。', + '填充:浅色文字;描边:蓝色,线宽 2。', + '间距:pad 让文字先从视口下方进入。' +] + .join('\n') + .repeat(2); + +// 文本样式:typer 控制字体、行距、填充与描边,文字宽度与视口一致 +export default defineComponent(() => { + const scroll = ref(); + let timer: ReturnType | undefined; + + const restart = () => { + timer = setTimeout(() => { + timer = undefined; + scroll.value?.rescroll(); + scroll.value?.resume(); + }, 1200); + }; + + onUnmounted(() => { + if (timer !== undefined) clearTimeout(timer); + }); + + return () => ( + + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/builtin-textcontent-basic-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-textcontent-basic-demo.tsx new file mode 100644 index 0000000..19862ad --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-textcontent-basic-demo.tsx @@ -0,0 +1,28 @@ +import { defineComponent, markRaw } from 'vue'; +import { TextContent } from '../../../../../packages-user/client-base/src/components/textbox'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#e8f0ff' + }) +); + +const TEXT = + '这段文字只给了 loc 的四个值:位置、折行宽度与高度。' + + '超出宽度的部分会自动换到下一行,行与行之间留出 8 像素额外间距。'; + +// 基本用法:loc 四项同时给位置、折行宽度与高度,长句按宽度折行 +export default defineComponent(() => { + return () => ( + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/builtin-textcontent-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-textcontent-demo.tsx index 35d6a54..d9858da 100644 --- a/docs/.vitepress/components/demos/ui/builtin-textcontent-demo.tsx +++ b/docs/.vitepress/components/demos/ui/builtin-textcontent-demo.tsx @@ -1,27 +1,42 @@ -import { defineComponent } from 'vue'; +import { defineComponent, markRaw } from 'vue'; import { TextContent } from '../../../../../packages-user/client-base/src/components/textbox'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; +import { demoMaterials } from './text-demo-materials'; + +const TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#e8f0ff' + }) +); const TEXT = - '[c=#f2b84b][s=20]标题字号[/s][/c]\n' + - '[c=#7ee787]正文颜色,长句会按 width 折行,超出宽度的部分自动换到下一行[/c]\n' + - '[c=#8aa0b8][s=13]小字注释:[[[[ 和 ]]]] 显示为方括号[/s][/c]'; + '[icon=1] 图标与文字并排,[icon=demo_star] 也可以用别名\n' + + '[c=#f2b84b]外层黄色 [c=#7ee787]内层绿色[/c] 回到外层黄色[/c]\n' + + '[f=Georgia]Georgia [f=Verdana]Verdana 内层[/f] 回到 Georgia[/f]\n' + + '[s=20]20 号字[/s] 普通字号,[b]粗体[b] 回到常规,[i]斜体[i] 回到常规\n' + + '[[[[方括号]]]] 显示为 [[方括号]],[[ 与 ]] 显示为单个方括号\n' + + '[c=#8aa0b8][s=13]小字注释[/s][/c]'; -// 文本标记:颜色、字号、手动换行与方括号字面量,长句按 width 折行 +// 文本标记:嵌套颜色与字体关闭后恢复之前的值,粗体、斜体是切换,图标取自 materials export default defineComponent(() => { return () => ( - + - + ); diff --git a/docs/.vitepress/components/demos/ui/builtin-textcontent-height-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-textcontent-height-demo.tsx new file mode 100644 index 0000000..9dacd12 --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-textcontent-height-demo.tsx @@ -0,0 +1,59 @@ +import { defineComponent, markRaw } from 'vue'; +import { TextContent } from '../../../../../packages-user/client-base/src/components/textbox'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const FIXED_TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#e8f0ff' + }) +); + +const AUTO_TYPER = markRaw( + new TextContentTyper({ + lineHeight: 8, + fillStyle: '#e8f0ff' + }) +); + +const TEXT = + '同一段文字放在固定高度与自适应高度的组件里。固定高度只保留前几行,' + + '更多内容会被裁掉;自适应高度会按实际排版结果重新计算高度。'; + +// 高度:左侧固定高度,右侧 autoHeight 按内容长高 +export default defineComponent(() => { + return () => ( + + + + + + + + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/builtin-textcontent-methods-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-textcontent-methods-demo.tsx new file mode 100644 index 0000000..e086023 --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-textcontent-methods-demo.tsx @@ -0,0 +1,84 @@ +import { defineComponent, markRaw, ref } from 'vue'; +import { + TextContent, + TextContentExpose +} from '../../../../../packages-user/client-base/src/components/textbox'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const TYPER = markRaw( + new TextContentTyper({ + interval: 55, + lineHeight: 6, + fillStyle: '#e8f0ff' + }) +); + +const TEXT = + '这段文字会逐字显示。[w=8]等待八个字的打字时间后继续。\n' + + '[c=#7ee787]可以用按钮重播、立即显示全文,或者读取当前高度。[/c]'; + +// 实例方法:重播、立即显示与读取总高度 +export default defineComponent(() => { + const content = ref(); + const height = ref(0); + + const replay = () => { + content.value?.retype(); + }; + + const reveal = () => { + content.value?.showAll(); + }; + + const readHeight = () => { + height.value = content.value?.getHeight() ?? 0; + }; + + return () => ( + + + + + + + + (height.value = value)} + /> + + + + + + + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/builtin-textcontent-style-demo.tsx b/docs/.vitepress/components/demos/ui/builtin-textcontent-style-demo.tsx new file mode 100644 index 0000000..d3de60e --- /dev/null +++ b/docs/.vitepress/components/demos/ui/builtin-textcontent-style-demo.tsx @@ -0,0 +1,65 @@ +import { defineComponent, markRaw } from 'vue'; +import { TextContent } from '../../../../../packages-user/client-base/src/components/textbox'; +import { TextContentTyper } from '../../../../../packages-user/client-base/src/components/textboxTyper'; + +const FILL = markRaw( + new TextContentTyper({ + fillStyle: '#58c4dd', + lineHeight: 4 + }) +); + +const STROKE = markRaw( + new TextContentTyper({ + fillStyle: '#58c4dd', + strokeStyle: '#e8f0ff', + strokeWidth: 3, + lineHeight: 4 + }) +); + +const BOTH = markRaw( + new TextContentTyper({ + fillStyle: '#58c4dd', + strokeStyle: '#e8f0ff', + strokeWidth: 3, + lineHeight: 4 + }) +); + +// 填充与描边:fill、stroke 是模式开关,颜色与线宽来自 typer +export default defineComponent(() => { + return () => ( + + + + + + + + + + + + + ); +}); diff --git a/docs/.vitepress/components/demos/ui/text-demo-materials.ts b/docs/.vitepress/components/demos/ui/text-demo-materials.ts new file mode 100644 index 0000000..c8121b1 --- /dev/null +++ b/docs/.vitepress/components/demos/ui/text-demo-materials.ts @@ -0,0 +1,60 @@ +import { Texture } from '@motajs/render'; +import { TileType } from '@user/data-common'; +import type { + IMaterialFramedData, + ITextureManager +} from '../../../../../packages-user/client-base/src/material'; + +// 演示用图标:只在 demo 内生成,正文不展示生成方式 +function createIconTexture(ink: string, accent: string): Texture { + const canvas = document.createElement('canvas'); + canvas.width = 16; + canvas.height = 16; + + const ctx = canvas.getContext('2d')!; + ctx.fillStyle = ink; + ctx.fillRect(0, 0, 16, 16); + ctx.fillStyle = accent; + ctx.beginPath(); + ctx.arc(8, 8, 5, 0, Math.PI * 2); + ctx.fill(); + + return new Texture(canvas); +} + +function createTile(texture: Texture): IMaterialFramedData { + return { + texture, + tileType: TileType.Tileset, + frames: 1, + // 单帧图标:offset 同时是帧宽,TextContent 用它取整张源图 + offset: texture.width, + defaultFrame: 0 + }; +} + +const ICONS: Record = { + 1: createTile(createIconTexture('#f2b84b', '#141a22')), + 2: createTile(createIconTexture('#7ee787', '#141a22')) +}; + +const ALIASES: Record = { + demo_star: 2 +}; + +function getTile(num: number) { + return ICONS[num] ?? null; +} + +function getTileByAlias(alias: string) { + const num = ALIASES[alias]; + return num === undefined ? null : ICONS[num]; +} + +export const demoMaterials = { + getTile, + getTileByAlias, + tile(tile: number | string) { + return typeof tile === 'number' ? getTile(tile) : getTileByAlias(tile); + } +} as unknown as ITextureManager; diff --git a/docs/render-guide/advanced/builtin-text.md b/docs/render-guide/advanced/builtin-text.md index 1286a6c..0614192 100644 --- a/docs/render-guide/advanced/builtin-text.md +++ b/docs/render-guide/advanced/builtin-text.md @@ -1,6 +1,6 @@ # 内置组件:文本 -文字元素只画一行。要让长文字按宽度折行、逐字显示或自动向上滚动时,用内置的文本组件。这一篇讲 `TextContent` 和 `ScrollText`,每个都先说它解决什么问题,再给最小写法,最后接一个 live。 +文字元素只画一行,不会自动换行,也没有行高与对齐。要让长文字按宽度折行、逐字显示,或者在一块固定区域里自动向上滚动,就用内置的文本组件。这一篇讲 `TextContent` 与 `ScrollText`:先说它们解决什么问题,再给能直接运行的最小写法,并在每个知识点旁边配上 live。 ## 前置知识 @@ -11,132 +11,132 @@ ## TextContent -要让长文字按宽度自动折行,用 `TextContent`。 +`TextContent` 用来显示一段会按宽度折行的文字。它自己负责排版、量高和逐字显示,默认样式已经能直接使用;要改字体、颜色、行高或逐字间隔时,再配置它的 `typer`。 -第一步:必须给 `width`,排版只按这个宽度折行;`text` 放内容,`loc` 前两项决定位置。 +### 基本用法 + +`text` 放内容,`loc` 的四个值依次是 `x`、`y`、折行宽度、高度。折行宽度决定文字在哪里换行,高度决定组件占用的区域;两个值都要写。`width` 与 `height` 是等价的独立属性,同时写时以 `loc` 的第三、四项为准。 ```tsx import { TextContent } from '@user/client-base'; // 组件内容 ; ``` -高度默认由 `loc` 的第四项决定;不想自己算高度时加 `autoHeight`,组件按内容算高度。 +这段示例演示最小写法:位置、折行宽度、高度与文字都写在组件上。示例里的文字在 360 像素宽、140 像素高的区域里排版。 -想让文字逐字显示,加 `interval`,单位是毫秒,`0` 表示一次显示完: + + + + + +live 只保留文本组件本身:长句按 `loc` 第三项的宽度折行。 + +### 高度 + +不写 `autoHeight` 时,组件高度固定为 `loc` 第四项;文字超过高度时,超出部分不显示。写 `autoHeight` 后,第四项只作为初始高度,排版完成后组件按实际内容重新计算高度。 ```tsx ``` -打字结束时要做点什么,用 `onTypeEnd`。 +这段示例演示高度自适应:宽度仍然来自 `loc` 第三项,高度交给 `TextContent` 自己算。每次重新排版后,组件会重新计算高度,并通过 `onUpdateHeight` 把新高度报出来。 + + + + + + +左侧固定高度只保留前几行,右侧开启 `autoHeight` 后按内容长高。 + +### 填充与描边 + +`fill` 控制填充,默认开启;`stroke` 控制描边,默认关闭。只写 `stroke` 时文字仍然会填充;要只描边,写 `fill={false} stroke`。填充色、描边色与描边线宽不在 `TextContent` 上,而在 `typer` 的配置里。 + +```tsx +import { markRaw } from 'vue'; +import { TextContent, TextContentTyper } from '@user/client-base'; + +const outlined = markRaw( + new TextContentTyper({ + fillStyle: '#58c4dd', + strokeStyle: '#e8f0ff', + strokeWidth: 3 + }) +); + +// 组件内容 +; +``` + +这段示例演示填充与描边同时开启:`fill` 与 `stroke` 是模式开关,颜色和线宽由 `typer` 提供。`TextContentTyper` 是一个大对象,交给组件时用 `markRaw` 包一层,避免 Vue 为它建立响应式代理;这和 typer 的配置项无关。 + + + + + + +三行文字分别只填充、只描边、填充加描边;只描边那一行写了 `fill={false}`。 + +### 文字标记 + +`TextContent` 会在文字里解析一组标记,用来在同一个段落中改颜色、改字号或插入图标。标记在排版前被解析掉,不参与宽度测量。 + +| 标记 | 行为 | +| ------------------- | -------------------------------------------------------- | +| `[c=颜色]...[/c]` | 成对改颜色;嵌套时关闭内层会恢复外层颜色 | +| `[s=字号]...[/s]` | 成对改字号;嵌套时关闭内层会恢复外层字号 | +| `[f=字体名]...[/f]` | 成对改字体族;嵌套时关闭内层会恢复外层字体 | +| `[b]` | 在常规与粗体之间切换,没有 `[/b]` 闭合标记,写一次切一次 | +| `[i]` | 在常规与斜体之间切换,没有 `[/i]` 闭合标记,写一次切一次 | +| `\n` | 手动换行 | +| `[[` | 输出一个普通的 `[` | +| `]]` | 输出一个普通的 `]` | +| `[w=字数]` | 等待若干个字的时间 | +| `[icon=编号或别名]` | 插入一个图标 | + +`[c]`、`[s]`、`[f]` 没有参数时不会按标记处理,会原样显示。要显示 `[[` 和 `]]` 本身,源文本里写成 `[[[[` 和 `]]]]`。`[b]` 与 `[i]` 是切换标记,没有 `[/b]`、`[/i]` 这样的闭合写法;要切回常规,再写一次 `[b]` 或 `[i]` 即可。 + +`[icon=编号或别名]` 的图标来自 `materials` 提供的素材管理器:数字按图块编号查找,字符串按别名查找。把 `materials` 交给 `TextContent` 就能使用其中的图标;不写 `materials` 时,使用默认的主素材来源。下面的 live 里,图标由示例在本地提供。 + +嵌套关系可以这样写: ```tsx { - // 打字结束时执行 - }} -/> -``` - -全部参数: - -| 属性 | 作用 | 默认或常用值 | -| --------------------------- | ------------------------------------------- | ------------------- | -| `text` | 显示的文字 | `''` | -| `width` | 折行宽度 | 必填 | -| `loc` | 位置与高度 | `[]`(即 `[0, 0]`) | -| `autoHeight` | 按内容计算高度 | `false` | -| `interval` | 逐字间隔,单位毫秒 | `0` | -| `font` | 文字字体 | `new Font()` | -| `lineHeight` | 行与行之间的额外间距 | `0` | -| `fill` / `stroke` | 填充 / 描边文字 | `true` / `false` | -| `fillStyle` / `strokeStyle` | 填充 / 描边颜色 | `#fff` / `#000` | -| `strokeWidth` | 描边线宽 | `2` | -| `textAlign` | 多行文字左对齐 / 居中 / 右对齐 | `TextAlign.Left` | -| `wordBreak` | 断词规则:不换行 / 空格与 CJK 处 / 任意字符 | `WordBreak.Space` | -| `breakChars` | 额外参与分词的字符 | `''` | -| `ignoreLineStart` | 不放在行首的字符 | `''` | -| `ignoreLineEnd` | 不放在行尾的字符 | `''` | -| `onTypeStart` / `onTypeEnd` | 开始 / 结束打字时触发 | 无 | -| `onUpdateHeight` | 高度重算后触发,参数为新高度 | 无 | - -多行文字时,`lineHeight` 默认为 `0`,行与行会贴得比较紧;通常设成 `8` 左右阅读起来更舒展。 - -需要重播或立刻显示全文时,组件实例还暴露 `retype()`、`showAll()` 和 `getHeight()`。 - -### 文本标记 - -要在文本里改颜色,写 `[c=颜色]...[/c]`: - -```tsx - -``` - -要改字号,写 `[s=字号]...[/s]`: - -```tsx - -``` - -要换行,写 `\n`;在 TSX 里用模板字符串或字符串拼接: - -```tsx - -``` - -`[[` 输出一个普通的 `[`,`]]` 输出一个普通的 `]`。要显示 `[[` 和 `]]` 本身,源文本里写成 `[[[[` 和 `]]]]`: - -```tsx - -``` - -`[c=...]` 与 `[s=...]` 要成对闭合,可以嵌套,例如 `[c=#f2b84b][s=20]标题[/s][/c]`。只写 `[c]` 没有颜色参数,不会按标记处理,会原样显示。 - -需要粗体、斜体、换字体、等待或图标时,再用这些标记: - -| 标记 | 作用 | -| ------------------- | ------------------ | -| `[b]` | 切换粗体 | -| `[i]` | 切换斜体 | -| `[f=字体名]...[/f]` | 改字体族 | -| `[w=字数]` | 等待若干个字的时间 | -| `[icon=图标名]` | 插入图标 | - -`[b]` 和 `[i]` 是切换,不是成对闭合:写一次切换一次。标记可以组合使用,也可以跨行书写。 - -把颜色、字号、换行与方括号放在一起: - -```tsx - ``` +这段示例演示三种成对标记的嵌套与切换标记:内层 `[c]`、`[f]`、`[s]` 关闭后,后面的文字恢复成外层值;`[b]` 与 `[i]` 没有闭合标记,再写一次就切回常规。 + -同一段文字里混用了颜色、字号、手动换行与方括号字面量,长句会按 `width` 折行。 +live 中的文字同时使用嵌套颜色、嵌套字体、字号、粗体、斜体、手动换行、方括号字面量与图标;图标使用的素材由示例在本地提供。 + +### 实例方法 + +需要重播打字、立刻显示全文或读取总高度时,用 `ref` 拿到组件实例。实例暴露三个方法: + +- `retype()`:从头开始重新打字; +- `showAll()`:立刻显示全部文字; +- `getHeight()`:返回当前排版结果的总高度。 + +```tsx +import { markRaw, ref } from 'vue'; +import { + TextContent, + TextContentExpose, + TextContentTyper +} from '@user/client-base'; + +const content = ref(); +const typer = markRaw(new TextContentTyper({ interval: 50 })); + +// 某个事件里立刻显示全文 +const reveal = () => content.value?.showAll(); + +// 组件内容 +; +``` + +这段示例演示通过实例方法控制打字:`ref` 保存组件实例,`showAll()` 跳过剩余间隔,直接显示全部文字;`retype()` 从头开始;`getHeight()` 读取总高度。 + + + + + + +live 中的按钮分别调用 `retype()`、`showAll()` 与 `getHeight()`,并显示高度读数。 + +### 参数 + +| 属性 | 作用 | 默认或常用值 | +| ---------------- | ------------------------------------------- | ------------------------- | +| `text` | 显示的文字 | `''` | +| `loc` | 位置与大小,四项依次是 x、y、折行宽度、高度 | 常用 `[40, 40, 360, 140]` | +| `width` | 折行宽度,与 `loc` 第三项同义 | 写 `loc` 时不用再写 | +| `height` | 固定高度,与 `loc` 第四项同义 | 写 `loc` 时不用再写 | +| `autoHeight` | 按内容计算高度 | `false` | +| `fill` | 是否填充文字 | `true` | +| `stroke` | 是否描边文字 | `false` | +| `typer` | 文本排版与打字配置 | 默认配置的 typer | +| `materials` | 图标素材管理器,可省略 | 默认素材管理器 | +| `onTypeStart` | 开始打字时触发 | 无 | +| `onTypeEnd` | 打字结束时触发 | 无 | +| `onUpdateHeight` | 高度重算后触发,参数为新高度 | 无 | + +## TextContentTyper + +默认 typer 已经能显示文字。要改字体、行高、文字颜色、对齐、断词规则或逐字间隔时,才需要自己构造一个 `TextContentTyper`,再通过 `TextContent` 的 `typer` 属性传进去。typer 是一个大对象,交给组件时用 `markRaw` 包一层,避免 Vue 为它建立响应式代理。 + +```tsx +import { Font } from '@motajs/render'; +import { markRaw } from 'vue'; +import { + TextContent, + TextContentTyper, + TextAlign, + WordBreak +} from '@user/client-base'; + +const typer = markRaw( + new TextContentTyper({ + font: new Font('Verdana', 18), + lineHeight: 8, + interval: 30, + textAlign: TextAlign.Left, + wordBreak: WordBreak.Space, + fillStyle: '#e8f0ff' + }) +); + +// 组件内容 +; +``` + +这段示例演示 typer 的常见配置:18px 字体、8 像素额外行距、逐字间隔 30 毫秒、左对齐、按空格与 CJK 字符折行、浅色填充。一个 typer 保存着当前文字与打字进度,只交给一个 `TextContent` 使用。 + +### 配置字段 + +| 字段 | 作用 | 默认或常用值 | +| ----------------- | ---------------------------------------------- | ----------------- | +| `font` | 字体,使用 `Font` 实例 | 默认 `Font` | +| `interval` | 每个字之间的间隔,单位毫秒;`0` 表示一次显示完 | `0` | +| `lineHeight` | 行与行之间的额外间距 | `0` | +| `textAlign` | 每行在折行宽度内的对齐方式 | `TextAlign.Left` | +| `wordBreak` | 断词规则 | `WordBreak.Space` | +| `breakChars` | 额外参与分词的字符 | `''` | +| `ignoreLineStart` | 额外不放在行首的字符 | `''` | +| `ignoreLineEnd` | 额外不放在行尾的字符 | `''` | +| `fillStyle` | 文字填充色 | `'#fff'` | +| `strokeStyle` | 文字描边色 | `'#000'` | +| `strokeWidth` | 文字描边线宽 | `2` | +| `materials` | 图标素材管理器,可省略 | 默认素材管理器 | + +`width` 不在这里设置:`TextContent` 每次重新排版时,会把组件自己的折行宽度写进 typer;typer 里写了也会被覆盖。`materials` 通常不用自己传,`TextContent` 会把可用的素材管理器交给它。 + + + + + + +三列文字使用不同的字体、字号、行高、对齐与填充色,对照 typer 的配置效果。 + +### 折行规则 + +`wordBreak` 决定文字可以在哪里断开: + +| 取值 | 行为 | +| ----------------- | ---------------------------------------------------- | +| `WordBreak.None` | 不折行,内容一行到底 | +| `WordBreak.Space` | 在空格、连字符等位置折行,CJK 字符可以单独折行,默认 | +| `WordBreak.All` | 任意字符都可以折行 | + +`breakChars` 在默认分词字符之外再补充可断开的字符;`ignoreLineStart` 与 `ignoreLineEnd` 在默认标点集合之外,再补充不允许出现在行首或行尾的字符。例如中文标点不应该落在行首时,可以把它加进 `ignoreLineStart`。 + +只控制不折行时,构造一个 typer: + +```tsx +import { Font } from '@motajs/render'; +import { markRaw } from 'vue'; +import { TextContent, TextContentTyper, WordBreak } from '@user/client-base'; + +const noWrap = markRaw( + new TextContentTyper({ + font: new Font('Verdana', 14), + wordBreak: WordBreak.None + }) +); + +// 组件内容 +; +``` + +这段示例演示 `WordBreak.None`:宽度仍然来自 `loc` 第三项,但 typer 禁止在宽度处换行。折行只受宽度影响,宽度来自 `TextContent` 的 `loc` 第三项或 `width` 属性,与 typer 无关。 + + + + + + +三列使用同一段英文,`None` 一行到底,`Space` 在空格处折行,`All` 连长单词内部也可以折行。 + +### 打字行为 + +`interval` 大于 `0` 时,文字从第一个字开始逐个出现,每两个字之间隔 `interval` 毫秒;`interval` 为 `0` 时,所有文字一次显示完。开始与结束分别触发 `onTypeStart` 与 `onTypeEnd`。 + +打字过程中调用 `showAll()` 会立刻补齐剩余文字;调用 `retype()` 则清空当前进度,从第一个字重新开始。需要控制逐字出现的节奏时,改 `interval`;需要改字体、颜色或行高时,改同一个 typer 的其他配置。 + +下面的示例用按钮显式开始,不在页面打开时自动打完: + +```tsx +import { markRaw, nextTick, ref } from 'vue'; +import { + TextContent, + TextContentExpose, + TextContentTyper +} from '@user/client-base'; + +const content = ref(); +const started = ref(false); +const typer = markRaw(new TextContentTyper({ interval: 80 })); + +const start = async () => { + if (!started.value) { + started.value = true; + await nextTick(); + } + content.value?.retype(); +}; + +// 组件内容 +; +``` + +这段示例演示可控的打字节奏:初始文字为空,点击按钮后才写入文字并调用 `retype()`;下次点击同一个按钮会从第一个字重播。 + + + + + + +live 初始停在「等待开始」,点击「开始 / 重播」后才逐字显示;`[w=8]` 会在中途多等八个字的打字时间,「立即显示」会跳过剩余文字。 ## ScrollText -要让文字自动向上滚动,用 `ScrollText`。 +`ScrollText` 用来让一段较长的文字在固定区域里自动向上滚动。它把 `TextContent` 放进一个滚动视口,按每秒多少像素把内容向上推;滚到末尾时触发 `scrollEnd`,随后停在底部。 -基本写法里,`width` 是文字的最大宽度,`speed` 是每秒滚动的像素数,`text` 放内容,`loc` 是滚动区域的视口。 +### 基本用法 + +`loc` 是滚动视口的位置与大小,四个值依次是 `x`、`y`、视口宽度、视口高度;`width` 是文字的折行宽度,通常与视口宽度一致;`speed` 是每秒滚动的像素数,`text` 放内容。 ```tsx import { ScrollText } from '@user/client-base'; @@ -159,22 +380,16 @@ import { ScrollText } from '@user/client-base'; const notice = '这是一条会自己向上滚动的公告。'.repeat(8); // 组件内容 -; +; ``` -文字从下往上滚过视口,超出视口的部分不显示。 +这段示例演示最基本的滚动:内容在 400×170 的视口里从下往上移动,超出的部分不显示。 -开始滚动前想先留一点空白,用 `pad` 给预留的像素数,默认 `16`: +文字正式开始进入视口之前,想先留一点空白,用 `pad` 给预留的像素数,默认 16: ```tsx ``` -排版参数与 `TextContent` 相同,`fillStyle`、`font`、`lineHeight` 照常写。`width` 是滚动区宽度,文字实际按 `width - 16` 折行;`loc` 的第四项是视口高度,省略时取 `width`。 - -全部参数: - -| 属性 | 作用 | 默认或常用值 | -| --------------------------- | ------------------------------------------- | ----------------- | -| `loc` | 滚动视口的位置与大小 | 必填 | -| `width` | 滚动区宽度 | 必填 | -| `speed` | 每秒滚动像素数 | 必填 | -| `pad` | 开始滚动前预留的像素 | `16` | -| `text` | 滚动文字 | `''` | -| `font` | 文字字体 | `new Font()` | -| `fillStyle` / `strokeStyle` | 填充 / 描边颜色 | `#fff` / `#000` | -| `fill` / `stroke` | 填充 / 描边文字 | `true` / `false` | -| `strokeWidth` | 描边线宽 | `2` | -| `lineHeight` | 行与行之间的额外间距 | `0` | -| `textAlign` | 多行文字左对齐 / 居中 / 右对齐 | `TextAlign.Left` | -| `wordBreak` | 断词规则:不换行 / 空格与 CJK 处 / 任意字符 | `WordBreak.Space` | -| `onScrollEnd` | 滚动到底时触发一次,随后停止 | 无 | - -需要中途控制滚动时,组件实例还暴露 `pause()`、`resume()`、`setSpeed(speed)` 和 `rescroll()`。 +这段示例演示 `pad`:文字先停在视口下方 32 像素处,再开始向上滚动。`pad` 只留垂直方向的空白,不是左右内边距。 @@ -211,13 +406,152 @@ const notice = '这是一条会自己向上滚动的公告。'.repeat(8); -公告在固定视口里持续向上滚动。 +live 中的公告在固定视口里持续向上滚动,滚到底后停顿片刻再从头开始。 + +### 文本样式 + +`ScrollText` 接收 `TextContent` 的文本参数:`text`、`fill`、`stroke`、`typer` 与 `materials` 都可以直接传给 `ScrollText`,再由它交给内部的 `TextContent`。字体、颜色、行高、对齐等仍然属于 `typer` 的配置,不要写成 `ScrollText` 的直接属性。只描边时同样写 `fill={false} stroke`。 + +```tsx +import { Font } from '@motajs/render'; +import { markRaw } from 'vue'; +import { ScrollText, TextContentTyper } from '@user/client-base'; + +const typer = markRaw( + new TextContentTyper({ + font: new Font('Verdana', 18), + lineHeight: 10, + fillStyle: '#e8f0ff', + strokeStyle: '#58c4dd', + strokeWidth: 2 + }) +); + +// 组件内容 +; +``` + +这段示例演示滚动文字的样式:字体、行距、填充与描边都来自同一个 typer;`width` 与视口宽度同为 400,文字在整块视口里排版,`pad` 让文字先从视口下方进入。 + + + + + + +live 演示字体、行距、填充与描边;`pad` 让文字先从视口下方进入。 + +### 实例方法 + +需要中途控制滚动时,用 `ref` 拿到组件实例。实例暴露四个方法: + +- `pause()`:暂停滚动; +- `resume()`:继续滚动; +- `setSpeed(speed)`:修改每秒滚动的像素数; +- `rescroll()`:立刻回到起点,重新滚动。 + +滚动到底时还会触发一次 `onScrollEnd`;之后组件会停在底部,需要重播时调用 `rescroll()`,再调用 `resume()`。 + +```tsx +import { ref } from 'vue'; +import { ScrollText, ScrollTextExpose } from '@user/client-base'; + +const scroll = ref(); + +// setup +const pause = () => scroll.value?.pause(); +const resume = () => scroll.value?.resume(); +const faster = () => scroll.value?.setSpeed(120); + +// 回到起点后要继续滚动,必须再调用 resume +const restart = () => { + scroll.value?.rescroll(); + scroll.value?.resume(); +}; + +// 组件内容 + { + // 已经停在底部,等待重新开始 + }} +/>; +``` + +这段示例演示滚动控制:`pause()` 与 `resume()` 只改暂停状态,`setSpeed()` 只影响之后的滚动,`rescroll()` 把位置拉回起点但不会自动继续。 + + + + + + +live 的四个按钮分别暂停、继续、在 70 与 140 像素每秒之间切换、回到起点并继续滚动;状态行显示当前状态与速度。 + +### 参数 + +| 属性 | 作用 | 默认或常用值 | +| ------------- | ------------------------------ | ------------------------- | +| `loc` | 滚动视口的位置与大小,四项都写 | 常用 `[40, 40, 400, 170]` | +| `width` | 文字的折行宽度 | 必填 | +| `speed` | 每秒滚动的像素数 | 必填 | +| `pad` | 文字进入视口前预留的垂直空白 | `16` | +| `text` | 滚动文字 | `''` | +| `typer` | 文本排版与打字配置 | 默认配置的 typer | +| `fill` | 是否填充文字 | `true` | +| `stroke` | 是否描边文字 | `false` | +| `materials` | 图标素材管理器,可省略 | 默认素材管理器 | +| `onScrollEnd` | 滚动到底时触发一次 | 无 | + +## 边界与注意 + +- `loc` 统一写四个值。折行宽度和高度分别来自第三、四项;把 `loc` 写成三项会少掉高度。 +- `lineHeight` 是行与行之间的额外间距,不是整行高度;默认 `0` 时多行会贴得比较紧。 +- `fill` 与 `stroke` 只是模式开关;填充色、描边色与线宽属于 `typer`,不要直接写在 `TextContent` 或 `ScrollText` 上。只描边要写 `fill={false} stroke`。 +- 一个 `typer` 实例只交给一个文本组件使用;多个组件共用会互相覆盖当前文字与打字进度。 +- 不写 `autoHeight` 时,`TextContent` 的高度固定为 `loc` 第四项,不会随内容变长;超出高度的部分不显示。 +- `ScrollText` 的 `width` 是文字折行宽度,通常与 `loc` 第三项写成一样。`pad` 是垂直方向的预留空白,不影响折行宽度。 +- `ScrollText` 滚到末尾后会暂停;需要重播时先 `rescroll()` 再 `resume()`,只调用其中一个不会从头开始。 +- `TextContentTyper` 是大对象,传给 `typer` 时用 `markRaw` 包一层,避免 Vue 建立响应式代理。