Skip to content

Commit 2fce4e1

Browse files
committed
Add 3d plot to example
1 parent c03b70a commit 2fce4e1

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

examples/wasm-demo/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use web_sys::HtmlCanvasElement;
33

44
mod func_plot;
55
mod mandelbrot;
6+
mod plot3d;
67

78
#[global_allocator]
89
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
@@ -44,6 +45,11 @@ impl Chart {
4445
})
4546
}
4647

48+
pub fn plot3d(canvas: HtmlCanvasElement, yaw: f64) -> Result<(), JsValue> {
49+
plot3d::draw(canvas, yaw).map_err(|err| err.to_string())?;
50+
Ok(())
51+
}
52+
4753
/// This function can be used to convert screen coordinates to
4854
/// chart coordinates.
4955
pub fn coord(&self, x: i32, y: i32) -> Option<Point> {

examples/wasm-demo/src/mandelbrot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::DrawResult;
22
use plotters::prelude::*;
3+
use plotters_canvas::CanvasBackend;
34
use std::ops::Range;
45
use web_sys::HtmlCanvasElement;
5-
use plotters_canvas::CanvasBackend;
66

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

examples/wasm-demo/src/plot3d.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::DrawResult;
2+
use plotters::prelude::*;
3+
use plotters_canvas::CanvasBackend;
4+
use web_sys::HtmlCanvasElement;
5+
6+
pub fn draw(canvas: HtmlCanvasElement, yaw: f64) -> DrawResult<()> {
7+
let area = CanvasBackend::with_canvas_object(canvas)
8+
.unwrap()
9+
.into_drawing_area();
10+
area.fill(&WHITE)?;
11+
12+
let x_axis = (-3.0..3.0).step(0.1);
13+
let z_axis = (-3.0..3.0).step(0.1);
14+
15+
let mut chart =
16+
ChartBuilder::on(&area).build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;
17+
18+
chart.with_projection(|mut pb| {
19+
pb.yaw = yaw;
20+
pb.scale = 0.8;
21+
pb.into_matrix()
22+
});
23+
24+
chart.configure_axes().draw()?;
25+
26+
chart.draw_series(SurfaceSeries::<f64, _, f64>::new(
27+
x_axis.values(),
28+
z_axis.values(),
29+
|&x, &z| (x * x + z * z).cos(),
30+
&BLUE.mix(0.2),
31+
))?;
32+
33+
chart.draw_series(LineSeries::new(
34+
(-100..100)
35+
.map(|y| y as f64 / 40.0)
36+
.map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
37+
&BLACK,
38+
))?;
39+
40+
Ok(())
41+
}

examples/wasm-demo/www/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h1>Plotters WebAssembly Demo</h1>
2424
<option value="4">Graph of y=x^4</option>
2525
<option value="5">Graph of y=x^5</option>
2626
<option value="mandelbrot">Mandelbrot Set</option>
27+
<option value="3d-plot">3D Plot Demo</option>
2728
</select>
2829
</div>
2930
</main>

examples/wasm-demo/www/index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,26 @@ function updatePlot() {
6464
status.innerText = `Rendering ${selected.innerText}...`;
6565
chart = null;
6666
const start = performance.now();
67-
chart = (selected.value == "mandelbrot")
68-
? Chart.mandelbrot(canvas)
69-
: Chart.power("canvas", Number(selected.value));
67+
switch(selected.value) {
68+
case "mandelbrot":
69+
chart = Chart.mandelbrot(canvas);
70+
break;
71+
case "3d-plot": {
72+
var yaw = 0;
73+
var update = function() {
74+
if(plotType.selectedOptions[0].value != "3d-plot")
75+
return;
76+
Chart.plot3d(canvas, yaw);
77+
yaw += 3.14 / 200;
78+
setTimeout(update, 50);
79+
};
80+
update();
81+
}
82+
break;
83+
default:
84+
Chart.power("canvas", Number(selected.value))
85+
}
86+
7087
const end = performance.now();
7188
status.innerText = `Rendered ${selected.innerText} in ${Math.ceil(end - start)}ms`;
7289
}

0 commit comments

Comments
 (0)