Replies: 1 comment 1 reply
-
|
Canvas 2D isn't something you can drive from rsx and signals. You have to reach the real The shape of it (web target): use dioxus::prelude::*;
use dioxus_web::WebEventExt;
use wasm_bindgen::JsCast;
use web_sys::{HtmlCanvasElement, CanvasRenderingContext2d};
let mut ctx = use_signal(|| None::<CanvasRenderingContext2d>);
let mut last = use_signal(|| None::<(f64, f64)>);
rsx! {
canvas {
width: "800",
height: "600",
onmounted: move |el| {
let canvas: HtmlCanvasElement = el.as_web_event().dyn_into().unwrap();
let c = canvas.get_context("2d").unwrap().unwrap()
.dyn_into::<CanvasRenderingContext2d>().unwrap();
ctx.set(Some(c));
},
onmousedown: move |event: Event<MouseData>| {
let p = event.data.coordinates().element();
last.set(Some((p.x, p.y)));
},
onmousemove: move |event: Event<MouseData>| {
if let (Some(c), Some((x0, y0))) = (ctx(), last()) {
let p = event.data.coordinates().element();
c.begin_path();
c.move_to(x0, y0);
c.line_to(p.x, p.y);
c.stroke();
last.set(Some((p.x, p.y)));
}
},
onmouseup: move |_| last.set(None),
onmouseleave: move |_| last.set(None),
}
}The key bit is This path is web-only, so gate it behind your web feature. If you want a fuller worked example, discussion #999 (Using Canvas in a dioxus web App) is the canonical one. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am currently trying to create a canvas in which the user could draw with their mouse, however I am not finding any recent example to perform that.
What is the more modern way to proceed?
Here is a snippet of what I am trying to achieve:
Beta Was this translation helpful? Give feedback.
All reactions