Skip to content

Commit 357a7f0

Browse files
committed
convert moves to Js Array + UI
1 parent b8bc99f commit 357a7f0

File tree

5 files changed

+282
-172
lines changed

5 files changed

+282
-172
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

string-bean-wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ crate-type = ["cdylib"]
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12+
js-sys = "0.3.64"
1213
string-bean = { path = "../string-bean" }
1314
wasm-bindgen = "0.2.87"

string-bean-wasm/src/lib.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
22

3+
type ReturnArray = js_sys::Array;
4+
35
#[wasm_bindgen]
4-
pub fn plan_as_json(
6+
pub fn json_plan_circle(
57
line_count: u32,
68
line_opacity: f32,
79
anchor_count: u32,
@@ -12,7 +14,7 @@ pub fn plan_as_json(
1214
height: usize,
1315
image_buffer: &[u8],
1416
start_anchor: usize,
15-
) -> JsValue {
17+
) -> ReturnArray {
1618
let (x_mid, y_mid) = (width / 2, height / 2);
1719
let radius = radius.min(x_mid.min(y_mid)) as f64;
1820

@@ -37,18 +39,48 @@ pub fn plan_as_json(
3739
image_buffer,
3840
);
3941

40-
let moves = planner
42+
planner
4143
.get_moves(start_anchor, line_count as _)
42-
.unwrap_or(Vec::new());
44+
.unwrap_or(Vec::new())
45+
.into_iter()
46+
.map(JsValue::from)
47+
.collect()
48+
}
49+
50+
#[wasm_bindgen]
51+
pub fn json_plan(
52+
line_count: u32,
53+
line_opacity: f32,
54+
anchor_list: &[f64],
55+
anchor_gap_count: usize,
56+
penalty: f32,
57+
width: usize,
58+
height: usize,
59+
image_buffer: &[u8],
60+
start_anchor: usize,
61+
) -> ReturnArray {
62+
let anchors = anchor_list
63+
.chunks_exact(2)
64+
.map(|chunk| (chunk[0], chunk[1]))
65+
.collect::<Vec<_>>();
4366

44-
JsValue::from_str(&format!(
45-
"[{}]",
46-
moves
47-
.iter()
48-
.map(usize::to_string)
49-
.collect::<Vec<_>>()
50-
.join(","),
51-
))
67+
let mut planner = string_bean::ThreadPlanner::new(
68+
line_opacity as _,
69+
&anchors,
70+
anchor_gap_count,
71+
penalty as _,
72+
grid_raytrace,
73+
width,
74+
height,
75+
image_buffer,
76+
);
77+
78+
planner
79+
.get_moves(start_anchor, line_count as _)
80+
.unwrap_or(Vec::new())
81+
.into_iter()
82+
.map(JsValue::from)
83+
.collect()
5284
}
5385

5486
/// https://playtechs.blogspot.com/2007/03/raytracing-on-grid.html

web/src/lib/utils.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Compute anchors for a circle
3+
* @param {number} anchor_count
4+
* @param {number} width
5+
* @param {number} height
6+
* @param {number} radius
7+
* @returns {[number, number][]}
8+
*/
9+
export function circle_anchors(anchor_count, width, height, radius) {
10+
const x_mid = width / 2;
11+
const y_mid = height / 2;
12+
13+
return Array.from({ length: anchor_count })
14+
.map((_, anchor) => (anchor * 2 * Math.PI) / anchor_count)
15+
.map((angle) => [
16+
x_mid + radius * Math.cos(angle),
17+
y_mid + radius * Math.sin(angle),
18+
]);
19+
}
20+
21+
/**
22+
* Compute anchors for a rectangle. Starts in the top left.
23+
* @param {number} anchor_count
24+
* @param {number} width
25+
* @param {number} height
26+
* @returns {[number, number][]}
27+
*/
28+
export function rectangle_anchors(anchor_count, width, height) {
29+
/** @type {[number, number][]} */
30+
const anchors = [[0, 0]];
31+
32+
const totalLength = width * 2 + height * 2;
33+
const gapLength = totalLength / anchor_count;
34+
35+
let currentLength = 0;
36+
37+
for (let i = 1; i < anchor_count; i++) {
38+
currentLength += gapLength;
39+
40+
if (currentLength < width) {
41+
// walk top left to right
42+
anchors.push([currentLength, 0]);
43+
} else if (currentLength < width + height) {
44+
// walk right top to bottom
45+
anchors.push([width, currentLength - (width)]);
46+
} else if (currentLength < width * 2 + height) {
47+
// walk bottom right to left
48+
anchors.push([2 * width - (currentLength - height), height]);
49+
} else {
50+
// walk left bottom to top
51+
anchors.push([0, height + 2 * width - (currentLength - height)]);
52+
}
53+
}
54+
55+
return anchors;
56+
}

0 commit comments

Comments
 (0)