This commit is contained in:
Simon Gardling
2022-02-11 09:53:23 -05:00
parent be2d6a2f71
commit 4c072d3d96
2 changed files with 54 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
#[wasm_bindgen]
pub struct Chart {
convert: Box<dyn Fn((i32, i32)) -> Option<(f32, f32)>>,
area: f32
area: f32,
}
/// Result of screen to chart coordinates conversion.
@@ -23,8 +23,27 @@ pub struct Point {
#[wasm_bindgen]
impl Chart {
pub fn draw(canvas: HtmlCanvasElement, func: &str, min_x: f32, max_x: f32, min_y: f32, max_y: f32, num_interval: usize, resolution: i32) -> Result<Chart, JsValue> {
let output = func_plot::draw(canvas, func, min_x, max_x, min_y, max_y, num_interval, resolution).map_err(|err| err.to_string())?;
pub fn draw(
canvas: HtmlCanvasElement,
func: &str,
min_x: f32,
max_x: f32,
min_y: f32,
max_y: f32,
num_interval: usize,
resolution: i32,
) -> Result<Chart, JsValue> {
let output = func_plot::draw(
canvas,
func,
min_x,
max_x,
min_y,
max_y,
num_interval,
resolution,
)
.map_err(|err| err.to_string())?;
let map_coord = output.0;
Ok(Chart {
convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))),