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;
33
44mod func_plot;
55mod mandelbrot;
6+ mod plot3d;
67
78#[ global_allocator]
89static 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 > {
Original file line number Diff line number Diff line change 11use crate :: DrawResult ;
22use plotters:: prelude:: * ;
3+ use plotters_canvas:: CanvasBackend ;
34use std:: ops:: Range ;
45use web_sys:: HtmlCanvasElement ;
5- use plotters_canvas:: CanvasBackend ;
66
77/// Draw Mandelbrot set
88pub 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>
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 >
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments