-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Renderer::animated method and improve frame rate
- Loading branch information
Showing
4 changed files
with
62 additions
and
53 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 |
---|---|---|
@@ -1,56 +1,27 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use skia_safe::Color4f; | ||
use taffy::{ | ||
prelude::Size, | ||
style::{AlignItems, JustifyContent}, | ||
}; | ||
use viewbuilder::{render::UserEvent, Context, Element, Renderer}; | ||
use taffy::prelude::Size; | ||
use viewbuilder::{Context, Element, Renderer}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// TODO this is really early stage, an animation frame should be requested | ||
|
||
let mut tree = Context::default(); | ||
let root = Element::new() | ||
.align_items(AlignItems::Center) | ||
.justify_content(JustifyContent::Center) | ||
let mut cx = Context::default(); | ||
let animated = Element::new() | ||
.size(Size::from_points(100., 100.)) | ||
.background_color(Color4f::new(0., 0., 1., 1.)) | ||
.build(&mut tree); | ||
|
||
let renderer = Renderer::new(); | ||
let tx = renderer.tx.clone(); | ||
let notify = renderer.notify.clone(); | ||
|
||
let mut is_forward = true; | ||
.build(&mut cx); | ||
|
||
tokio::spawn(async move { | ||
let mut start = Instant::now(); | ||
loop { | ||
let _min = 0.; | ||
let max = 500.; | ||
|
||
let elapsed = Instant::now() - start; | ||
let millis = elapsed.as_millis() as f32; | ||
let (begin, end) = if is_forward { (0., max) } else { (max, 0.) }; | ||
let interpolated: f32 = interpolation::lerp(&begin, &end, &(millis / 500.)); | ||
let size = interpolated.min(max).max(0.); | ||
|
||
if elapsed >= Duration::from_secs(1) { | ||
start = Instant::now(); | ||
is_forward = !is_forward; | ||
} | ||
|
||
tx.send(UserEvent(Box::new(move |tree| { | ||
tree.node(root) | ||
.set_size(Size::from_points(size as f32, size as f32)) | ||
}))) | ||
.unwrap(); | ||
let root = Element::new() | ||
.size(Size::from_points(1000., 200.)) | ||
.child(cx.insert("Hello")) | ||
.child(animated) | ||
.child(cx.insert("World")) | ||
.build(&mut cx); | ||
|
||
notify.notified().await; | ||
} | ||
}); | ||
let renderer = Renderer::new(); | ||
tokio::spawn(renderer.animation(animated, 0., 100., move |cx, size| { | ||
cx.node(animated) | ||
.set_size(Size::from_points(size as f32, size as f32)) | ||
})); | ||
|
||
renderer.run(tree, root) | ||
renderer.run(cx, root) | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
//! It supports layout, drawing, and accessability. | ||
|
||
pub mod node; | ||
|
||
pub use node::Node; | ||
|
||
pub mod tree; | ||
|
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
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