Skip to content

Commit 51c451a

Browse files
committed
Update the WASM example
1 parent 2fce4e1 commit 51c451a

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

examples/wasm-demo/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl Chart {
4545
})
4646
}
4747

48-
pub fn plot3d(canvas: HtmlCanvasElement, yaw: f64) -> Result<(), JsValue> {
49-
plot3d::draw(canvas, yaw).map_err(|err| err.to_string())?;
48+
pub fn plot3d(canvas: HtmlCanvasElement, pitch: f64, yaw: f64) -> Result<(), JsValue> {
49+
plot3d::draw(canvas, pitch, yaw).map_err(|err| err.to_string())?;
5050
Ok(())
5151
}
5252

examples/wasm-demo/src/plot3d.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use plotters::prelude::*;
33
use plotters_canvas::CanvasBackend;
44
use web_sys::HtmlCanvasElement;
55

6-
pub fn draw(canvas: HtmlCanvasElement, yaw: f64) -> DrawResult<()> {
6+
pub fn draw(canvas: HtmlCanvasElement, pitch: f64, yaw: f64) -> DrawResult<()> {
77
let area = CanvasBackend::with_canvas_object(canvas)
88
.unwrap()
99
.into_drawing_area();
@@ -17,7 +17,8 @@ pub fn draw(canvas: HtmlCanvasElement, yaw: f64) -> DrawResult<()> {
1717

1818
chart.with_projection(|mut pb| {
1919
pb.yaw = yaw;
20-
pb.scale = 0.8;
20+
pb.pitch = pitch;
21+
pb.scale = 0.7;
2122
pb.into_matrix()
2223
});
2324

examples/wasm-demo/www/index.html

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ <h1>Plotters WebAssembly Demo</h1>
1515
<canvas id="canvas" width="600" height="400"></canvas>
1616
<div id="status">Loading WebAssembly...</div>
1717
<div id="control">
18-
<label for="plot-type">Demo: </label>
19-
<select id="plot-type">
20-
<option value="0">Graph of y=1</option>
21-
<option value="1">Graph of y=x</option>
22-
<option value="2">Graph of y=x^2</option>
23-
<option value="3">Graph of y=x^3</option>
24-
<option value="4">Graph of y=x^4</option>
25-
<option value="5">Graph of y=x^5</option>
26-
<option value="mandelbrot">Mandelbrot Set</option>
27-
<option value="3d-plot">3D Plot Demo</option>
28-
</select>
18+
<label for="plot-type">Demo: </label>
19+
<select id="plot-type">
20+
<option value="0">Graph of y=1</option>
21+
<option value="1">Graph of y=x</option>
22+
<option value="2">Graph of y=x^2</option>
23+
<option value="3">Graph of y=x^3</option>
24+
<option value="4">Graph of y=x^4</option>
25+
<option value="5">Graph of y=x^5</option>
26+
<option value="mandelbrot">Mandelbrot Set</option>
27+
<option value="3d-plot">3D Plot Demo</option>
28+
</select>
29+
<div id="3d-control" class="hide">
30+
<label for="pitch">Pitch: </label> <input type="range" min="0" max="157" id="pitch" value="10"> <br/>
31+
<label for="yaw">Yaw: </label> <input type="range" min="0" max="314" id="yaw" value="50">
32+
</div>
2933
</div>
3034
</main>
3135
<footer>

examples/wasm-demo/www/index.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ class Chart {}
55
const canvas = document.getElementById("canvas");
66
const coord = document.getElementById("coord");
77
const plotType = document.getElementById("plot-type");
8+
const pitch = document.getElementById("pitch");
9+
const yaw = document.getElementById("yaw");
10+
const control = document.getElementById("3d-control");
811
const status = document.getElementById("status");
912

1013
let chart = null;
1114

1215
/** Main entry point */
1316
export function main() {
17+
let hash = location.hash.substr(1);
18+
for(var i = 0; i < plotType.options.length; i++) {
19+
if(hash == plotType.options[i].value) {
20+
plotType.value = hash;
21+
}
22+
}
1423
setupUI();
1524
setupCanvas();
1625
}
@@ -24,6 +33,10 @@ export function setup(WasmChart) {
2433
function setupUI() {
2534
status.innerText = "WebAssembly loaded!";
2635
plotType.addEventListener("change", updatePlot);
36+
yaw.addEventListener("change", updatePlot);
37+
pitch.addEventListener("change", updatePlot);
38+
yaw.addEventListener("input", updatePlot);
39+
pitch.addEventListener("input", updatePlot);
2740
window.addEventListener("resize", setupCanvas);
2841
window.addEventListener("mousemove", onMouseMove);
2942
}
@@ -58,6 +71,13 @@ function onMouseMove(event) {
5871
}
5972
}
6073

74+
function updatePlot3d() {
75+
let yaw_value = Number(yaw.value) / 100.0;
76+
let pitch_value = Number(pitch.value) / 100.0;
77+
Chart.plot3d(canvas, pitch_value, yaw_value);
78+
coord.innerText = `Pitch:${pitch_value}, Yaw:${yaw_value}`
79+
}
80+
6181
/** Redraw currently selected plot. */
6282
function updatePlot() {
6383
const selected = plotType.selectedOptions[0];
@@ -66,24 +86,18 @@ function updatePlot() {
6686
const start = performance.now();
6787
switch(selected.value) {
6888
case "mandelbrot":
89+
control.classList.add("hide");
6990
chart = Chart.mandelbrot(canvas);
7091
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-
}
92+
case "3d-plot":
93+
control.classList.remove("hide");
94+
updatePlot3d();
8295
break;
8396
default:
84-
Chart.power("canvas", Number(selected.value))
97+
control.classList.add("hide");
98+
chart = Chart.power("canvas", Number(selected.value))
8599
}
86-
100+
87101
const end = performance.now();
88102
status.innerText = `Rendered ${selected.innerText} in ${Math.ceil(end - start)}ms`;
89103
}

examples/wasm-demo/www/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ footer {
4848
font-size: 12px;
4949
text-align: center;
5050
}
51+
52+
.hide {
53+
visibility: hidden;
54+
height: 0px;
55+
}

0 commit comments

Comments
 (0)