-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tiny skia example * image * remove unnecessary let binding
- Loading branch information
1 parent
0a85025
commit bc8235f
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.