mirror of
https://github.com/2234839/web-font.git
synced 2026-12-02 05:34:03 +08:00
- npm 0.2.0:脏节点增量扫描 + 贡献者回记 + 百万节点零风暴(见 0ab51e4) - demo 页三个裸导入统一进 importmap,版本引用 0.1.0/0.2.0 混用 → 全部 @latest
160 lines
7.2 KiB
HTML
160 lines
7.2 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>leafer-x-webfont —— LeaferJS 海报中文字体 Demo</title>
|
||
<style>
|
||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||
body { font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; background: #f5f5f4; color: #1c1917; }
|
||
.wrap { max-width: 960px; margin: 0 auto; padding: 32px 20px 80px; }
|
||
h1 { font-size: 24px; margin-bottom: 8px; }
|
||
.sub { color: #78716c; font-size: 14px; margin-bottom: 24px; line-height: 1.7; }
|
||
.sub a { color: #0d9488; }
|
||
.panel { background: #fff; border: 1px solid #e7e5e4; border-radius: 12px; padding: 16px; margin-bottom: 16px; display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
|
||
label { font-size: 13px; color: #57534e; }
|
||
input[type="text"], select { padding: 8px 10px; border: 1px solid #d6d3d1; border-radius: 8px; font-size: 14px; outline: none; }
|
||
input[type="text"] { width: 280px; }
|
||
input[type="text"]:focus, select:focus { border-color: #0d9488; }
|
||
button { padding: 8px 16px; background: #0d9488; color: #fff; border: none; border-radius: 8px; font-size: 14px; cursor: pointer; }
|
||
button:hover { background: #0f766e; }
|
||
#canvas-host { width: 100%; height: 480px; background: #fff; border: 1px solid #e7e5e4; border-radius: 12px; overflow: hidden; }
|
||
.status { font-size: 12px; color: #78716c; }
|
||
.ok { color: #0d9488; font-weight: 600; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="wrap">
|
||
<h1>leafer-x-webfont</h1>
|
||
<p class="sub">
|
||
LeaferJS 中文字体插件 —— 画布里的 Text 用什么字体,就只加载那几个字的子集(6 字 ≈ 6KB,而非 16MB)。
|
||
服务端:<a href="https://webfont.shenzilong.cn" target="_blank">webfont.shenzilong.cn</a>
|
||
/ <a href="https://github.com/2234839/web-font/tree/new/packages/leafer-x-webfont" target="_blank">插件源码</a>
|
||
/ <a href="https://github.com/2234839/web-font/blob/new/packages/leafer-x-webfont/demo/index.html" target="_blank">本页源码</a>
|
||
/ <a href="https://www.npmjs.com/package/leafer-x-webfont" target="_blank">npm</a>
|
||
</p>
|
||
|
||
<div class="panel">
|
||
<label>标题文字</label>
|
||
<input id="title-input" type="text" value="静心茶舍" maxlength="12">
|
||
<label>字体</label>
|
||
<select id="font-select"></select>
|
||
<button id="export-btn">导出 PNG</button>
|
||
<span class="status" id="status">初始化…</span>
|
||
</div>
|
||
|
||
<div id="canvas-host"></div>
|
||
</div>
|
||
|
||
<!-- leafer-ui 2.2.9(与项目 @leafer-ui/node 版本对齐),CDN 版本与原 vendor 文件逐字节一致 -->
|
||
<script src="https://cdn.jsdelivr.net/npm/leafer-ui@2.2.9/dist/web.min.js"></script>
|
||
<!-- leafer 导出 PNG 需要额外插件(画布转 blob) -->
|
||
<script src="https://cdn.jsdelivr.net/npm/@leafer-in/export@2.2.9/dist/export.min.js"></script>
|
||
<!-- demo 全 CDN 化:leafer-ui / export 插件走 jsDelivr IIFE,插件与 SDK 走 jsDelivr ESM @latest(importmap 统一解析裸导入:插件 dist 内部裸导入 webfont-sdk,也必须在此映射) -->
|
||
<script type="importmap">
|
||
{
|
||
"imports": {
|
||
"leafer-x-webfont": "https://cdn.jsdelivr.net/npm/leafer-x-webfont@latest/dist/index.js",
|
||
"webfont-sdk": "https://cdn.jsdelivr.net/npm/webfont-sdk@latest/dist/index.js",
|
||
"webfont-sdk/api": "https://cdn.jsdelivr.net/npm/webfont-sdk@latest/dist/api.js"
|
||
}
|
||
}
|
||
</script>
|
||
<script type="module">
|
||
import { WebFontPlugin } from 'leafer-x-webfont'
|
||
import { createWebFontApi } from 'webfont-sdk/api'
|
||
|
||
const { Leafer, Text, Rect, Ellipse, Image } = LeaferUI
|
||
const status = document.getElementById('status')
|
||
|
||
const leafer = new Leafer({ view: 'canvas-host', fill: '#fafaf9' })
|
||
/** 字体子集化服务:本地开发连本地后端,任何部署环境(主站/GitHub Pages)一律走线上服务(后端 CORS 全开) */
|
||
const isDev = location.hostname === 'localhost' || location.hostname === '127.0.0.1'
|
||
const fontApi = isDev ? 'http://localhost:8087' : 'https://webfont.shenzilong.cn'
|
||
const webfont = new WebFontPlugin(leafer, { debug: true, baseUrl: fontApi })
|
||
|
||
/* ---------- 字体列表:API 实时拉取(不再硬编码) ---------- */
|
||
const api = createWebFontApi({ baseUrl: fontApi })
|
||
let fonts
|
||
try {
|
||
fonts = (await api.fonts()).filter((f) => !f.temporary)
|
||
} catch (e) {
|
||
status.textContent = `字体列表拉取失败:${e.message}`
|
||
throw e
|
||
}
|
||
/** 初始字体选择:优先演示字体,服务端没有则回退列表第一个 */
|
||
const pick = (prefer) => fonts.find((f) => f.name === prefer)?.name ?? fonts[0]?.name ?? ''
|
||
|
||
/* ---------- 海报场景 ---------- */
|
||
const bg = new Rect({ x: 0, y: 0, width: 900, height: 480, fill: { type: 'linear', from: { x: 0, y: 0 }, to: { x: 1, y: 1 }, stops: [{ offset: 0, color: '#fef3c7' }, { offset: 1, color: '#fde68a' }] } })
|
||
const deco = new Ellipse({ x: 660, y: -80, width: 360, height: 360, fill: 'rgba(255,255,255,0.35)' })
|
||
const deco2 = new Ellipse({ x: -100, y: 320, width: 300, height: 300, fill: 'rgba(255,255,255,0.25)' })
|
||
|
||
const title = new Text({
|
||
x: 80, y: 150,
|
||
text: '静心茶舍',
|
||
fontFamily: pick('令东齐伋复刻体.ttf'),
|
||
fontSize: 96,
|
||
fill: '#1c1917',
|
||
letterSpacing: 8,
|
||
})
|
||
const subtitle = new Text({
|
||
x: 84, y: 290,
|
||
text: '以茶为媒 · 观自在',
|
||
fontFamily: pick('霞鹜文楷.ttf'),
|
||
fontSize: 28,
|
||
fill: '#57534e',
|
||
letterSpacing: 4,
|
||
})
|
||
leafer.add(bg)
|
||
leafer.add(deco)
|
||
leafer.add(deco2)
|
||
leafer.add(title)
|
||
leafer.add(subtitle)
|
||
|
||
/* ---------- 控件交互 ---------- */
|
||
const fontSelect = document.getElementById('font-select')
|
||
for (const f of fonts) {
|
||
const opt = document.createElement('option')
|
||
opt.value = f.name
|
||
/** label 用去扩展名的文件名;更精细的展示名是 font-config displayName 的职责 */
|
||
opt.textContent = f.name.replace(/\.(ttf|otf|woff2?)$/i, '')
|
||
fontSelect.appendChild(opt)
|
||
}
|
||
/** 当前标题字体在列表中高亮选中 */
|
||
if (fonts.some((f) => f.name === title.fontFamily)) fontSelect.value = title.fontFamily
|
||
fontSelect.onchange = () => {
|
||
title.fontFamily = fontSelect.value
|
||
status.textContent = '字体切换中…'
|
||
}
|
||
|
||
document.getElementById('title-input').oninput = (e) => {
|
||
title.text = e.target.value || ' '
|
||
}
|
||
|
||
document.getElementById('export-btn').onclick = async () => {
|
||
status.textContent = '等待字体就绪…'
|
||
await webfont.ready()
|
||
/** leafer.export 返回 { data: dataURL, ... },转 Blob 下载 */
|
||
const { data } = await leafer.export('png', { pixelRatio: 2 })
|
||
const blob = await (await fetch(data)).blob()
|
||
const url = URL.createObjectURL(blob)
|
||
const a = document.createElement('a')
|
||
a.href = url
|
||
a.download = `海报-${title.text}.png`
|
||
a.click()
|
||
URL.revokeObjectURL(url)
|
||
status.textContent = '已导出 ✓'
|
||
setTimeout(() => status.textContent = '', 2000)
|
||
}
|
||
|
||
/* ---------- 首屏字体就绪提示 ---------- */
|
||
setTimeout(async () => {
|
||
await webfont.ready()
|
||
const count = webfont.families.length
|
||
status.innerHTML = `<span class="ok">就绪</span> · 已加载 ${count} 种字体的子集(打开 DevTools Network 查看 API 请求体积)`
|
||
}, 600)
|
||
</script>
|
||
</body>
|
||
</html>
|