Add tiny-skia example (#317)

* tiny skia example

* image

* remove unnecessary let binding
This commit is contained in:
Jeffrey Rosenbluth 2022-11-15 14:47:28 -05:00 committed by GitHub
parent 0a85025345
commit bc8235fdb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 183 additions and 0 deletions

View file

@ -0,0 +1,19 @@
[package]
name = "tiny-skia-winit"
version = "0.1.0"
authors = ["Jeffrey Rosenbluth <jeffrey.rosenbluth@gmail.com"]
edition = "2021"
publish = false
[features]
optimize = ["log/release_max_level_warn"]
default = ["optimize"]
[dependencies]
env_logger = "0.9"
euclid = "0.22"
log = "0.4"
pixels = { path = "../.." }
winit = "0.27"
winit_input_helper = "0.13"
tiny-skia = "0.8.2"

View file

@ -0,0 +1,15 @@
# Hello tiny-skia
![Hello Tiny-Skia](../../img/tiny-skia-winit.png)
`tiny-skia` example with `winit`.
## Running
```bash
cargo run --release --package tiny-skia-winit
```
## About
This example uses `tiny-skia` to rasterize and animate a few paths.

View file

@ -0,0 +1,76 @@
#![deny(clippy::all)]
#![forbid(unsafe_code)]
use log::error;
use pixels::{Error, Pixels, SurfaceTexture};
use shape::draw;
use std::time::Instant;
use tiny_skia::Pixmap;
use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;
mod shape;
const WIDTH: u32 = 1000;
const HEIGHT: u32 = 1000;
fn main() -> Result<(), Error> {
env_logger::init();
let event_loop = EventLoop::new();
let mut input = WinitInputHelper::new();
let window = {
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
WindowBuilder::new()
.with_title("Hello tiny-skia")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
.unwrap()
};
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(WIDTH, HEIGHT, surface_texture)?
};
let mut drawing = Pixmap::new(1000, 1000).unwrap();
let now = Instant::now();
event_loop.run(move |event, _, control_flow| {
// Draw the current frame
if let Event::RedrawRequested(_) = event {
pixels.get_frame_mut().copy_from_slice(drawing.data());
if pixels
.render()
.map_err(|e| error!("pixels.render() failed: {}", e))
.is_err()
{
*control_flow = ControlFlow::Exit;
return;
}
}
// Handle input events
if input.update(&event) {
// Close events
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
*control_flow = ControlFlow::Exit;
return;
}
// Resize the window
if let Some(size) = input.window_resized() {
pixels.resize_surface(size.width, size.height);
}
// Update internal state and request a redraw
// shapes.draw(now.elapsed().as_secs_f32());
draw(&mut drawing, now.elapsed().as_secs_f32());
window.request_redraw();
}
});
}

View file

@ -0,0 +1,73 @@
use tiny_skia::*;
pub fn draw(pixmap: &mut Pixmap, delta: f32) {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 107, 160, 255);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(255, 125, 0, 150);
paint2.anti_alias = true;
let mut paint3 = Paint::default();
paint3.set_color_rgba8(205, 205, 205, 205);
paint3.anti_alias = true;
let mut paint4 = Paint::default();
paint4.set_color_rgba8(128, 0, 128, 255);
paint4.anti_alias = true;
let mut paint5 = Paint::default();
paint5.set_color_rgba8(20, 205, 25, 205);
paint5.anti_alias = true;
let path1 = PathBuilder::from_circle(400.0, 400.0, 300.0).unwrap();
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
pixmap.fill(Color::from_rgba8(0, 0, 0, 255));
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::from_rotate_at(delta * 15.0, 500.0, 500.0),
None,
);
stroke.width = 4.0;
pixmap.stroke_path(
&path1,
&paint5,
&stroke,
Transform::from_rotate_at(delta * 15.0, 500.0, 500.0),
None,
);
stroke.width = 48.0;
pixmap.stroke_path(
&path1,
&paint4,
&stroke,
Transform::from_rotate_at(-delta * 25.0, 500.0, 500.0).post_scale(0.75, 0.75),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
stroke.width = 8.0;
pixmap.stroke_path(&path2, &paint3, &stroke, Transform::identity(), None);
}

BIN
img/tiny-skia-winit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB