Skip to main content
Recipe number 100

Cookbook · Chapter 10 · Output & integration

A laid-out book on the desk, in 3D

createFolioFromDocument from the postext-folio package opens a laid-out chapter as a cloth-bound book on a walnut desk, styled by config.folio.

  • Trim 129 × 198 mm
  • 1 column
  • Literata 9.8/14
  • Fraunces
  • Work Sans
  • 4 pages
  • Level
  • Postext 1.57.1
  • Laid out in 49 ms
  • 119 lines of code

In short

The first chapter of a short novel, shown as a real book lying open on a desk. You turn its pages with the mouse, and the paper, cover and desk are chosen in the settings.

What you'll build

The first chapter of The Ferryman’s Ledger, a short novel on a 129 × 198 mm page, opened as a book on a walnut desk. The case is bottle-green cloth, the pages are a bulky cream novel paper, and the reader takes the right-hand page by its edge and turns it, the leaf curling and casting its shadow on the page below. The text is set once with buildDocument; one call to the postext-folio package turns that layout into the book. Everything the book is made of (paper, binding, desk, light, the angle of the view) is part of the config, so the same document carries its look into the Sandbox’s Folio tab.

This recipe answers

  • How do I show my document as a 3D book whose pages the reader turns by hand?
  • How do I preview a book's binding, cover, paper and lighting before it goes to print?

The short answer

script.js · lines 174–185in full code
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('–')),
});

The document opened as a book; the container gives it its size

Ingredients

Type
Literata, Fraunces, Work Sans (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · Say what the book is made of

script.js · lines 34–41in full code
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 is read by the 3D viewer and by nothing else: the layout, the canvas, the PDF and the HTML output ignore it, so changing the paper never moves a line. bookWove is a bulky novel paper, and 70 g/m² at its bulk makes a sheet about 112 µm thick; the viewer multiplies that by the page count for the depth of the page block. The cloth colour links to the palette's accent, the same green as the kicker and the folios.

#2 · A quiet opener and running heads

script.js · lines 45–57in full code
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) } } },
] } };

The chapter opens a third of the way down a recto, under a kicker taken from the heading's attributes. In 3D a sunk opener reads as it does in a printed novel: the white space at the head of the page is the first thing the reader sees when the case is opened.

script.js · lines 61–74in full code
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 · Open the document as a book

The code is the short answer above. createFolioFromDocument takes a container and the laid-out document: it paints each page with renderPageToCanvas at the exact size of a page slot, only around the open spread, and fits the book into the container. The container needs a height of its own, or the book has no room. textureBaseUrl points at the photographed desks on postext.dev; without it the walnut is drawn with procedural maps.

The whole recipe

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

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

#A paperback on oak in daylight

Change three settings and the same chapter is a softcover on a pale desk; nothing is laid out again.

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

#Every leaf on another paper

A swatch book prints each leaf on its own stock with :::paper, and the settings bench changes every setting live with setAppearance.

Pitfalls

Pitfall

The Folio viewer takes the size of its container

createFolioFromDocument fits the book into the element you give it, with the buttons and the page count in its margins. An element with no height (an empty div in normal flow) shows nothing: give it a height in CSS. 3D book viewer (Folio) →

Credits

Text
Original prose, CC BY 4.0
Fonts
Literata (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Work Sans (SIL OFL 1.1)
Sandbox