File tree Expand file tree Collapse file tree 5 files changed +69
-4
lines changed Expand file tree Collapse file tree 5 files changed +69
-4
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use web_sys::HtmlCanvasElement;
3
3
4
4
mod func_plot;
5
5
mod mandelbrot;
6
+ mod plot3d;
6
7
7
8
#[ global_allocator]
8
9
static ALLOC : wee_alloc:: WeeAlloc = wee_alloc:: WeeAlloc :: INIT ;
@@ -44,6 +45,11 @@ impl Chart {
44
45
} )
45
46
}
46
47
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
+
47
53
/// This function can be used to convert screen coordinates to
48
54
/// chart coordinates.
49
55
pub fn coord ( & self , x : i32 , y : i32 ) -> Option < Point > {
Original file line number Diff line number Diff line change 1
1
use crate :: DrawResult ;
2
2
use plotters:: prelude:: * ;
3
+ use plotters_canvas:: CanvasBackend ;
3
4
use std:: ops:: Range ;
4
5
use web_sys:: HtmlCanvasElement ;
5
- use plotters_canvas:: CanvasBackend ;
6
6
7
7
/// Draw Mandelbrot set
8
8
pub fn draw ( element : HtmlCanvasElement ) -> DrawResult < impl Fn ( ( i32 , i32 ) ) -> Option < ( f64 , f64 ) > > {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ <h1>Plotters WebAssembly Demo</h1>
24
24
< option value ="4 "> Graph of y=x^4</ option >
25
25
< option value ="5 "> Graph of y=x^5</ option >
26
26
< option value ="mandelbrot "> Mandelbrot Set</ option >
27
+ < option value ="3d-plot "> 3D Plot Demo</ option >
27
28
</ select >
28
29
</ div >
29
30
</ main >
Original file line number Diff line number Diff line change @@ -64,9 +64,26 @@ function updatePlot() {
64
64
status . innerText = `Rendering ${ selected . innerText } ...` ;
65
65
chart = null ;
66
66
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
+
70
87
const end = performance . now ( ) ;
71
88
status . innerText = `Rendered ${ selected . innerText } in ${ Math . ceil ( end - start ) } ms` ;
72
89
}
You can’t perform that action at this time.
0 commit comments