Skip to content

Target

The Target interface (trait in Rust) is a rendering destination for the Renderer implemented by both WindowTarget and TextureTarget.

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

Target constructors are private and can only be created via the Renderer using either Renderer.create_target(window) for on-screen rendering, or Renderer.create_texture_target() for offscreen rendering.

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

Returns the current contents of the target as a byte array in RGBA8 format.

A TextureTarget returns its pixels, or an error when the readback fails. A WindowTarget always returns an error, because a frame on screen can’t be read back.

use fragmentcolor::{Shader, Renderer, Target};
let renderer = Renderer::new();
let target = renderer.create_texture_target([16, 16]).await?;
renderer.render(&Shader::default(), &target)?;
let image = target.get_image().await?;

Target::resize(size: Size { width, height } | (u32, u32) | [u32; 2])

Section titled “Target::resize(size: Size { width, height } | (u32, u32) | [u32; 2])”

Resizes the Target to the given width and height.

It returns an error when the target can’t take the size, and keeps the size it had. JavaScript throws an Error named after the class Python raises, Python raises FragmentColorError or a subclass, Swift throws FragmentColorError, and Kotlin throws FragmentColorException. A TextureTarget refuses a width or height of zero. A WindowTarget accepts one, such as the 0×0 a minimized window reports, keeps its last size, and draws again once the window has one.

use fragmentcolor::{Renderer, Target};
let renderer = Renderer::new();
let mut target = renderer.create_texture_target([64, 32]).await?;
target.resize([128, 64])?;

Returns the size of the Target in pixels.

depth is absent for every target, as for a 2D Texture, 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_texture_target([64, 32]).await?;
let size = target.size();
let width = size.width;
let height = size.height;
let depth = size.depth;