refactoring
This commit is contained in:
parent
f83994a2fa
commit
ec7d2e4bb1
35
src/lib.rs
35
src/lib.rs
@ -4,12 +4,12 @@ use plotters_canvas::CanvasBackend;
|
|||||||
use std::panic;
|
use std::panic;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use web_sys::HtmlCanvasElement;
|
use web_sys::HtmlCanvasElement;
|
||||||
|
mod misc;
|
||||||
|
use crate::misc::{Chart, DrawResult};
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||||
|
|
||||||
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
||||||
@ -18,18 +18,7 @@ extern "C" {
|
|||||||
fn log(s: &str);
|
fn log(s: &str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Result of screen to chart coordinates conversion.
|
// Manages Chart generation and caching of values
|
||||||
#[wasm_bindgen]
|
|
||||||
pub struct Point {
|
|
||||||
pub x: f32,
|
|
||||||
pub y: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
impl Point {
|
|
||||||
pub fn new(x: f32, y: f32) -> Self { Self { x, y } }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub struct ChartManager {
|
pub struct ChartManager {
|
||||||
func_str: String,
|
func_str: String,
|
||||||
@ -69,6 +58,7 @@ impl ChartManager {
|
|||||||
// Used in order to hook into `panic!()` to log in the browser's console
|
// Used in order to hook into `panic!()` to log in the browser's console
|
||||||
pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); }
|
pub fn init_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); }
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
fn get_back_cache(&self) -> Vec<(f32, f32)> {
|
fn get_back_cache(&self) -> Vec<(f32, f32)> {
|
||||||
log("Using back_cache");
|
log("Using back_cache");
|
||||||
match &self.back_cache {
|
match &self.back_cache {
|
||||||
@ -77,6 +67,7 @@ impl ChartManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
fn get_front_cache(&self) -> (Vec<(f32, f32, f32)>, f32) {
|
fn get_front_cache(&self) -> (Vec<(f32, f32, f32)>, f32) {
|
||||||
log("Using front_cache");
|
log("Using front_cache");
|
||||||
match &self.front_cache {
|
match &self.front_cache {
|
||||||
@ -85,6 +76,7 @@ impl ChartManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
fn draw(
|
fn draw(
|
||||||
&mut self, element: HtmlCanvasElement,
|
&mut self, element: HtmlCanvasElement,
|
||||||
) -> DrawResult<(impl Fn((i32, i32)) -> Option<(f32, f32)>, f32)> {
|
) -> DrawResult<(impl Fn((i32, i32)) -> Option<(f32, f32)>, f32)> {
|
||||||
@ -229,18 +221,3 @@ impl ChartManager {
|
|||||||
(data2, area)
|
(data2, area)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub struct Chart {
|
|
||||||
convert: Box<dyn Fn((i32, i32)) -> Option<(f32, f32)>>,
|
|
||||||
area: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
impl Chart {
|
|
||||||
pub fn get_area(&self) -> f32 { self.area }
|
|
||||||
|
|
||||||
pub fn coord(&self, x: i32, y: i32) -> Option<Point> {
|
|
||||||
(self.convert)((x, y)).map(|(x, y)| Point::new(x, y))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
30
src/misc.rs
Normal file
30
src/misc.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
/// Result of screen to chart coordinates conversion.
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct Point {
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Point {
|
||||||
|
pub fn new(x: f32, y: f32) -> Self { Self { x, y } }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct Chart {
|
||||||
|
pub(crate) convert: Box<dyn Fn((i32, i32)) -> Option<(f32, f32)>>,
|
||||||
|
pub(crate) area: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Chart {
|
||||||
|
pub fn get_area(&self) -> f32 { self.area }
|
||||||
|
|
||||||
|
pub fn coord(&self, x: i32, y: i32) -> Option<Point> {
|
||||||
|
(self.convert)((x, y)).map(|(x, y)| Point::new(x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user