Skip to content

Commit 577708e

Browse files
committed
Add docs to wasm-demo and rustfmt it
1 parent 705fc03 commit 577708e

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

examples/wasm-demo/src/func_plot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use plotters::prelude::*;
21
use crate::DrawResult;
2+
use plotters::prelude::*;
33

44
/// Draw power function f(x) = x^power.
55
pub fn draw(canvas_id: &str, power: i32) -> DrawResult<impl Fn((i32, i32)) -> Option<(f32, f32)>> {
6-
let backend = CanvasBackend::new(canvas_id).unwrap();
6+
let backend = CanvasBackend::new(canvas_id).expect("cannot find canvas");
77
let root = backend.into_drawing_area();
88
let font: FontDesc = ("sans-serif", 20.0).into();
99

examples/wasm-demo/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1010
/// Type alias for the result of a drawing function.
1111
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
1212

13+
/// Type used on the JS side to convert screen coordinates to chart
14+
/// coordinates.
1315
#[wasm_bindgen]
1416
pub struct Chart {
1517
convert: Box<dyn Fn((i32, i32)) -> Option<(f64, f64)>>,
1618
}
1719

20+
/// Result of screen to chart coordinates conversion.
1821
#[wasm_bindgen]
1922
pub struct Point {
2023
pub x: f64,
@@ -23,21 +26,27 @@ pub struct Point {
2326

2427
#[wasm_bindgen]
2528
impl Chart {
29+
/// Draw provided power function on the canvas element using it's id.
30+
/// Return `Chart` struct suitable for coordinate conversion.
2631
pub fn power(canvas_id: &str, power: i32) -> Result<Chart, JsValue> {
2732
let map_coord = func_plot::draw(canvas_id, power).map_err(|err| err.to_string())?;
28-
Ok(Chart{convert: Box::new(move |coord| {
29-
map_coord(coord).map(|(x, y)| (x.into(), y.into()))
30-
})})
33+
Ok(Chart {
34+
convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))),
35+
})
3136
}
3237

38+
/// Draw Mandelbrot set on the provided canvas element.
39+
/// Return `Chart` struct suitable for coordinate conversion.
3340
pub fn mandelbrot(canvas: HtmlCanvasElement) -> Result<Chart, JsValue> {
3441
let map_coord = mandelbrot::draw(canvas).map_err(|err| err.to_string())?;
35-
Ok(Chart{convert: Box::new(map_coord)})
42+
Ok(Chart {
43+
convert: Box::new(map_coord),
44+
})
3645
}
3746

3847
/// This function can be used to convert screen coordinates to
39-
/// graph coordinates.
48+
/// chart coordinates.
4049
pub fn coord(&self, x: i32, y: i32) -> Option<Point> {
41-
(self.convert)((x, y)).map(|(x, y)| Point{x, y})
50+
(self.convert)((x, y)).map(|(x, y)| Point { x, y })
4251
}
4352
}

examples/wasm-demo/src/mandelbrot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::DrawResult;
12
use plotters::prelude::*;
23
use std::ops::Range;
34
use web_sys::HtmlCanvasElement;
4-
use crate::DrawResult;
55

66
/// Draw Mandelbrot set
77
pub fn draw(element: HtmlCanvasElement) -> DrawResult<impl Fn((i32, i32)) -> Option<(f64, f64)>> {

examples/wasm-demo/www/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Chart, drawPower } from "wasm-demo";
22

33
const canvas = document.getElementById("canvas");
4-
const ctx = canvas.getContext("2d");
54
const coord = document.getElementById("coord");
65
const plotType = document.getElementById("plot-type");
76
const status = document.getElementById("status");
@@ -14,7 +13,7 @@ function main() {
1413
setupCanvas();
1514
}
1615

17-
/** Add event listeners */
16+
/** Add event listeners. */
1817
function setupUI() {
1918
status.innerText = "WebAssembly loaded!";
2019
plotType.addEventListener("change", updatePlot);
@@ -31,29 +30,29 @@ function setupCanvas() {
3130
canvas.style.height = size / aspectRatio + "px";
3231
canvas.width = size * dpr;
3332
canvas.height = size / aspectRatio * dpr;
34-
ctx.scale(dpr, dpr);
33+
canvas.getContext("2d").scale(dpr, dpr);
3534
updatePlot();
3635
}
3736

38-
/** Update displayed coordinates */
37+
/** Update displayed coordinates. */
3938
function onMouseMove(event) {
40-
if (chart) {
39+
if (chart) {
4140
const point = chart.coord(event.offsetX, event.offsetY);
4241
coord.innerText = (point)
4342
? `(${point.x.toFixed(3)}, ${point.y.toFixed(3)})`
4443
: "Mouse pointer is out of range";
4544
}
4645
}
4746

48-
/** Redraw currently selected plot */
47+
/** Redraw currently selected plot. */
4948
function updatePlot() {
5049
const selected = plotType.selectedOptions[0];
51-
status.innerText = `Rendering ${selected.innerText}...`;
52-
chart = undefined;
53-
const start = performance.now();
50+
status.innerText = `Rendering ${selected.innerText}...`;
51+
chart = undefined;
52+
const start = performance.now();
5453
chart = (selected.value == "mandelbrot")
5554
? Chart.mandelbrot(canvas)
5655
: Chart.power("canvas", Number(selected.value));
57-
const end = performance.now();
58-
status.innerText = `Rendered ${selected.innerText} in ${Math.ceil(end - start)}ms`;
56+
const end = performance.now();
57+
status.innerText = `Rendered ${selected.innerText} in ${Math.ceil(end - start)}ms`;
5958
}

0 commit comments

Comments
 (0)