简单来说
一本短篇小说的第一章,显示成一本摊开在桌上的真书。用鼠标翻页,纸张、封面和桌面都在配置里选。
成品一览
短篇小说The Ferryman’s Ledger的第一章,页面129 × 198毫米,摊开在一张胡桃木桌上。书壳是瓶绿色的布面,书页是小说常用的松厚米色纸。读者捏住右页的边缘翻过去,纸页弯曲,在下面一页投下影子。正文用buildDocument排一次,再调用一次postext-folio包,就把这个版面变成一本书。书的材质(纸张、装订、桌面、光照、视角)都写在配置里,所以同一个文档在沙盒的Folio标签页里也是这个样子。
这道食谱解答
- 怎样把文档显示成读者可以用手翻页的3D书本?
- 付印前怎样预览书的装订、封面、纸张和光照效果?
简短回答
const stage = document.createElement('section');
stage.id = 'folio';
stage.ariaLabel = 'The book in 3D';
stage.style.cssText = 'height: min(78vh, 720px); margin: 0 auto; max-width: 1280px';
document.getElementById('pages').before(stage);
createFolioFromDocument(stage, doc, {
// The paper, binding, desk and light come from doc.config.folio. Only the photographed
// desk is the host's choice: without textureBaseUrl the walnut is drawn procedurally.
appearance: { textureBaseUrl: 'https://postext.dev/folio/textures' },
onChange: ({ pages }) => kitStatus(`${doc.pages.length} pages · open at `
+ pages.map((i) => doc.pages[i].pageLabel || i + 1).join('–')),
});
用料
- 功能
- 3D书本查看器(Folio)纸张、装订、台面与光照(Folio)在Canvas上绘制页面设计过的章首页标题属性书眉与页码按页面角色显示书眉对称页边距段末孤行、段首孤行与孤字段落样式语义调色板排版前加载字体
- 类型
- Literata, Fraunces, Work Sans(SIL OFL 1.1)
- 素材
- 无:所有图片都用代码绘制
做法
#1 · 说明书是用什么做的
const folio = {
tilt: 26, // degrees from straight above: enough to see the depth of the page block
paper: { type: 'bookWove', grammage: 70, shade: col('cream') }, // a bulky cream novel paper
binding: { type: 'hardcover', cover: 'case', coverMaterial: 'cloth',
coverColor: col('accent') }, // the case wraps the pages; boards a little larger
surface: { type: 'walnut' },
lighting: { environment: 'studio', intensity: 1, shadows: true },
};
config.folio只有3D查看器读取:排版、Canvas、PDF和HTML输出都忽略它,所以换纸不会挪动任何一行。bookWove是松厚的小说纸,70 g/m²按它的松厚度算,一张纸约112微米厚;查看器再乘以页数,得出书芯的厚度。布面颜色链接到调色板的强调色,与眉题和页码同一种绿。
#2 · 安静的章首页和书眉
const opener = { enabled: true, minHeight: mm(50), slot: { elements: [
{ kind: 'text', id: 'kicker', content: '{attr.kicker}', fontFamily: 'Work Sans',
fontSize: pt(7.5), fontWeight: 600, letterSpacing: pt(1.5), textTransform: 'uppercase',
color: col('accent'), align: 'left',
placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(10) } } },
{ kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Fraunces', fontSize: pt(28),
lineHeight: 1.05, fontWeight: 600, color: col('ink'), align: 'left', overflow: 'wrap',
placement: { anchor: { to: '#kicker', edge: 'below' }, offset: { y: mm(3) },
size: { width: mm(90) } } },
{ kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1.25), color: col('accent'),
placement: { anchor: { to: '#title', edge: 'below' }, offset: { y: mm(4.5) },
size: { width: mm(12) } } },
] } };
章节从奇数页三分之一高处开始,眉题取自标题的属性。在3D里,下沉的章首页和印刷的小说一样:打开封面时,读者先看到页面顶部的留白。
const head = (id, content, parity, edge, x, extra = {}) => ({ kind: 'text', id, content, parity,
pages: 'body', fontFamily: 'Work Sans', fontSize: pt(7), fontWeight: 500,
letterSpacing: pt(1.1), textTransform: 'uppercase', color: col('muted'),
placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(11) } }, ...extra });
const folioNumber = { fontWeight: 600, color: col('accent') };
const header = { elements: [
head('verso-folio', '{pageNumber}', 'even', 'top-left', OUTER, folioNumber),
head('verso-author', '{author}', 'even', 'top-left', OUTER + 8),
head('recto-title', '{title}', 'odd', 'top-right', -(OUTER + 8)),
head('recto-folio', '{pageNumber}', 'odd', 'top-right', -OUTER, folioNumber),
] };
const footer = { elements: [head('drop-folio', '{pageNumber}', 'all', 'bottom', 0,
{ ...folioNumber, pages: 'opener', placement: { anchor: { to: 'page', edge: 'bottom' },
offset: { y: mm(-11) } } })] };
#3 · 把文档打开成书
代码就是上面的简短回答。createFolioFromDocument接收一个容器和排好的文档:它用renderPageToCanvas按页位的确切尺寸绘制每一页,只画打开的跨页附近几页,并把书放进容器。容器必须有自己的高度,否则书没有空间。textureBaseUrl指向postext.dev上拍摄的桌面;不设它,胡桃木就用程序生成的贴图来画。
完整食谱
// ═══ Postext Cookbook · Nº 100 · A laid-out book on the desk, in 3D ═══════════════════
// https://postext.dev/en/cookbook/folio-book-on-a-desk
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Literata, Fraunces, Work Sans (SIL OFL 1.1) · Needs postext ≥ 1.25.0
//
// A chapter of a short novel, laid out on a trade-book page and opened with postext-folio as a
// cloth-bound book lying on a walnut desk. Take the right-hand page by its edge and drag it
// over, click a page, or use the arrow keys. The flat pages follow under the book.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { createFolioFromDocument } from 'https://esm.sh/postext-folio';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'folio-book-on-a-desk';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
ink: '#211d1a', // text: a warm near-black
accent: '#2f5d50', // bottle green: kicker, rule, folios, and the cloth of the case
rule: '#c9bfae', // the colophon's hairline
muted: '#6d655c', // running heads and the colophon
cream: '#f7f0e1', // the book wove the pages are printed on (the 3D paper only)
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
{ id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
const [TRIM_W, TRIM_H, TOP, BOTTOM, INNER, OUTER] = [129, 198, 20, 22, 17, 15]; // mm: B-format
const LEAD = 14; // pt: the body leading
// #region folio: what the book is made of and where it lies; layout never reads it
const folio = {
tilt: 26, // degrees from straight above: enough to see the depth of the page block
paper: { type: 'bookWove', grammage: 70, shade: col('cream') }, // a bulky cream novel paper
binding: { type: 'hardcover', cover: 'case', coverMaterial: 'cloth',
coverColor: col('accent') }, // the case wraps the pages; boards a little larger
surface: { type: 'walnut' },
lighting: { environment: 'studio', intensity: 1, shadows: true },
};
// #endregion
// #region opener: a sunk chapter opening, kicker, title and a short rule
const opener = { enabled: true, minHeight: mm(50), slot: { elements: [
{ kind: 'text', id: 'kicker', content: '{attr.kicker}', fontFamily: 'Work Sans',
fontSize: pt(7.5), fontWeight: 600, letterSpacing: pt(1.5), textTransform: 'uppercase',
color: col('accent'), align: 'left',
组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)
变化
#白天光线下橡木桌上的平装书
改三个设置,同一章就成了浅色桌面上的软皮书;不需要重新排版。
- binding: { type: 'hardcover', cover: 'case', coverMaterial: 'cloth',
- coverColor: col('accent') }, // the case wraps the pages; boards a little larger
- surface: { type: 'walnut' },
- lighting: { environment: 'studio', intensity: 1, shadows: true },
+ binding: { type: 'paperback', cover: 'case', coverMaterial: 'paper',
+ coverColor: col('accent') },
+ surface: { type: 'oak' },
+ lighting: { environment: 'daylight', intensity: 1, shadows: true },#每张纸用不同的纸张
常见问题
易错点
Folio查看器取其容器的尺寸
createFolioFromDocument把书本放进你给它的元素中,按钮和页码显示在元素的边距里。没有高度的元素(正常文档流中的空div)什么也显示不出:要用CSS给它设定高度。 3D书本查看器(Folio) →
致谢
- 文本
- 原创文字, CC BY 4.0
- 字体
- Literata (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Work Sans (SIL OFL 1.1)


