Canvas takes whole parent space now, attractors are initialized nicely from a random grid
This commit is contained in:
parent
0fafffb40f
commit
111285f32d
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -913,8 +913,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasi",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1424,12 +1426,14 @@ dependencies = [
|
||||
"cfg-if",
|
||||
"console_error_panic_hook",
|
||||
"console_log",
|
||||
"getrandom",
|
||||
"js-sys",
|
||||
"leptos",
|
||||
"leptos_actix",
|
||||
"leptos_meta",
|
||||
"leptos_router",
|
||||
"log",
|
||||
"rand",
|
||||
"simple_logger",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
|
||||
@ -22,6 +22,8 @@ log = "0.4"
|
||||
simple_logger = "4"
|
||||
wasm-bindgen = "=0.2.84"
|
||||
js-sys = "0.3.51"
|
||||
rand = "0.8.5"
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
|
||||
[dependencies.web-sys]
|
||||
version = "0.3.4"
|
||||
|
||||
@ -9,15 +9,18 @@ use crate::space_colonization::SpaceColonization;
|
||||
#[component]
|
||||
pub fn Background(cx: Scope, class: &'static str) -> impl IntoView {
|
||||
let canvas = create_node_ref::<Canvas>(cx);
|
||||
let mut size = (300, 300);
|
||||
|
||||
canvas.on_load(cx, move |_| {
|
||||
let canvas = canvas.get().expect("canvas is mounted");
|
||||
let canvas_parent = canvas.parent_element().unwrap();
|
||||
canvas.set_width(u32::try_from(canvas_parent.client_width()).unwrap());
|
||||
canvas.set_height(u32::try_from(canvas_parent.client_height()).unwrap());
|
||||
let width = canvas_parent.client_width();
|
||||
let height = canvas_parent.client_height();
|
||||
canvas.set_width(u32::try_from(width).unwrap());
|
||||
canvas.set_height(u32::try_from(height).unwrap());
|
||||
let sc = SpaceColonization::new(width.try_into().unwrap(), height.try_into().unwrap());
|
||||
// TODO Resize on window resize
|
||||
log!(
|
||||
"canvas parent size = {} {}",
|
||||
"TODO resize on window resize canvas parent size = {} {}",
|
||||
canvas_parent.client_width(),
|
||||
canvas_parent.client_height()
|
||||
);
|
||||
@ -57,7 +60,6 @@ pub fn Background(cx: Scope, class: &'static str) -> impl IntoView {
|
||||
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() {
|
||||
@ -70,12 +72,8 @@ pub fn Background(cx: Scope, class: &'static str) -> impl IntoView {
|
||||
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,
|
||||
<div class={class}>
|
||||
|
||||
@ -31,7 +31,7 @@ pub fn Blog(cx: Scope) -> impl IntoView {
|
||||
].join("\n");
|
||||
view! { cx,
|
||||
<Title text="Blog - NationTech - Unconventional team for extraordinary challenges"/>
|
||||
<div class="text-white bg-indigo-950">
|
||||
<div>
|
||||
<div class="flex space-y-4 flex-col max-w-3xl container mx-auto">
|
||||
<h1 class="text-6xl text-orange-400 font-extrabold pt-12">"Blog"</h1>
|
||||
<h2 class="text-3xl">"Modifying storageClass of kubernetes bitnami ElasticSearch cluster without downtime"</h2>
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
pub x: u16,
|
||||
@ -22,25 +25,66 @@ pub struct SpaceColonization {
|
||||
}
|
||||
|
||||
impl SpaceColonization {
|
||||
pub fn new() -> SpaceColonization {
|
||||
pub fn new(width: u16, height: u16) -> 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 } });
|
||||
nodes.push(Node {
|
||||
position: Point { x: 100, y: 100 },
|
||||
});
|
||||
let attractors = Vec::new();
|
||||
|
||||
SpaceColonization {
|
||||
max_point: Point { x: 500, y: 500 },
|
||||
let mut sc = SpaceColonization {
|
||||
max_point: Point {
|
||||
x: width,
|
||||
y: height,
|
||||
},
|
||||
kill_distance: 10,
|
||||
attraction_force: 15,
|
||||
density: 10,
|
||||
density: 30,
|
||||
nodes,
|
||||
attractors,
|
||||
};
|
||||
|
||||
sc.place_attractors();
|
||||
|
||||
return sc;
|
||||
}
|
||||
|
||||
fn place_attractors(&mut self) {
|
||||
let mut x_pos = 0;
|
||||
let mut y_pos = 0;
|
||||
while x_pos < self.max_point.x {
|
||||
while y_pos < self.max_point.y {
|
||||
self.attractors.push(Attractor {
|
||||
position: self.get_random_point(x_pos.into(), y_pos.into()),
|
||||
});
|
||||
y_pos += self.density;
|
||||
}
|
||||
x_pos += self.density;
|
||||
y_pos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn get_random_point(&self, x_pos: i32, y_pos: i32) -> Point {
|
||||
let half_density: i32 = (self.density / 2).into();
|
||||
let mut x_min = x_pos - half_density;
|
||||
if x_min < 0 {
|
||||
x_min = 0;
|
||||
}
|
||||
|
||||
let mut y_min = y_pos - half_density;
|
||||
if y_min < 0 {
|
||||
y_min = 0;
|
||||
}
|
||||
|
||||
Point {
|
||||
x: thread_rng()
|
||||
.gen_range(x_min..x_pos + half_density)
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
y: thread_rng()
|
||||
.gen_range(y_min..y_pos + half_density)
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user