Beginnings of the bezier-rs math library (#662)

Co-authored-by: Thomas Cheng <35661641+Androxium@users.noreply.github.com>
Co-authored-by: Robert Nadal <Robnadal44@gmail.com>
Co-authored-by: ll2zheng <ll2zheng@uwaterloo.ca>
This commit is contained in:
Hannah Li
2022-06-16 20:50:58 -04:00
committed by Keavon Chambers
co-authored by Thomas Cheng Robert Nadal ll2zheng
parent 18a7c6a289
commit 9f76315bdc
30 changed files with 20185 additions and 2 deletions
@@ -0,0 +1,43 @@
[package]
name = "bezier-rs-wasm"
publish = false
version = "0.0.0"
rust-version = "1.56.0"
authors = ["Graphite Authors <contact@graphite.rs>"]
edition = "2021"
readme = "../../README.md"
homepage = "https://graphite.rs"
repository = "https://github.com/GraphiteEditor/Graphite"
license = "Apache-2.0"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
bezier-rs = { path = "../../../lib", package = "bezier-rs" }
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2.73", features = ["serde-serialize"] }
serde_json = "*"
serde-wasm-bindgen = "0.4.1"
js-sys = "0.3.55"
glam = { version = "0.17", features = ["serde"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.22"
[package.metadata.wasm-pack.profile.dev]
wasm-opt = false
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
debug-js-glue = true
demangle-name-section = true
dwarf-debug-info = true
[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Os"]
[package.metadata.wasm-pack.profile.release.wasm-bindgen]
debug-js-glue = false
demangle-name-section = false
dwarf-debug-info = false
@@ -0,0 +1,74 @@
use bezier_rs::Bezier;
use glam::DVec2;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
#[wasm_bindgen]
pub struct WasmBezier {
internal: Bezier,
}
pub fn vec_to_point(p: &DVec2) -> JsValue {
JsValue::from_serde(&serde_json::to_string(&Point { x: p[0], y: p[1] }).unwrap()).unwrap()
}
#[wasm_bindgen]
impl WasmBezier {
/// Expect js_points to be a list of 3 pairs
pub fn new_quad(js_points: &JsValue) -> WasmBezier {
let points: [DVec2; 3] = js_points.into_serde().unwrap();
WasmBezier {
internal: Bezier::from_quadratic_dvec2(points[0], points[1], points[2]),
}
}
/// Expect js_points to be a list of 4 pairs
pub fn new_cubic(js_points: &JsValue) -> WasmBezier {
let points: [DVec2; 4] = js_points.into_serde().unwrap();
WasmBezier {
internal: Bezier::from_cubic_dvec2(points[0], points[1], points[2], points[3]),
}
}
pub fn set_start(&mut self, x: f64, y: f64) {
self.internal.set_start(DVec2::from((x, y)));
}
pub fn set_end(&mut self, x: f64, y: f64) {
self.internal.set_end(DVec2::from((x, y)));
}
pub fn set_handle1(&mut self, x: f64, y: f64) {
self.internal.set_handle1(DVec2::from((x, y)));
}
pub fn set_handle2(&mut self, x: f64, y: f64) {
self.internal.set_handle2(DVec2::from((x, y)));
}
pub fn get_points(&self) -> Vec<JsValue> {
self.internal.get_points().iter().flatten().map(vec_to_point).collect()
}
pub fn to_svg(&self) -> String {
self.internal.to_svg()
}
pub fn length(&self) -> f64 {
self.internal.length()
}
pub fn compute(&self, t: f64) -> JsValue {
vec_to_point(&self.internal.compute(t))
}
pub fn compute_lookup_table(&self, steps: i32) -> Vec<JsValue> {
self.internal.compute_lookup_table(Some(steps)).iter().map(vec_to_point).collect()
}
}