Expand description
Draw a picture by describing it, not by issuing commands.
use meo_canvas::{
Column, Image, Row, Styled, Text, all, hex_rgb, px, scene::ObjectFit,
};
let card = Row::new()
.gap(px(16.0))
.padding(all(px(24.0)))
.background_color(hex_rgb(0x10_10_14))
.children([
Image::path("avatar.png")
.size(px(64.0), px(64.0))
.object_fit(ObjectFit::Cover),
Column::new().children([
Text::new("Ukasyah").font_size(24.0).bold(),
Text::new("Bandung").color(hex_rgb(0x88_88_90)),
]),
]);
let (scene, _diagnostics) = card.into_scene(360.0, 112.0)?;Properties are named on the node, flat, as CSS names them and as the
JavaScript surface’s props spell them. That is the path this crate is
taught in: the setters come from Styled, which every node implements,
so the import is one trait rather than a type per node.
A node carries a constructor, its properties, and its children. The method
set never grows per node: a new property is a new entry in one table, not a
method on seven node types. Each node’s essential argument is a constructor
parameter – Text::new takes the content, Image::path the source,
Path::d the data – so it cannot be forgotten.
§One flat style
Style is one type, not four. Authoring is flat because CSS is flat: a
reader never has to know which group gap lives in versus
background_color. The scene keeps them grouped because the codec needs
them grouped, and Style::into_parts splits at the moment the tree
becomes a scene.
The names and the behaviour are CSS’s. Style::color is the inherited
text colour and Style::background_color is the fill; the two sit
adjacent and mean different things, which is CSS’s trap and not one invented
here. Keeping it is what lets a design be ported without translation.
§A style as a value, for the base a design reuses
The flat setters name one property at a time, which is the wrong shape for
a base that many nodes share. Style carries the same setters as
const fns, so that base is a const, and
with_style layers it onto a node:
use meo_canvas::{ColorValue, Row, Style, Styled, all, hex_rgb, px};
const CARD: Style = Style::new().padding(all(px(24.0))).gap(px(16.0));
let dark = Row::new().with_style(
CARD.background_color(ColorValue::Rgba(hex_rgb(0x10_10_14))),
);
let light = Row::new()
.with_style(CARD)
.background_color(hex_rgb(0x1c_1c_22));with_style merges: what the style names wins, what it leaves absent the
node keeps. So the two idioms compose in either order, and neither erases
the other – light above is a card that is also a row, and would be
whichever way round those two lines were written.
A const is substituted at each use, so every CARD is a fresh value a
self-taking setter may consume. No clone and no lifetime. A property
holding a String or a Vec cannot be const, because assigning one drops
what it replaces; a function returning a Style serves the same purpose
there.
Every field is public, so a property with no setter is still reachable:
use meo_canvas::{Style, px};
let golden = Style {
aspect_ratio: Some(1.618),
..Style::new().gap(px(8.0))
};§Chains and literals
A tree is chained and a bag of options is a literal, and the difference is not a matter of taste.
A node’s children nest, and each carries a style of its own, so the shape of
the call has to be the shape of the picture – Row::new().children([..])
reads as the row it describes where the same tree written as a literal
reads as bookkeeping. Style chains for a second reason: the properties
are optional and independent, so a chain names the handful a caller set
rather than defaulting the sixty they did not.
Options are struct literals closed with ..Default::default(). A literal
shows every value the caller chose in one place, and there is no ordering
between options and no invalid intermediate state for a builder to prevent.
Style takes both, which is why its fields are public and why it is not
#[non_exhaustive]: the chain covers the properties with setters, and the
literal reaches the ones without. The rest pattern is what keeps such a
literal compiling when a field is added.
§What this crate deliberately excludes
No Skia and no taffy in any signature, and neither as a direct dependency.
Both live behind meo-canvas-core. A caller who upgrades either one does so
on that crate’s schedule, and a caller who reads this crate’s documentation
never has to learn two other vocabularies to place a rectangle.
No async runtime, and no HTTP client unless you ask for one. An
ImageSource::Url is Error::UnresolvedSource by
default, and the net feature makes it a request instead – the client
beneath is blocking by construction, so enabling it puts no runtime in any
consumer. HttpOptions sets headers per source, so a
page pulling from two origins sends each one only what belongs to it.
Off by default is the same reach at a different price. The JavaScript surface fetches without a flag because its addon ships the client prebuilt; here the consumer compiles it, and the flag lets whoever pays for the dependency decide. Most never do: a scene built from bytes or paths names no URL.
No mutable drawing context. There is no move_to/line_to state machine
here – that API already exists, in meo-skia-canvas, and reproducing it
would give the workspace two answers to the same question.
Re-exports§
pub use element::Box;pub use element::Column;pub use element::Each;pub use element::Element;pub use element::Grid;pub use element::Image;pub use element::IntoElements;pub use element::Path;pub use element::Row;pub use element::Text;pub use element::each;pub use root::BuildError;pub use root::Canvas;pub use root::PageInfo;pub use root::Root;pub use root::SequenceError;pub use style::Style;pub use style::Styled;pub use unit::DefaultZero;pub use unit::IntoCorners;pub use unit::IntoSides;pub use unit::all;pub use unit::auto;pub use unit::bottom;pub use unit::corners;pub use unit::corners_all;pub use unit::fr;pub use unit::fraction;pub use unit::hex;pub use unit::hex_rgb;pub use unit::hex_rgba;pub use unit::left;pub use unit::pct;pub use unit::px;pub use unit::rgb;pub use unit::rgba;pub use unit::right;pub use unit::sides;pub use unit::size_auto;pub use unit::top;pub use unit::track;pub use unit::xy;
Modules§
- animate
- The animation helpers, as a module rather than a curated list.
- chart
- Charts, built out of layout rather than draw calls.
- element
- The nested tree a caller writes, and its flattening into the scene’s arena.
- root
- The entry point, and the canvas a render gives back.
- scene
- The scene vocabulary, re-exported so callers need one dependency rather than two.
- style
- One flat style, as CSS writes one.
- unit
- The values a style is written in: lengths, edges, corners and colours.
Structs§
- Color
- A non-premultiplied sRGB colour with an alpha channel.
- Encode
Options - Quality and container settings that only apply to some formats.
- Font
Weight - A font weight on the CSS numeric scale.
- Grid
Placement - Where a grid item sits, as a track span.
- Image
Warning - One image source that could not be resolved, and what a render did about it.
- Prepared
Encode - A painted surface’s pages, resolved and ready to encode on any thread.
- Rendered
Canvas - A scene that has been drawn and not yet encoded.
- Renderer
- Everything a render needs that is not the scene itself.
Enums§
- Align
- Placement along the cross axis.
- Background
Repeat - How a background image tiles.
- Blend
Mode - How a node’s pixels combine with what is behind them.
- Border
Style - How a border’s line is drawn.
- BoxSizing
- Whether
widthandheightinclude padding and border. - Color
Space - The style keywords, at the crate root.
- Color
Type - The style keywords, at the crate root.
- Color
Value - A colour as a style states it: a colour, or CSS’s
currentColor. - Direction
- Inline direction, which decides which edge is the start.
- Display
- How a node’s children are arranged.
- Error
- Anything that can stop a render.
- Fetch
Failure - Why a fetch failed, in the terms a caller can act on.
- Fill
Rule - Which side of a path’s winding counts as inside.
- Flex
Direction - The axis children are placed along, and which end they start from.
- Flex
Wrap - Whether children overflow onto further lines.
- Font
Style - Upright or slanted glyphs.
- Font
Variant - One CSS
font-variantkeyword. - Format
- The container an encoded image is written in.
- Gradient
Kind - The shape a gradient’s stops are laid along.
- Grid
Auto Flow - The order the grid’s auto-placement algorithm fills tracks in.
- Justify
- Distribution of free space along the main axis.
- Justify
Items - CSS’s
justify-items: where this box’s grid items and block children sit on the inline axis, for each whosejustify-selfisauto. - Justify
Self - CSS’s
justify-self: where this box sits on the inline axis of its alignment container, overriding the container’sjustify-items. - Legacy
Position - The keyword after
legacyinjustify-items. - Line
Height - How tall a line box is, in the four spellings CSS gives it.
- Object
Fit - How an image fills its box.
- OnImage
Error - The style keywords, at the crate root.
- Overflow
- What happens to content larger than its box.
- Overflow
Position - CSS’s
<overflow-position>: what a positioned box does when it is wider than its alignment container. - Paint
Order - Which of a glyph’s fill and stroke is painted on top.
- Position
Type - Whether a node is placed by the flow or by its own offsets.
- Self
Position - CSS’s
<self-position>, withleftandright: whereJustifySelf::PositionandJustifyItems::Positionput a box on the inline axis. - Text
Align - Where a line of text sits across its width.
- Text
Decoration - A line drawn through, over or under text.
- Track
Size - One track of a grid template.
- Vertical
Align - Where the text sits within the node’s box.