From 747e9f959585ff7381b651ba37a7f690d754dbe8 Mon Sep 17 00:00:00 2001 From: jeangab Date: Fri, 14 Jul 2023 13:30:38 -0400 Subject: [PATCH] Working on canvas, now having little squares wherever I want, next up is figuring out how to resize the canvas to the window size --- src/app.rs | 6 ++--- src/components/background.rs | 29 +++++++++++++++++++--- src/lib.rs | 1 + src/space_colonization/mod.rs | 46 +++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/space_colonization/mod.rs diff --git a/src/app.rs b/src/app.rs index 49acffe..9fd95be 100644 --- a/src/app.rs +++ b/src/app.rs @@ -24,9 +24,8 @@ pub fn App(cx: Scope) -> impl IntoView { // content for this welcome page -
- -
+
+
+
} diff --git a/src/components/background.rs b/src/components/background.rs index 09abf22..f634ecf 100644 --- a/src/components/background.rs +++ b/src/components/background.rs @@ -1,11 +1,13 @@ -use std::f64; use leptos::html::Canvas; use leptos::*; +use std::f64; use wasm_bindgen::JsCast; use wasm_bindgen::JsValue; +use crate::space_colonization::SpaceColonization; + #[component] -pub fn Background(cx: Scope) -> impl IntoView { +pub fn Background(cx: Scope, class: &'static str) -> impl IntoView { let canvas = create_node_ref::(cx); canvas.on_load(cx, move |_| { @@ -44,10 +46,31 @@ pub fn Background(cx: Scope) -> impl IntoView { .arc(90.0, 65.0, 5.0, 0.0, f64::consts::PI * 2.0) .unwrap(); + log!("before stroke"); context.stroke(); + + let sc = SpaceColonization::new(); + context.set_fill_style(&JsValue::from("yellow")); + log!("About to render nodes"); + for n in sc.nodes.iter() { + context.fill_rect(n.position.x.into(), n.position.y.into(), 5.0, 5.0); + log!("filled node at position = {:#?}", n.position); + } + + context.set_fill_style(&JsValue::from("magenta")); + for a in sc.attractors.iter() { + context.fill_rect(a.position.x.into(), a.position.y.into(), 5.0, 5.0); + log!("filled node at position = {:#?}", a.position); + } + context.fill_rect(100.0, 100.0, 5.0, 5.0); + context.fill_rect(200.0, 200.0, 5.0, 5.0); + context.fill_rect(300.0, 300.0, 5.0, 5.0); + context.fill_rect(400.0, 400.0, 5.0, 5.0); + context.fill_rect(500.0, 500.0, 5.0, 5.0); }); + let class = format!("canvas {}", class); view! { cx, -
+
} diff --git a/src/lib.rs b/src/lib.rs index 70dc848..716860b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod space_colonization; pub mod app; mod components; mod routes; diff --git a/src/space_colonization/mod.rs b/src/space_colonization/mod.rs new file mode 100644 index 0000000..1727c4d --- /dev/null +++ b/src/space_colonization/mod.rs @@ -0,0 +1,46 @@ +#[derive(Debug)] +pub struct Point { + pub x: u16, + pub y: u16, +} + +pub struct Attractor { + pub position: Point, +} + +pub struct Node { + pub position: Point, +} + +pub struct SpaceColonization { + max_point: Point, + kill_distance: u16, + attraction_force: u16, + density: u16, + pub nodes: Vec, + pub attractors: Vec, +} + +impl SpaceColonization { + pub fn new() -> SpaceColonization { + let mut nodes = Vec::new(); + nodes.push(Node { position: Point { x: 100, y: 100 } }); + let mut attractors = Vec::new(); + attractors.push(Attractor { position: Point { x: 10, y: 10 } }); + attractors.push(Attractor { position: Point { x: 20, y: 20 } }); + attractors.push(Attractor { position: Point { x: 30, y: 30 } }); + attractors.push(Attractor { position: Point { x: 40, y: 40 } }); + attractors.push(Attractor { position: Point { x: 50, y: 50 } }); + attractors.push(Attractor { position: Point { x: 60, y: 60 } }); + attractors.push(Attractor { position: Point { x: 70, y: 70 } }); + + SpaceColonization { + max_point: Point { x: 500, y: 500 }, + kill_distance: 10, + attraction_force: 15, + density: 10, + nodes, + attractors, + } + } +}