-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: responsive canvas and grid on canvas
- Loading branch information
Showing
8 changed files
with
133 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use wasm_bindgen::JsValue; | ||
use web_sys::CanvasRenderingContext2d; | ||
|
||
pub fn draw_grid(canvas: &CanvasRenderingContext2d, size: (u32, u32), square_size: u32) { | ||
canvas.set_line_width(1.0); | ||
canvas.set_stroke_style(&JsValue::from_str("grey")); | ||
|
||
draw_vertical_lines(canvas, size.0, square_size, size.1 / square_size); | ||
draw_horizontal_lines(canvas, size.1, square_size, size.0 / square_size); | ||
} | ||
|
||
fn draw_vertical_lines( | ||
canvas: &CanvasRenderingContext2d, | ||
length: u32, | ||
square_size: u32, | ||
count: u32, | ||
) { | ||
for i in 0..count { | ||
let x = (i * square_size + square_size) as f64; | ||
canvas.begin_path(); | ||
canvas.move_to(x, 0.0); | ||
canvas.line_to(x, length as f64); | ||
canvas.stroke(); | ||
} | ||
} | ||
|
||
fn draw_horizontal_lines( | ||
canvas: &CanvasRenderingContext2d, | ||
length: u32, | ||
square_size: u32, | ||
count: u32, | ||
) { | ||
for i in 0..count { | ||
let y = (i * square_size + square_size) as f64; | ||
canvas.begin_path(); | ||
canvas.move_to(0.0, y); | ||
canvas.line_to(length as f64, y); | ||
canvas.stroke(); | ||
} | ||
} |
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,17 @@ | ||
use wasm_bindgen::JsCast; | ||
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement}; | ||
|
||
mod grid; | ||
|
||
use grid::draw_grid; | ||
|
||
pub fn redraw_canvas(canvas: &HtmlCanvasElement, size: (u32, u32)) { | ||
let context = canvas | ||
.get_context("2d") | ||
.unwrap() | ||
.unwrap() | ||
.dyn_into::<CanvasRenderingContext2d>() | ||
.unwrap(); | ||
|
||
draw_grid(&context, size, 30); | ||
} |
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,10 +1,76 @@ | ||
use leptos::html::Canvas; | ||
use leptos::logging::log; | ||
use leptos::*; | ||
use wasm_bindgen::closure::Closure; | ||
use wasm_bindgen::JsCast; | ||
|
||
use crate::algorithm::redraw_canvas; | ||
|
||
fn redraw(canvas_node: &HtmlElement<Canvas>) { | ||
// To have a canvas resize dynamically, we need to manually adjust its size | ||
// CSS will NOT work, as it will just make everything blurry | ||
let doc = window().document().expect("should have document"); | ||
let win_height = window().inner_height().unwrap().as_f64().unwrap(); | ||
let win_width = window().inner_width().unwrap().as_f64().unwrap(); | ||
|
||
// the navbar borders the top, so the height is `window - navbar` | ||
let nav = doc | ||
.get_element_by_id("navbar") | ||
.expect("navbar should exist"); | ||
let nav_height_px = window() | ||
.get_computed_style(&nav) | ||
.unwrap() | ||
.expect("should have style") | ||
.get_property_value("height") | ||
.expect("should have height property"); | ||
|
||
let height = (win_height | ||
- nav_height_px | ||
.trim_end_matches("px") | ||
.parse::<f64>() | ||
.expect("height should be an integer")) as u32; | ||
canvas_node.set_height(height); | ||
|
||
// the sidebar borders its side, so width is `window - sidebar` | ||
let side = doc | ||
.get_element_by_id("sidebar") | ||
.expect("sidebar should exist"); | ||
let side_width_px = window() | ||
.get_computed_style(&side) | ||
.unwrap() | ||
.expect("should have style") | ||
.get_property_value("width") | ||
.expect("should have width property"); | ||
|
||
let width = (win_width | ||
- side_width_px | ||
.trim_end_matches("px") | ||
.parse::<f64>() | ||
.expect("width should be an integer")) as u32; | ||
canvas_node.set_width(width); | ||
|
||
// Now the canvas is the correct size, we can draw it | ||
log!("redrawing canvas"); | ||
redraw_canvas(&*canvas_node, (height, width)); | ||
} | ||
|
||
#[component] | ||
pub fn Canvas() -> impl IntoView { | ||
let canvas_ref = create_node_ref::<Canvas>(); | ||
|
||
create_effect(move |_| { | ||
let canvas_node = canvas_ref.get().expect("should be loaded now"); | ||
|
||
redraw(&canvas_node); | ||
|
||
let f = Closure::<dyn Fn()>::new(move || redraw(&canvas_node)); | ||
window().set_onresize(Some(f.as_ref().unchecked_ref())); | ||
f.forget(); | ||
}); | ||
|
||
view! { | ||
<div class="h-full w-full flex bg-zinc-50 dark:bg-neutral-700 text-black dark:text-white"> | ||
<canvas id="canvas" class="grow m-5"/> | ||
<div class="grow overflow-hidden bg-zinc-50 dark:bg-neutral-700 text-black dark:text-white"> | ||
<canvas _ref=canvas_ref id="canvas" class="object-contain"/> | ||
</div> | ||
} | ||
} |
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
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