Is this the right way to add event on window object? #245
Answered
by
1216892614
1216892614
asked this question in
Q&A
-
Main Codelet window = window().unwrap();
let _ = EventListener::new(&window, "resize", |_| {
log!("resize!");
});
log!(window); What happend?
All Code for this.use gloo::{console::log, events::EventListener};
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{window, CanvasRenderingContext2d, HtmlCanvasElement};
use yew::prelude::*;
fn rander(interface: &CanvasRenderingContext2d, canvas: &HtmlCanvasElement) {
interface.clear_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64);
interface.set_fill_style(&JsValue::from_str("#fe5c5a"));
interface.set_font("100px sans-serif");
interface.set_text_baseline("top");
let text = "拽😂🤣TUVWXYZ0123456789";
let text_metrics = interface.measure_text(text).unwrap();
let (actual_bounding_box_descent, font_bounding_box_descent, width) = (
text_metrics.actual_bounding_box_descent(),
text_metrics.font_bounding_box_descent(),
text_metrics.width(),
);
let text_pos = (100.0, 100.0);
interface.fill_text(text, text_pos.0, text_pos.1).unwrap();
interface.set_stroke_style(&JsValue::from_str("red"));
interface.stroke_rect(text_pos.0, text_pos.1, width, actual_bounding_box_descent);
interface.set_stroke_style(&JsValue::from_str("green"));
interface.stroke_rect(text_pos.0, text_pos.1, width, font_bounding_box_descent)
}
#[function_component(SAKARA)]
pub fn sakara() -> Html {
let node_ref = NodeRef::default();
let is_first_rander = use_state(|| true);
{
let node_ref = node_ref.clone();
use_effect(move || {
if *is_first_rander {
is_first_rander.set(false);
//HERE !!!!!!!!!!!!!!!!!
let window = window().unwrap();
let _ = EventListener::new(&window, "resize", |_| {
log!("resize!");
});
log!(window);
}
let canvas = node_ref.cast::<HtmlCanvasElement>().unwrap();
let interface: CanvasRenderingContext2d = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into()
.unwrap();
rander(&interface, &canvas);
|| {}
});
}
html! {
<canvas
style="width: 100%; height: 100%;"
ref={node_ref}
/>
}
}
fn main() {
yew::start_app::<SAKARA>();
} |
Beta Was this translation helpful? Give feedback.
Answered by
1216892614
Aug 23, 2022
Replies: 1 comment
-
Fixed!Add |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
1216892614
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed!
Add
.forget()
at the tail ofEventListener::new(...)