use freya::{
plot::{
PlotSkiaBackend,
plotters::{
chart::ChartBuilder,
prelude::{IntoDrawingArea, IntoLinspace},
series::SurfaceSeries,
style::{BLACK, BLUE, WHITE},
},
},
prelude::*,
};
fn main() {
launch(LaunchConfig::new().with_window(WindowConfig::new(app)))
}
fn on_render(ctx: &mut RenderContext, (cursor_x, cursor_y): (f64, f64)) {
let backend = PlotSkiaBackend::new(
ctx.canvas,
ctx.font_collection,
ctx.layout_node.area.size.to_i32().to_tuple(),
)
.into_drawing_area();
backend.fill(&WHITE).unwrap();
let pitch = std::f64::consts::PI * (0.5 - cursor_y / ctx.layout_node.area.height() as f64);
let yaw = std::f64::consts::PI * 2.0 * (cursor_x / ctx.layout_node.area.width() as f64 - 0.5);
let scale = 0.4 + 0.6 * (1.0 - cursor_y / ctx.layout_node.area.height() as f64);
let x_axis = (-3.0..3.0).step(0.1);
let z_axis = (-3.0..3.0).step(0.1);
let mut chart = ChartBuilder::on(&backend)
.caption("3D Plot - Move mouse to rotate", ("sans", 20))
.build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())
.unwrap();
chart.with_projection(|mut pb| {
pb.pitch = pitch;
pb.yaw = yaw;
pb.scale = scale;
pb.into_matrix()
});
chart
.configure_axes()
.light_grid_style(BLACK.mix(0.15))
.max_light_lines(3)
.draw()
.unwrap();
chart
.draw_series(
SurfaceSeries::xoz(
(-30..30).map(|f| f as f64 / 10.0),
(-30..30).map(|f| f as f64 / 10.0),
|x, z| (x * x + z * z).cos(),
)
.style(BLUE.mix(0.2).filled()),
)
.unwrap();
}
fn app() -> impl IntoElement {
let mut cursor_position = use_state(CursorPoint::default);
let on_global_mouse_move = move |e: Event<MouseEventData>| {
if e.global_location.to_tuple() != (-1., -1.) {
cursor_position.set(e.global_location);
let platform = Platform::get();
platform.send(UserEvent::RequestRedraw);
}
};
canvas(RenderCallback::new(move |context| {
on_render(context, cursor_position().to_tuple());
}))
.expanded()
.on_global_mouse_move(on_global_mouse_move)
}