本文へスキップ
レシピ番号100

レシピ集 · 第10章 · 出力と組み込み

組版した本を机の上に3Dで開く

postext-folioパッケージのcreateFolioFromDocumentが、組版した章をクロス装の本としてクルミ材の机の上に開きます。見た目はconfig.folioで決めます。

このページの内容
レベル
中級
Postext
Postext 1.65.0で検証済み
1.25.0以上が必要
ライセンス
2026年10月10日更新
コードMIT · 本文CC BY 4.0

1ページ(全4ページ)

  • 英語の見本:日本語版はまだありません
  • 仕上がり129 × 198 mm
  • 1段
  • Literata 9.8/14
  • Fraunces
  • Work Sans
  • 4ページ
  • レベル
  • Postext 1.57.1
  • 組版時間49 ms
  • コード119行

かんたんな説明

短い小説の第1章を、机の上に開いて置かれた本物の本として見せます。ページはマウスでめくり、紙、表紙、机は設定で選びます。

できあがり

129 × 198mmのページの短い小説『The Ferryman’s Ledger』の第1章を、クルミ材の机の上の本として開きます。表紙はボトルグリーンのクロス装、本文用紙はかさ高のクリーム色の小説用紙です。読者は右ページの端をつまんでめくり、紙は丸まりながら下のページに影を落とします。本文はbuildDocumentで一度だけ組み、postext-folioパッケージを1回呼び出せば、そのレイアウトが本になります。紙、製本、机、光、視点の角度と、本を形づくるものはすべて設定の一部なので、同じ文書はSandboxのFolioタブでも同じ見た目になります。

このレシピが答える質問

  • 文書を、読者が手でページをめくる3Dの本として表示するには?
  • 入稿の前に、本の綴じ、表紙、用紙、照明をプレビューするには?

手短な答え

script.js · 174〜185行コード全体で見る
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('–')),
});

材料

種類
Literata, Fraunces, Work Sans (SIL OFL 1.1)
素材
なし:画像はすべてコードで描画

作り方

#1 · 本の材料を指定する

script.js · 34〜41行コード全体で見る
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の出力はこれを無視するので、紙を変えても行は1本も動きません。bookWoveはかさ高の小説用紙で、このかさ高で70g/m²なら1枚の厚さは約112µmです。ビューアーはこれにページ数を掛けて束の厚さを決めます。クロスの色はパレットのアクセントにつながっていて、キッカー(題の上の小さな文字)やノンブルと同じ緑です。

#2 · 控えめな章扉と柱

script.js · 45〜57行コード全体で見る
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) } } },
] } };

章は奇数ページの上から3分の1下がった位置で、見出しの属性から取ったキッカーの下で始まります。3Dでも、下げて組んだ章扉は印刷された小説と同じように読めます。表紙を開いたとき読者が最初に目にするのは、ページの上部の余白です。

script.js · 61〜74行コード全体で見る
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にある机の写真を指します。これがないと、クルミ材はプロシージャルなマップで描かれます。

レシピの全体

Sandbox
// ═══ 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上のレシピのフォルダー ↗ (新しいタブで開きます)

アレンジ

#昼の光の中、オーク材の机にペーパーバック

設定を3つ変えると、同じ章が淡い色の机の上のソフトカバーになります。組み直しは起こりません。

-  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 },

#丁ごとに別の紙

紙の見本帳は:::paperで丁ごとに別の用紙に刷り、設定の試験台はsetAppearanceですべての設定をその場で変えます。

よくあるつまずき

つまずき

Folioビューアーはコンテナーの大きさに合わせる

createFolioFromDocumentは、渡した要素の中に本を収め、余白にボタンとページ数を置きます。高さのない要素(通常フローの空のdiv)では何も表示されないので、CSSで高さを与えてください。 3Dの本のビューアー(Folio) →

クレジット

レシピ
Ignacio Ferro
本文
書き下ろしの文章, CC BY 4.0
フォント
Literata (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Work Sans (SIL OFL 1.1)
Sandbox