Skip to content

WindowTarget

WindowTarget is an implementation of Target that represents a rendering destination attached to a platform-specific window.

The Target object is a rendering destination for the Renderer.

It contains a GPU surface texture attached to a platform-specific window or an offscreen texture for headless rendering (see TextureTarget).

In Rust, we officially support Winit. Check the examples folder for details.

use fragmentcolor::{Renderer, Shader, Target};
let renderer = Renderer::new();
// The canvas comes from your platform's windowing system.
let target = renderer.create_target(canvas).await?;
renderer.render(&Shader::default(), &target)?;

WindowTarget::size() -> Size { width, height, depth }

Section titled “WindowTarget::size() -> Size { width, height, depth }”

Returns the size of the WindowTarget in pixels. depth is absent for a window target in every language: None in Rust and Python, undefined in JS, nil in Swift and null in Kotlin.

use fragmentcolor::{Renderer, Target};
let renderer = Renderer::new();
let target = renderer.create_target(canvas).await?;
let size = target.size();

WindowTarget::resize(size: [u32; 2] | (u32, u32))

Section titled “WindowTarget::resize(size: [u32; 2] | (u32, u32))”

Resizes the WindowTarget to the given width and height. Call it when the window or canvas changes size.

A width or height of zero, such as the 0×0 a minimized window reports, is no error: the target keeps its last size and draws at the new one once the window has an area again. A side past the device limit returns an error.

use fragmentcolor::{Renderer, Target};
let renderer = Renderer::new();
let mut target = renderer.create_target(canvas).await?;
target.resize([1024, 768])?;

Returns an error for a WindowTarget: a frame on screen can’t be read back. JavaScript throws an Error named FragmentColorError, Python and Swift throw a FragmentColorError, and Kotlin throws a FragmentColorException.

To read the pixels of a frame, draw it into a TextureTarget and call get_image there, as the example does.

use fragmentcolor::{Renderer, Shader, Target};
let renderer = Renderer::new();
let target = renderer.create_target(canvas).await?;
let shader = Shader::default();
renderer.render(&shader, &target)?;
// The same frame, drawn where it can be read back.
let offscreen = renderer.create_texture_target([800, 600]).await?;
renderer.render(&shader, &offscreen)?;
let image = offscreen.get_image().await?;