@@ -23,6 +23,42 @@ let UNIT_COORDS = {};
2323let REVERSE_UNIT_DEPS = { } ;
2424let REVERSE_UNIT_RMETA_DEPS = { } ;
2525
26+ const MIN_GRAPH_WIDTH = 200 ;
27+ const MAX_GRAPH_WIDTH = 4096 ;
28+
29+ // How many pixels per second is added by each scale value
30+ const SCALE_PIXELS_PER_SEC = 8 ;
31+
32+ function scale_to_graph_width ( scale ) {
33+ // The scale corresponds to `SCALE_PIXELS_PER_SEC` pixels per seconds.
34+ // We thus multiply it by that, and the total duration, to get the graph width.
35+ const width = scale * SCALE_PIXELS_PER_SEC * DURATION ;
36+
37+ // We then cap the size of the graph. It is hard to view if it is too large, and
38+ // browsers may not render a large graph because it takes too much memory.
39+ // 4096 is still ridiculously large, and probably won't render on mobile
40+ // browsers, but should be ok for many desktop environments.
41+ // Also use a minimum width of 200.
42+ return Math . max ( MIN_GRAPH_WIDTH , Math . min ( MAX_GRAPH_WIDTH , width ) ) ;
43+ }
44+
45+ // This function performs the reverse of `scale_to_graph_width`.
46+ function width_to_graph_scale ( width ) {
47+ const maxWidth = Math . min ( MAX_GRAPH_WIDTH , width ) ;
48+ const minWidth = Math . max ( MIN_GRAPH_WIDTH , width ) ;
49+
50+ const trimmedWidth = Math . max ( minWidth , Math . min ( maxWidth , width ) ) ;
51+
52+ const scale = Math . round ( trimmedWidth / ( DURATION * SCALE_PIXELS_PER_SEC ) ) ;
53+ return Math . max ( 1 , scale ) ;
54+ }
55+
56+ // Init scale value and limits based on the client's window width and min/max graph width.
57+ const scaleElement = document . getElementById ( 'scale' ) ;
58+ scaleElement . min = width_to_graph_scale ( MIN_GRAPH_WIDTH ) ;
59+ scaleElement . max = width_to_graph_scale ( MAX_GRAPH_WIDTH ) ;
60+ scaleElement . value = width_to_graph_scale ( window . innerWidth * 0.75 ) ;
61+
2662// Colors from css
2763const getCssColor = name => getComputedStyle ( document . body ) . getPropertyValue ( name ) ;
2864const TEXT_COLOR = getCssColor ( '--text' ) ;
@@ -318,11 +354,7 @@ function setup_canvas(id, width, height) {
318354
319355function draw_graph_axes ( id , graph_height ) {
320356 const scale = document . getElementById ( 'scale' ) . valueAsNumber ;
321- // Cap the size of the graph. It is hard to view if it is too large, and
322- // browsers may not render a large graph because it takes too much memory.
323- // 4096 is still ridiculously large, and probably won't render on mobile
324- // browsers, but should be ok for many desktop environments.
325- const graph_width = Math . min ( scale * DURATION , 4096 ) ;
357+ const graph_width = scale_to_graph_width ( scale ) ;
326358 const px_per_sec = graph_width / DURATION ;
327359 const canvas_width = Math . max ( graph_width + X_LINE + 30 , X_LINE + 250 ) ;
328360 const canvas_height = graph_height + MARGIN + Y_LINE ;
0 commit comments