Skip to content

Commit cbd0684

Browse files
committed
渲染字体
1 parent 40645e6 commit cbd0684

5 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎src/app.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { gensvg } from './gensvg'
22
import { startSimulation } from './server/physics'
3+
import { TEXT } from './utils'
34
import pageApp from './web/index.html'
45

5-
const res = await startSimulation('flycran')
6+
const res = await startSimulation('yyx990803')
67

78
Bun.serve({
89
port: 3000,

‎src/server/parseTextPath.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import earcut from 'earcut'
22
import opentype from 'opentype.js'
33
import { pointsOnPath } from 'points-on-path'
4+
import { FONT_SIZE, SCALE } from '../utils'
45

56
function svgPathToRapierTrimesh(path: string, scale = 1, tolerance = 1) {
67
// pointsOnPath returns Point[][] — one array per sub-path, flatten them
@@ -40,7 +41,7 @@ export interface TextPathResult {
4041
}
4142

4243
export function getTextPaths(text: string): TextPathResult[] {
43-
const paths = font.getPaths(text, 0, 0, 80)
44+
const paths = font.getPaths(text, 0, 0, FONT_SIZE / SCALE)
4445

4546
return paths.map((path) => {
4647
const bbox = path.getBoundingBox()

‎src/server/physics.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
INTENSITY_COLORS,
1919
RawContributionDay
2020
} from './github-contributions'
21+
import { getTextPaths } from './parseTextPath'
2122

2223
export class Contribution implements ContributionJson {
2324
date: string
@@ -97,6 +98,35 @@ export async function startSimulation(
9798
const world = await createWord()
9899
const contributionsSet = new Set<Contribution>()
99100

101+
// 创建用户名文字静止刚体(居中对齐),同时收集三角网格数据
102+
const textPaths = getTextPaths(username)
103+
const totalWidth = textPaths.reduce((sum, { rect }) => sum + rect.width, 0)
104+
const offsetX = (WIDTH - totalWidth) / 2
105+
const centerY = HEIGHT / 2
106+
const textTriangles: number[][] = []
107+
108+
for (const { paths } of textPaths) {
109+
const { vertices, indices } = paths
110+
const cx = offsetX
111+
world.createCollider(
112+
ColliderDesc.trimesh(vertices, indices),
113+
world.createRigidBody(RigidBodyDesc.fixed().setTranslation(cx, centerY))
114+
)
115+
for (let i = 0; i < indices.length; i += 3) {
116+
const i0 = indices[i] * 2,
117+
i1 = indices[i + 1] * 2,
118+
i2 = indices[i + 2] * 2
119+
textTriangles.push([
120+
cx + vertices[i0],
121+
centerY + vertices[i0 + 1],
122+
cx + vertices[i1],
123+
centerY + vertices[i1 + 1],
124+
cx + vertices[i2],
125+
centerY + vertices[i2 + 1]
126+
])
127+
}
128+
}
129+
100130
function createContribution(
101131
contributionDay: RawContributionDay,
102132
x: number,
@@ -180,6 +210,7 @@ export async function startSimulation(
180210
time: endTime - startTime,
181211
contributions: Array.from(contributionsSet).map((contribution) =>
182212
contribution.toJSON()
183-
)
213+
),
214+
textTriangles
184215
}
185216
}

‎src/utils/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export const HEIGHT = 100
33
export const SIZE = 2
44
export const SCALE = 6
55
export const INTERVAL = SIZE / 2
6+
export const FONT_SIZE = 120
7+
export const TEXT = 'Flycran'
68

79
export interface ContributionJson {
810
date: string
@@ -17,4 +19,6 @@ export interface SimulationResult {
1719
stepCount: number
1820
time: number
1921
contributions: ContributionJson[]
22+
/** 用户名文字碰撞体的三角网格(世界坐标),用于 web 端渲染 */
23+
textTriangles: number[][]
2024
}

‎src/web/index.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,17 @@ function createContribution(contribution: ContributionJson) {
5151

5252
trajectoryRes.contributions.forEach(createContribution)
5353

54+
// 用三角网格渲染用户名文字(与物理碰撞体完全一致)
55+
for (const tri of trajectoryRes.textTriangles) {
56+
const [x0, y0, x1, y1, x2, y2] = tri
57+
const poly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon')
58+
poly.setAttribute(
59+
'points',
60+
`${(x0 * SCALE).toFixed(1)},${((HEIGHT - y0) * SCALE).toFixed(1)} ${(x1 * SCALE).toFixed(1)},${((HEIGHT - y1) * SCALE).toFixed(1)} ${(x2 * SCALE).toFixed(1)},${((HEIGHT - y2) * SCALE).toFixed(1)}`
61+
)
62+
poly.setAttribute('fill', '#333')
63+
poly.setAttribute('opacity', '0.3')
64+
svg.appendChild(poly)
65+
}
66+
5467
export {}

0 commit comments

Comments
 (0)