Skip to content

Plot: how to make text and stroke scale with zoom? #3277

Answered by YgorSouza
tgross35 asked this question in Q&A
Discussion options

You must be logged in to vote

You could do something like this:

let scale = plot_ui.transform().rect_from_values(&[0.0, 0.0].into(), &[1.0, 1.0].into()).size();
let text_size = scale.max_elem().clamp(0.001, 250.0);
let stroke_width = scale.max_elem().clamp(0.001, 2500.0) * 0.1;
plot_ui.text(Text::new([0.0, 0.0].into(), RichText::new("Plot Text").size(text_size)));
plot_ui.polygon(Polygon::new(PlotPoints::new(vec![[-4.0, -1.5], [4.0, -1.5], [4.0, 1.5], [-4.0, 1.5]])).stroke(Stroke::new(stroke_width, Color32::BLUE)));

You have to limit the font size because egui will panic if it is too large (250 is already pushing it). And you have to decide what should happen if the X and Y scales are different, since you can't distor…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tgross35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants