Functions and modules#
Purpose and concepts#
function encapsulates computation or material composition. Component .lay files export variables/functions; the entry imports selected names and may rename them.
Minimal complete example#
Run this file directly with laymesh validate or laymesh render; it contains its own canvas and required definitions.
# Minimal complete example: grouppage=canvas(size=(100mm,75mm),background="#ffffff")g=group()a=g.add(rect(size=(35mm,20mm),fill="#e6f2f3"))g.add(text("Reused"),target=a.center,anchor=center)page.add(g,offset=(8mm,8mm))page.add(g,offset=(55mm,38mm),rotation=15deg)# Minimal complete example: grouppage=canvas(size=(100mm,75mm),background="#ffffff")g=group()a=g.add(rect(size=(35mm,20mm),fill="#e6f2f3"))g.add(text("Reused"),target=a.center,anchor=center)page.add(g,offset=(8mm,8mm))page.add(g,offset=(55mm,38mm),rotation=15deg)Preview
Dependencies
examples/manual/group.lay
Parameters and default behavior#
Unitless geometry uses the canvas unit; unitless type and stroke sizes use pt. Explicit call parameters override inherited/theme defaults. The linked interface reference lists accepted types, choices and defaults per parameter.
Composition#
Functions may accept sizes, titles or cap variables and return material or groups. Predefined variables also work in modules, with user/module bindings taking priority. Resource paths are relative to the module file.
first = page.add(card("A"), target=page.top_left, offset=(8 mm, 29 mm))page.add(card("B"), target=first.top_right, offset=(13 mm, 0 mm))# Gallery: containers / module-importimport { card } from "./card-component.lay"page = canvas(name="Imported component", size=(120 mm, 80 mm), background="#f7f9fc")font = "DejaVu Sans"heading = text(content="Imported component", font_family=font, font_size=14 pt, color="#203864")page.add(heading, target=page.top_left, offset=(7 mm, 5 mm))# BEGIN DEMOfirst = page.add(card("A"), target=page.top_left, offset=(8 mm, 29 mm))page.add(card("B"), target=first.top_right, offset=(13 mm, 0 mm))# END DEMO## @lang zh-CN## 创建圆角卡片,并将标题放在中央。## @param {string} label - 标题文字;支持行内公式。## @returns {group} 可重复放置的卡片,宽 42mm、高 31mm。## @example## item = card("结果")## @lang en## Create a rounded card with a centered caption.## @param {string} label - Caption text; supports inline mathematics.## @returns {group} Reusable card material, 42mm wide and 31mm high.## @example## item = card("Result")export function card(label) { item = group() plate = item.add(rect(size=(42 mm, 31 mm), border_radius=3 mm, fill="#d8f0ec", border_color="#087f8c", border_width=0.5 mm), target=item.top_left) item.add(text(content=label, font_family="DejaVu Sans", font_size=11 pt, color="#203864"), anchor=center, target=plate.center) return item}Preview
Dependencies
examples/gallery/containers/card-component.layexamples/gallery/containers/module-import.lay
Common errors and limits#
Imports are read-only; cycles and missing exports error. Modules do not create or draw directly on the entry canvas. Create the entry canvas before using imported components.
Individual functions#
Detailed behavior and further examples#
Parameters#
| Parameter | Purpose | Default or requirement |
|---|---|---|
import |
Import local components | Path relative to importing file |
function |
Define reusable functions | Components do not create an entry canvas |
Common usage#
Imports must be local relative .lay paths. Modules use export for constants, materials, groups, or functions and may import further modules; imported names are read-only. The entry canvas must exist before imported values are used. A component module cannot create or draw on the entry canvas. Missing exports and import cycles are errors. See scripted.lay and card.lay.
Limits and related topics#
Variables and expressions · Conditions and loops
Library documentation comments explain how to attach function and parameter help to your reusable components.
workflow#
Complete sources and executable verification fixtures for this workflow are listed in the feature coverage map. Follow this page’s input conditions and limits when composing features.

