Skip to content

Commit 89880b7

Browse files
committed
Update the wasm example
1 parent 1c74585 commit 89880b7

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

examples/wasm-demo/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ edition = "2018"
88
crate-type=["cdylib"]
99

1010
[dependencies]
11-
plotters = {path = "../..", default_features = false, features = ["line_series"] }
11+
plotters = {path = "../.."}
1212
plotters-canvas = "0.1.*"
13-
wasm-bindgen = "0.2"
13+
wasm-bindgen = "0.2.62"
1414
wee_alloc = "0.4.5"
1515
web-sys = { version = "0.3.39", features = ["HtmlCanvasElement"] }
1616

examples/wasm-demo/src/func_plot.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ pub fn draw(canvas_id: &str, power: i32) -> DrawResult<impl Fn((i32, i32)) -> Op
1111
root.fill(&WHITE)?;
1212

1313
let mut chart = ChartBuilder::on(&root)
14+
.margin(20)
1415
.caption(format!("y=x^{}", power), font)
1516
.x_label_area_size(30)
1617
.y_label_area_size(30)
17-
.build_ranged(-1f32..1f32, -1.2f32..1.2f32)?;
18+
.build_cartesian_2d(-1f32..1f32, -1.2f32..1.2f32)?;
1819

1920
chart.configure_mesh().x_labels(3).y_labels(3).draw()?;
2021

examples/wasm-demo/src/mandelbrot.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::DrawResult;
2+
use plotters::prelude::*;
23
use std::ops::Range;
34
use web_sys::HtmlCanvasElement;
4-
5-
use plotters::prelude::*;
65
use plotters_canvas::CanvasBackend;
76

87
/// Draw Mandelbrot set
@@ -16,7 +15,7 @@ pub fn draw(element: HtmlCanvasElement) -> DrawResult<impl Fn((i32, i32)) -> Opt
1615
.margin(20)
1716
.x_label_area_size(10)
1817
.y_label_area_size(10)
19-
.build_ranged(-2.1..0.6, -1.2..1.2)?;
18+
.build_cartesian_2d(-2.1..0.6, -1.2..1.2)?;
2019

2120
chart
2221
.configure_mesh()

examples/wasm-demo/start-server.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ wasm-pack build --release
44
if errorlevel 1 cargo install wasm-pack
55
wasm-pack build --release
66
cd www
7-
npm install
7+
call npm install
88
npm start

examples/wasm-demo/www/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<main>
1313
<h1>Plotters WebAssembly Demo</h1>
1414
<div id="coord"></div>
15-
<canvas id="canvas" width="600" height="400"></canvas>
15+
<canvas id="canvas" width="600" height="400"></canvas>
1616
<div id="status">Loading WebAssembly...</div>
1717
<div id="control">
1818
<label for="plot-type">Demo: </label>

examples/wasm-demo/www/index.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,31 @@ function setupUI() {
3030

3131
/** Setup canvas to properly handle high DPI and redraw current plot. */
3232
function setupCanvas() {
33-
const dpr = window.devicePixelRatio || 1;
33+
const dpr = window.devicePixelRatio || 1.0;
3434
const aspectRatio = canvas.width / canvas.height;
35-
const size = Math.min(canvas.width, canvas.parentNode.offsetWidth);
35+
const size = canvas.parentNode.offsetWidth * 0.8;
3636
canvas.style.width = size + "px";
3737
canvas.style.height = size / aspectRatio + "px";
38-
canvas.width = size * dpr;
39-
canvas.height = size / aspectRatio * dpr;
40-
canvas.getContext("2d").scale(dpr, dpr);
38+
canvas.width = size;
39+
canvas.height = size / aspectRatio;
4140
updatePlot();
4241
}
4342

4443
/** Update displayed coordinates. */
4544
function onMouseMove(event) {
4645
if (chart) {
47-
const point = chart.coord(event.offsetX, event.offsetY);
48-
coord.innerText = (point)
49-
? `(${point.x.toFixed(3)}, ${point.y.toFixed(3)})`
50-
: "Mouse pointer is out of range";
46+
var text = "Mouse pointer is out of range";
47+
48+
if(event.target == canvas) {
49+
let actualRect = canvas.getBoundingClientRect();
50+
let logicX = event.offsetX * canvas.width / actualRect.width;
51+
let logicY = event.offsetY * canvas.height / actualRect.height;
52+
const point = chart.coord(logicX, logicY);
53+
text = (point)
54+
? `(${point.x.toFixed(3)}, ${point.y.toFixed(3)})`
55+
: text;
56+
}
57+
coord.innerText = text;
5158
}
5259
}
5360

0 commit comments

Comments
 (0)