36 releases

0.8.4 Oct 13, 2023
0.8.2 Jul 21, 2023
0.6.1 Feb 26, 2023
0.5.12 Mar 8, 2020
0.5.2 Mar 22, 2018

#76 in Data formats

Download history 128/week @ 2023-10-26 132/week @ 2023-11-02 168/week @ 2023-11-09 155/week @ 2023-11-16 229/week @ 2023-11-23 227/week @ 2023-11-30 221/week @ 2023-12-07 298/week @ 2023-12-14 216/week @ 2023-12-21 185/week @ 2023-12-28 260/week @ 2024-01-04 458/week @ 2024-01-11 110/week @ 2024-01-18 244/week @ 2024-01-25 369/week @ 2024-02-01 323/week @ 2024-02-08

1,058 downloads per month
Used in 9 crates (6 directly)

GPL-3.0 license

74KB
1K SLoC

blit

Build Status Crates.io Documentation License: GPL-3.0 Downloads

Documentation

Draw sprites quickly using a masking color or an alpha treshold.

Interactive Demo

This crate works with RGBA u32 buffers. The alpha channel can only be read with a singular treshold, converting it to a binary transparent or opaque color. The reason this limitation is in place is that it allows efficient rendering optimizations.

For ergonomic use of this crate without needing to type convert everything most functions accepting numbers are generic with the number types being num_traits::ToPrimitive, this might seem confusing but any number can be passed to these functions immediately.

When using this crate the most important function to know about is Blit::blit, which is implemented for BlitBuffer.

Example

use blit::{Blit, ToBlitBuffer, BlitOptions, geom::Size};

const CANVAS_SIZE: Size = Size { width: 180, height: 180 };
const MASK_COLOR: u32 = 0xFF_00_FF;
// Create a buffer in which we'll draw our image
let mut canvas: Vec<u32> = vec![0xFF_FF_FF_FF; CANVAS_SIZE.pixels()];

// Load the image from disk using the `image` crate
let img = image::open("examples/smiley_rgb.png").unwrap().into_rgb8();

// Blit by creating a special blitting buffer first where the MASK_COLOR will be the color that will be made transparent
let blit_buffer = img.to_blit_buffer_with_mask_color(MASK_COLOR);

// Draw the image 2 times to the buffer
blit_buffer.blit(&mut canvas, CANVAS_SIZE, &BlitOptions::new_position(10, 10));
blit_buffer.blit(&mut canvas, CANVAS_SIZE, &BlitOptions::new_position(20, 20));

Dependencies

~0.4–4MB
~34K SLoC