Repository navigation
Images SVG
Blinc supports raster images and SVG graphics. You can set their size and style.
use blinc::layout::image::image;
image("path/to/photo.png")
.w(200.0)
.h(150.0)image("https://example.com/image.jpg")
.w(300.0)
.h(200.0)To load an image from a URL, your app needs the network feature of blinc_image. Add it to your
Cargo.toml:
blinc_image = { version = "0.6", features = ["network"] }Set how the image fills its container:
image(src)
.w(200.0)
.h(200.0)
.cover() // Fill container, crop if needed (default)
image(src)
.contain() // Fit entirely, may letterbox
image(src)
.fill() // Stretch to fill exactly
image(src)
.scale_down() // Scale down only if larger
image(src)
.no_scale() // No scaling, original sizeSet the alignment of the image in its container:
image(src)
.cover()
.center() // Center (default)
image(src)
.cover()
.top_left()
image(src)
.cover()
.top_center()
image(src)
.cover()
.bottom_right()
// Custom position (0.0 to 1.0)
image(src)
.cover()
.position_xy(0.25, 0.75)image(src)
.w(200.0)
.h(200.0)
.grayscale(0.5) // 0.0 = color, 1.0 = grayscale
.sepia(0.3) // Sepia tone
.brightness(1.2) // > 1.0 brighter, < 1.0 darker
.contrast(1.1) // > 1.0 more contrast
.saturate(0.8) // < 1.0 less saturated
.hue_rotate(45.0) // Rotate hue (degrees)
.invert(0.2) // Color inversion
.blur(2.0) // Blur radiusLazy loading is for content-heavy apps with many images, such as galleries, feeds, and chat. It delays decode until the image is within 100 px of the visible area. While the real bitmap loads, Blinc renders a placeholder in its place. After the texture is in the cache, the image fades in. You can set the duration of the fade.
- On each frame, the renderer compares every lazy image with the visible area, plus 100 px on each side. Inside a scroll container, the visible area is that of the container. The renderer skips the other images, with no decode and no GPU upload.
- When an image comes into that area for the first time, Blinc starts the loader. It draws a placeholder at the final layout rect of the image.
- As soon as the decoded texture is in the GPU cache, Blinc records
image_load_times[source] = Instant::now(). Then it starts the fade-in. The alpha multiplier iselapsed_ms / fade_duration_ms. When the multiplier reaches1.0, the image is fully opaque and Blinc clears the fade-in flag. - Blinc preloads placeholder images (type 2) eagerly. They are available as soon as the lazy element appears. You see no flash of empty space while you wait for the thumbnail itself.
- While any image fades in, the runtime continues to request redraws. The animation thus runs smoothly, even on a screen where nothing else changes.
use blinc::core::{Brush, Gradient, Point};
use blinc::layout::prelude::*;
use std::time::Duration;
// Basic lazy loading, with the default gray placeholder
img("large-photo.jpg")
.lazy()
.w(300.0)
.h(200.0)
// Solid color placeholder
img("photo.jpg")
.lazy()
.placeholder_color(Color::rgba(0.2, 0.2, 0.2, 1.0))
.w(300.0)
.h(200.0)
// Gradient placeholder, with coordinates in the pixels of the image box
img("photo.jpg")
.lazy()
.placeholder_brush(Brush::Gradient(Gradient::linear(
Point::new(0.0, 0.0),
Point::new(300.0, 0.0),
Color::rgb(0.15, 0.15, 0.2),
Color::rgb(0.3, 0.3, 0.4),
)))
.w(300.0)
.h(200.0)
// Thumbnail / blur-hash placeholder — eagerly preloaded
img("large-photo.jpg")
.lazy()
.placeholder_image("thumbnail.jpg")
.fade_in(Duration::from_millis(300))
.w(300.0)
.h(200.0)
// Skeleton shimmer (animated band sweeping left → right)
img("photo.jpg")
.lazy()
.skeleton()
.fade_in(Duration::from_millis(250))
.w(300.0)
.h(200.0)
// Cross-fade off — image pops in instantly when ready
img("photo.jpg")
.lazy()
.no_fade()
.w(300.0)
.h(200.0)| Strategy | Description |
|---|---|
Eager (default) |
Load and decode the image immediately at element creation |
Lazy |
Delay the load until the image is within 100 px of the visible area |
| Placeholder | Description |
|---|---|
None |
No placeholder. The area stays empty until the bitmap arrives |
Color(color) |
Solid color background. The default is a translucent gray |
Brush(brush) |
A gradient fills the image box. An image brush shows its image. A glass or blur brush blurs the content behind the box |
Image(url) |
Another image (a low-res thumbnail or a blur hash). Preloaded on tree build |
Skeleton |
Shimmer band animation that needs no asset |
Content that you draw after the image stays above a glass or blur placeholder. The placeholder shows its tint color only in one place. That place is a container in a 3D layer whose position, scale or rotation follows an animated value.
If an image fails to load, it shows its placeholder, and Blinc tries to load it again. Examples of a failure are a missing file and a request that returns an error. The first retry comes after half a second. Each later wait is twice the last wait, but never longer than half a minute. .retry(..) sets this behavior for each image, and .no_retry() turns it off. If hot reload sees a change to the file behind an image, Blinc tries that image again at once.
use blinc::layout::prelude::*;
use std::time::Duration;
// Try again after 1 s, 2 s, 4 s, then every 5 s, five times at most
img("https://example.com/photo.jpg")
.retry(ImageRetry::backoff(Duration::from_secs(1), Duration::from_secs(5)).attempts(5))
// A failed load stays failed
img("optional-badge.png").no_retry()If a retry is due while the window is idle, it runs on the next frame that the window draws.
A stylesheet can override lazy-loading behavior, with no change to the builder. Use this to theme galleries, or to apply defaults to all images that match a class.
.gallery-item {
loading: lazy;
image-placeholder-type: skeleton;
fade-duration: 250ms;
}
.avatar {
loading: lazy;
image-placeholder-color: rgba(40, 40, 50, 1.0);
fade-duration: 200ms;
}
.hero {
loading: lazy;
image-placeholder-image: "hero-blur.jpg";
fade-duration: 400ms;
}| Property | Values | Effect |
|---|---|---|
loading |
eager | lazy
|
Switches the loading strategy |
image-placeholder-type |
none | color | skeleton
|
Selects which placeholder to draw |
image-placeholder-color |
<color> |
Solid color placeholder. Implies type: color
|
image-placeholder-image |
<url> |
Thumbnail placeholder. Implies type: image
|
fade-duration |
<time> (200ms, 0.3s) |
Length of the fade-in after the bitmap is ready |
CSS values override builder values. Use the builder for one-off images and CSS for repeated patterns.
Note: Lazy loading currently applies to raster images only. Blinc vectorizes and rasterizes SVGs on demand, so a delay of their decode is rarely worth the cost. If you need a deferred SVG, wrap it in a
Stateful, and reveal it on demand.
You can render emoji as images at any size with the system emoji font. To save memory, Blinc lazy-loads emoji images automatically.
use blinc::layout::image::{emoji, emoji_sized};
// Default size (64px)
emoji("😀")
// Custom size
emoji_sized("🚀", 128.0)
// In a layout
div()
.flex_row()
.gap(2.0)
.child(emoji_sized("👍", 32.0))
.child(emoji_sized("🎉", 32.0))
.child(emoji_sized("✨", 32.0))Emoji images use the system color emoji font (Apple Color Emoji on macOS, Segoe UI Emoji on Windows, Noto Color Emoji on Linux).
svg() takes SVG markup, not a file path. To use a file, read it first, for example with
include_str!("icons/menu.svg").
use blinc::layout::svg::svg;
const MENU: &str = r#"<svg viewBox="0 0 24 24">
<path d="M4 6h16M4 12h16M4 18h16" stroke="currentColor" stroke-width="2"/>
</svg>"#;
svg(MENU).size(24.0, 24.0)Apply a color tint to monochrome (single-color) SVGs:
svg(MENU)
.square(24.0)
.tint(Color::WHITE)
svg(MENU)
.square(20.0)
.tint(Color::rgba(0.9, 0.3, 0.3, 1.0))// Width and height
svg(MENU).size(32.0, 16.0)
// Square shorthand
svg(MENU).square(24.0)fn avatar(url: &str, size: f32) -> impl ElementBuilder {
image(url)
.w(size)
.h(size)
.cover()
.circular()
}use blinc::layout::stateful::stateful;
fn icon_button(icon: &'static str) -> impl ElementBuilder {
stateful::<ButtonState>().on_state(move |ctx| {
let bg = match ctx.state() {
ButtonState::Hovered => Color::rgba(0.2, 0.2, 0.25, 1.0),
ButtonState::Pressed => Color::rgba(0.15, 0.15, 0.2, 1.0),
_ => Color::TRANSPARENT,
};
div()
.w(40.0)
.h(40.0)
.rounded(8.0)
.items_center()
.justify_center()
.bg(bg)
.child(svg(icon).square(20.0).tint(Color::WHITE))
})
}fn image_card(image_url: &str, title: &str) -> impl ElementBuilder {
div()
.w(300.0)
.rounded(12.0)
.overflow_clip()
.bg(Color::rgba(0.15, 0.15, 0.2, 1.0))
.child(
image(image_url)
.w_full()
.h(180.0)
.cover()
)
.child(
div()
.p(4.0)
.child(
text(title)
.size(18.0)
.weight(FontWeight::SemiBold)
.color(Color::WHITE)
)
)
}fn gallery(images: &[&str]) -> impl ElementBuilder {
div()
.flex_row()
.flex_wrap()
.gap(2.0)
.children(images.iter().map(|url| {
image(*url)
.w(150.0)
.h(150.0)
.cover()
.rounded(8.0)
}))
}fn image_with_placeholder(url: Option<&str>, icon: &str) -> Div {
match url {
Some(src) => div().child(image(src).w(200.0).h(200.0).cover().rounded(8.0)),
None => div()
.w(200.0)
.h(200.0)
.rounded(8.0)
.bg(Color::rgba(0.2, 0.2, 0.25, 1.0))
.items_center()
.justify_center()
.child(svg(icon).square(48.0).tint(Color::rgba(0.4, 0.4, 0.5, 1.0))),
}
}PNG and JPEG work by default. For the other formats, add blinc_image with the matching feature to
your Cargo.toml:
- WebP:
webp - GIF (first frame):
gif - BMP:
bmp
- Standard SVG 1.1
- Path elements
- Basic shapes (rect, circle, ellipse, line, polyline, polygon)
- Transforms
- Fill and stroke
-
Set explicit dimensions. An image without a size is 100 by 100 px.
-
Use
coverfor photos. It fills the container well, without distortion. -
Use
containfor diagrams. It makes sure that nothing is cropped. -
Tint icons. Use
.tint()to match your color scheme. -
Use SVG for icons. SVG scales perfectly to any size.
-
Optimize images. Use suitable formats and compression for the web.
-
Use lazy loading for galleries. In scroll containers with many images, use
.lazy(). It reduces memory usage and improves the initial load time. -
Use emoji images for large emoji. For crisp rendering of emoji larger than ~24px, use
emoji_sized()instead of text.
Getting Started
Migrating
Mobile Development
Example Gallery
- Example Gallery
- Canvas Element
- Canvas Kit Interactive
- Carousel Demo - Selector API Showcase
- Chrome-Style Tabs
- blinc_cn Components
- Run with
cargo run -p blinc_app_examples --features cn --example cn_pickers_demo. - Code Element
- Complex SVG
- CSS Debug
- CSS Visual Features
- Layer Effects
- Emoji and HTML Entities
- @flow Shader
- Fluid Surface
- Skeleton animation with glTF +
blinc_canvas_kit. - Gpu Pass Demo
- Image CSS Styling
- Image Layer Test
- Keyframe Animation Canvas
- Markdown Editor
- 3D Mesh Demo — renders the Khronos glTF
DamagedHelmetsample model - Motion Demo
- Music Player Glass Card
- Node-editor demo — pre-wired graph with three node types, typed
- Notch Menu Bar
- Overflow Fade
- Overlay System
- Pointer Query
- Rich Text Element
- Rich Text Editor
- Scroll Container
- Semantic @flow
- Sortable
- Stateful API + Signal-bound modifiers demo.
- End-to-end 3D demo wiring Blinc's SceneKit3D renderer up to
- Unified Styling API
- SVG Animation
- Table Builder
- Tabler Icons
- Minimal text positioning test
- Text Input Widgets
- KHR_texture_transform
- Theme System
- Timeline Animation
- Typography
- Wet Glass
- Windowed Application
Web Development
Core Concepts
- Elements & Layout
- Styling & Materials
- CSS Styling
- Theming
- Event Handling
- Accessibility
- State Management
- Lists & Conditional Content
- Element Refs
Animation
Components
Component Library (blinc_cn)
- Overview
- Button
- Card
- Icons
- Typography
- Dialog
- Form Components
- Date, Time and Colour Pickers
- Rich Text Editor
- Code Editor
- Navigation
- Feedback
- Layout
- Data Display
Widgets
- Buttons & Inputs
- Text & Rich Text
- Code Editor
- Scroll Containers
- Virtualized List
- Canvas Drawing
- Gestures
- Notched Surfaces
- Images & SVG
- Audio & Video
- Markdown Rendering
Canvas Kit
Advanced
- Routing & Navigation
- Multi-Window
- System Integration
- Element Query API
- Overlay System
- Custom State Machines
- Pointer Query
- Performance Tips
- Flow Shaders
- 3D Rendering
- Custom GPU Passes
- Hot reloading
Architecture
Contributing