かんたんな説明
日食を前にした天文講演会のポスターです。紙の端やほかの要素からの距離で、ひとつひとつの要素を置く方法を示します。
できあがり
架空の天文クラブが、2027年8月2日の皆既日食を前に木曜日ごとに開く4回の講演会のポスターです。A3の紙の上3分の2を群青の空が占めます。右側では黒い月がレモン色の太陽を覆い、太陽は仕上がり線で切れています。コードで描いたコロナが両者の背後に広がります。120ptのFunnel Displayで組んだ題『The Shadow Returns』は、空の下端でリード文の上に立ちます。地平線の下では青い4つの日付が講演の見出しになり、細かな注記が青い帯の上に2段で続きます。各要素は塗り足し、ページ、スロット、ほかの要素のいずれかに固定され、オフセットはそこから測ります。2枚目は同じポスターを繰り返し、各要素の箱とヘッダー・フッターのコンテナーをピンクの枠で囲み、13の要素にそれぞれの配置を印字したタグを付けています。
このレシピが答える質問
- デザイン要素を座標ではなく、ページ、裁ち落とし、またはほかの要素を基準に配置するには?
- すべてのページに透かし、背景の色、装飾画像を加えるには?
手短な答え
const SKY = 272; // mm from the trim's top edge to the horizon, the sky's lower edge
const INSET = BLEED + MARGIN; // from the bleed's edge to the text's edge
const AIR = 14; // mm between the standfirst and the horizon
const poster = () => ({ enabled: true, slot: { elements: [ // array order is paint order
// 'bleed' and 'page' are frames of the sheet; 'container' would be this heading's own box.
{ kind: 'box', id: 'sky', style: { backgroundColor: col('sky') }, // no width: fills the bleed
placement: at('bleed', 'top-left', 0, 0, { height: mm(BLEED + SKY) }) },
...eclipse(), // the Sun hangs from the trim's corner, the Moon and the corona from the Sun
// The headline stands on the horizon and grows upwards: 'above' puts an element's
// bottom-left corner on the top-left corner of its anchor, so a longer title lifts the kicker.
text('deck', '{attr.deck}', FACE.deck, { ...at('#sky', 'align-bottom', INSET, -AIR),
size: { maxWidth: mm(153) } }), // shrink-wraps its lines, but wraps at 153 mm
text('title', '{titleText}', FACE.title, at('#deck', 'above', 0, -9)), // breaks at the \\
text('kicker', '{attr.kicker}', FACE.kicker, at('#title', 'above', 0, -7)),
...programme(), // four columns chained off the horizon
] } }); // hook-up: headings.levels[0] = { span: 'page', breakBefore, advancedDesign: poster() }
// {titleText} is the heading's text; each {attr.*} is written on its line in content.md.
材料
- 機能
- デザイン要素の基準位置ページデザインのテキスト・罫・ボックスページデザインの画像デザインした章扉段抜きの章見出し帯裁ち落としの帯とツメ見出しスタイル節ごとの柱見出しの属性タイトル内の改行セマンティックカラーパレット段落スタイルリソースとしての図と表キャンバス上のページ
- 併用する機能
- 裁ち落としとトンボ
- 種類
- Funnel Display, Funnel Sans, Martian Mono (SIL OFL 1.1)
- 素材
- なし:画像はすべてコードで描画
作り方
#1 · 2つの要素を紙に固定し、残りをそこから吊るす
コードは前に掲げた手短な答えです。anchor.toは要素を何に固定するかを指定します。'bleed'と'page'は紙の枠(page.cutLinesがオフのあいだは同じ箱)、'container'はスロット自身の箱、'#id'はほかの要素です(要素の配置)。幅のない箱は枠の反対側の端まで伸びるので、空は塗り足しまで左右いっぱいに広がります。空を基準に置いた要素は空の左端、つまり塗り足しの左端から測るので、リード文はxにINSETの18mmを取ります。見出しまわりは空の下端から上へ積み上げます。'align-bottom'はリード文の箱の下端をその辺からAIRの14mm上に置き、'above'は各要素の下端を下の要素の上端に載せます。そのため題の行数が増えると、リード文を覆うのではなく見出し前置きが押し上げられます。デザインの下では、細かな注記が、いちばん下の要素である講演者の行から少なくとも3.2mm下にある最初の16ptのグリッド線から始まります。この3.2mmは見出しの下のアキの既定値です。
#2 · 月とコロナを太陽から吊るす
const SUN = 150; // mm across; the Moon is drawn the same size
const CORONA = 300; // mm, the square picture of the corona
const HALO = (CORONA - SUN) / 2; // how far the corona reaches past the Sun on every side
const BITE = { x: 2, y: 2.5 }; // mm the Moon sits off the Sun: a sliver is left at upper left
const disc = (id, colour, placement) => ({ kind: 'box', id, placement: { ...placement,
size: square(SUN) }, style: { backgroundColor: col(colour), borderRadius: mm(SUN / 2) } });
const eclipse = () => [
{ kind: 'image', id: 'corona', resourceId: 'corona', // listed before the discs: painted behind
placement: at('#sun', 'align-top', -HALO, -HALO, square(CORONA)) },
disc('sun', 'sun', at('page', 'top-right', 30, 25)), // 30 mm of it past the right edge
disc('moon', 'ink', at('#sun', 'align-top', BITE.x, BITE.y)),
];
ページ、つまり仕上がりの箱に固定した要素は太陽だけです。'top-right'の角から測ると、正のxは右端の外へ進むので、円盤の30mmが切れます。月とコロナは'align-top'で太陽に固定してあるので、太陽を動かせば両方がついてきます。オフセットは太陽の左上の角から測ります。月は右へ2mm、下へ2.5mmずれて左上に太陽の細い縁を残し、コロナはHALOの75mmだけ左上から始まって、300mm四方の画像を150mmの円盤の中心にそろえます。コロナは箱の大きさに合わせる画像要素で(画像要素)、一覧で円盤より前にあるため、円盤の背後に描かれます。
#3 · 講演の夕べを連ねて段にする
const GUT = 7; // mm between the evenings, and between the two columns of text under them
const COL = (A3.width - 2 * MARGIN - 3 * GUT) / 4; // four columns across the text width: 60 mm
const DROP = 12; // mm from the horizon to the dates
const programme = () => [1, 2, 3, 4].flatMap((n) => [
text(`day${n}`, `{attr.d${n}}`, FACE.day, n === 1
? at('#sky', 'below', INSET, DROP, wide(COL)) // the first date hangs from the horizon
: at(`#day${n - 1}`, 'right-of', GUT, 0, wide(COL))), // the others from the one before
text(`talk${n}`, `{attr.t${n}}`, FACE.talk, at(`#day${n}`, 'below', 0, 3, wide(COL))),
text(`who${n}`, `{attr.s${n}}`, FACE.who, at(`#talk${n}`, 'below', 0, 2.5, wide(COL))),
]);
日付、講演題目、講演者の各行にはCOLの固定のsize.widthがあり、'right-of'とGUTで次の日付を1段右に置きます。幅がないと日付は数字に合わせて縮み(「8」は幅17mm)、「15」はその7mm後から始まり、各講演題目は1行のまま次の段に食い込みます。COLは版面の幅を4つに分け、下の2段組みの注記と同じ7mmの段間を取ります。これで注記の2段目が「22」の下にそろいます。講演題目はそれぞれの日付の'below'に、講演者の行は題目の下に固定してあるので、題目が3行目に折り返しても押し下げられるのはその講演者の行だけです。
#4 · 帯をフッターに入れ、全ページに載せる
const MARK = 10; // mm, the club's mark
const PAD = (FOOT - MARK) / 2; // centres the mark in the bottom margin
const markTall = { height: mm(MARK) }; // as tall as the mark: the line is centred on it
const footer = { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('sky') }, // the bottom margin, bled
placement: at('bleed', 'bottom-left', 0, 0, { height: mm(BLEED + FOOT) }) },
// The container runs from the text down to the trim: 'bottom-*' counts up, 'top-*' down.
{ kind: 'image', id: 'mark', resourceId: 'mark',
placement: at('container', 'bottom-left', 0, -PAD, square(MARK)) },
text('club', '{attr.club}', FACE.foot, at('#mark', 'right-of', 3, 0, markTall)),
text('free', '{attr.free}', FACE.foot, at('container', 'top-right', 0, PAD, markTall)),
] };
この帯のように全ページに印刷するものは、ヘッダーかフッターのスロットに入れます。独自のヘッダーやフッターのある見出しスタイルは、その節のページでスロットを置き換えます。guidesスタイルのフッターはこの4つの要素を枠とタグ付きで繰り返すので、帯とマークは2枚とも印刷されます。フッターのコンテナーは地の余白、つまり版面から仕上がりまでです。'bottom-left'は仕上がりから上へ、'top-right'は版面から下へ数えるので、マークの−10と「Free entry」の行の+10は帯の中央の同じ10mmの帯域に収まります(ヘッダーとフッター)。フッターは本文の後に描かれ、本文の場所を取りません。そこでFOOTで地の余白と帯の高さを両方決め、帯がその上の行にかからないようにします。
#5 · 同じ設定から補助線を描く
const GUIDE = { borderColor: col('guide'), borderWidth: pt(1) };
const signed = ({ value }) => `${value < 0 ? '−' : '+'}${Math.abs(value)}`; // all offsets in mm
const describe = ({ id, placement: { anchor: { to, edge }, offset = {} } }) => [`#${id}`,
`${edge} ${to}`, // the two words of anchor: { to, edge }
['x', 'y'].filter((k) => offset[k]?.value).map((k) => `${k} ${signed(offset[k])}`).join(' '),
].filter(Boolean).join(' · ');
// A border never changes an element's size (padding does), so each frame traces its box.
// An image or a rounded box gets a square frame of its size, hung from its top-left corner.
const frame = (el) => (el.kind === 'text' ? [{ ...el, box: { ...el.box, ...GUIDE } }]
: el.kind === 'box' && !el.style.borderRadius ? [{ ...el, style: { ...el.style, ...GUIDE } }]
: [el, { kind: 'box', id: `${el.id}-box`, style: GUIDE,
placement: at(`#${el.id}`, 'align-top', 0, 0, el.placement.size) }]);
const tag = (el, [edge, x = 0, y = 0, note]) => text(`${el.id}-tag`, note ?? describe(el),
face('Martian Mono', 8.5, 500, 'ink', { box: { backgroundColor: col('guide'),
padding: { top: pt(1.6), bottom: pt(1.2), left: pt(3), right: pt(3) } } }),
at(`#${el.id}`, edge, x, y));
const guides = (elements, tags) => [...elements.flatMap(frame),
...elements.filter((el) => tags[el.id]).map((el) => tag(el, tags[el.id]))];
const container = (id) => ({ kind: 'box', id, style: GUIDE, // the slot's own box, drawn
placement: at('container', 'top-left', 0, 0, { width: 'fill', height: 'fill' }) });
const TAGS = { // where each tag sits against its element: [edge, x, y, text]
sky: ['align-bottom', INSET, -3], corona: ['align-bottom', 90, -2], sun: ['above'],
moon: ['below', 20, 2], kicker: ['above'], title: ['above'], deck: ['above'],
day1: ['above', 0, -6], day2: ['above'], who1: ['below'],
mark: ['above', 0, -2], club: ['above', 69, -2], free: ['above', -30, -2],
header: ['align-bottom', 0, -2, t({ en: 'header container', es: 'contenedor de la cabecera' })],
footer: ['align-bottom', 0, -2, t({ en: 'footer container', es: 'contenedor del pie' })],
};
guides()はポスターの要素をたどります。テキストには箱に、四角い箱にはスタイルに枠線を付けます。どちらも何も動かしません。箱を大きくするのは内側余白だけだからです。画像や円盤には、同じ大きさの四角い箱を左上の角に固定します。これで月とコロナの固定先である太陽の角も描かれます。各タグはその要素に固定したテキストで、describe()がその要素自身のplacementから書き出すので、タグが表す設定と食い違うことはありません。2つ目の見出しに付けた{style="guides"}が、このデザインと独自のヘッダー・フッターを呼び込みます(見出しスタイル)。幅のないタグが使えるのは、アンカーからコンテナーの右端までの範囲だけです。#freeのタグがテキストより30mm左から始まるのはそのためです。内容のcontinuationにあるpageIndexOffset: 1はポスターを偶数ページにするので、ビューアーは2枚を見開きで並べます。
レシピの全体
// ═══ Postext Cookbook · Nº 029 · Anchoring cheat sheet: a poster built from chained elements ═══
// https://postext.dev/en/cookbook/anchoring-cheat-sheet
// Code: MIT · Text: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Funnel Display, Funnel Sans, Martian Mono (SIL OFL 1.1) · Needs postext ≥ 1.34.0
// An A3 poster whose elements hang from the bleed, the page, their slot or one another, never
// from coordinates; the second sheet is the same poster with each element framed and tagged.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'anchoring-cheat-sheet';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
ink: '#0b0b0c', // text, and the Moon
sky: '#1b3bff', // the sky, the foot band and the dates
sun: '#ffe53b', // the Sun and the kicker
paper: '#ffffff', // type on the sky
muted: '#5c5f66', // speakers and the colophon
guide: '#ff2d9b', // frames and tags on the construction sheet
};
// col(id): a colour linked to its palette entry, so a swapped palette recolours the sheet.
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: 'sky (defaults)', value: { hex: palette.sky, model: 'hex' } },
];
const A3 = { width: 297, height: 420 }; // mm, the trim
const MARGIN = 18; // mm at the top and sides: the body's edges, and the header container's
const FOOT = 30; // mm, the bottom margin: the footer container and its band
const BLEED = 0; // mm; 3 for the printer, which switches on page.cutLines below
// at(): a placement. Offsets are distances from the anchor point, never page coordinates.
const at = (to, edge, x = 0, y = 0, size) => ({ anchor: { to, edge },
offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const wide = (w) => ({ width: mm(w) }); // fixed, so a text is as wide as its column
const square = (d) => ({ width: mm(d), height: mm(d) });
// Design text is centred by default, and in a footer it is cut with '…': these wrap, set left.
const text = (id, content, face, placement) => ({ kind: 'text', id, content, overflow: 'wrap',
align: 'left', ...face, placement });
const face = (fontFamily, size, fontWeight, colour, more) => ({ fontFamily, fontSize: pt(size),
fontWeight, color: col(colour), ...more });
const caps = (tracking) => ({ letterSpacing: pt(tracking), textTransform: 'uppercase' });
const FACE = { // lineHeight as a multiple of each size
kicker: face('Martian Mono', 12, 500, 'sun', caps(1.8)),
title: face('Funnel Display', 120, 800, 'paper', { lineHeight: 0.86 }),
deck: face('Funnel Sans', 22, 400, 'paper', { lineHeight: 1.22 }),
組み立てたscript.jsはそのまま動きます。任意のページのモジュールスクリプトに貼り付けるか、レシピをCodePenで開いてください。 GitHub上のレシピのフォルダー ↗ (新しいタブで開きます)
アレンジ
#3mmの裁ち落としとトンボを付ける
BLEEDを3mmにするとトンボが有効になり、空と下端の帯は固定先の塗り足しの中へ3mm押し出されます。INSETがあるので文字は余白の位置にとどまります。太陽、月、コロナはページに固定されていますが、ページに描かれるものはすべて塗り足しの範囲で切り取られるため、太陽は仕上がり線の3mm外で終わり、その外側にはトンボだけが残ります。
-const BLEED = 0; // mm; 3 for the printer, which switches on page.cutLines below
+const BLEED = 3; // mm; 3 for the printer, which switches on page.cutLines below#地平線を上げる
SKYから20mm引くと空の下端が上がり、それに連れてリード文、題、見出し前置き、講演プログラムの12のテキストも上がり、細かな注記は1本上のグリッド線から始まります。太陽はページに固定しているので、2つ目の変更で同じく20mm持ち上げます。そうしないと「Shadow」の「w」が三日月に触れてしまいます。
-const SKY = 272; // mm from the trim's top edge to the horizon, the sky's lower edge
+const SKY = 252; // mm from the trim's top edge to the horizon, the sky's lower edge
- disc('sun', 'sun', at('page', 'top-right', 30, 25)), // 30 mm of it past the right edge
+ disc('sun', 'sun', at('page', 'top-right', 30, 5)), // 30 mm of it past the right edgeよくあるつまずき
つまずき
ヘッダーとフッターの要素は本文の上に描かれる
ヘッダーとフッターの要素はページの上に重ねて描かれ、本文の領域はそのための場所を空けません。要素は余白の中に収めてください。その場所を確保するのは余白です。 柱とノンブル →
つまずき
属性値には{も}も書けない。"を含む値は一重引用符で囲む
属性値は閉じ波かっこで終わるため、{や}を含められません。二重引用符を含む値は一重引用符で囲みます。ドル記号は問題ありません。 見出しの属性 →
Sandboxの検査 · designCyclicAnchor
循環するアンカー参照
原因. デザイン要素どうしが循環するように互いに固定されているため、どの要素も配置できません。
対処. 連鎖の中の1つの要素を、コンテナー、ページ、または塗り足しに固定してください。 ドキュメント →
Sandboxの検査 · designDanglingAnchor
参照先のないアンカー
原因. デザイン要素の固定先の#idが、そのスロットのどの要素にもありません。
対処. idを修正するか、参照先の要素を追加してください。 ドキュメント →
- CodePenのサンプルでは、循環したアンカーも存在しない先を指すアンカーも報告されません。Sandboxの検査パネルは、ヘッダー、フッター、部扉、見出しレベルにあるものは一覧にしますが、
guidesのような見出しスタイルの中のものは載せません。存在しない#idに固定した要素はコンテナーを基準に置かれます。'below'のような要素の辺はオフセット込みでコンテナーの左上の角に、'top-right'のようなコンテナーの辺はその角に戻ります。循環している場合は、1つの要素が同じように戻り、ほかの要素はそこから連なります。 'align-left'と'align-top'は同じ点に置かれます。どちらも要素の左上の角をアンカーの左上の角に合わせます。- 枠線は箱の辺を中心に描かれるため、1ptのピンクの枠はなぞる箱の外へ0.5ptはみ出します。設定のドキュメントには線は内側に描かれるとありますが、実際はそうなりません。
クレジット
- 本文
- 書き下ろしの文章, CC BY 4.0
- フォント
- Funnel Display (SIL OFL 1.1) · Funnel Sans (SIL OFL 1.1) · Martian Mono (SIL OFL 1.1)


