-
Notifications
You must be signed in to change notification settings - Fork 12k
Create standardized text render method #8227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create standardized text render method #8227
Conversation
|
I like this. |
|
Maybe it can be used in annotation plugin for the labels |
|
I added in some tests. I also created a simple fiddle to ensure that my bounding box methodology is correct across a wide variety of alignments and baselines. https://jsfiddle.net/4j5dqph0/ |
|
@etimberg @kurkle That's just a thought about the signature of To draw a text, you should set the canvas context font in advance, that means this implementation is assuming that whoever is using it must set the font before the call.. Furthermore the Why don't to pass the font instance as argument to the method in order to:
Something like that: export function renderText(
ctx: CanvasRenderingContext2D,
text: string | string[],
x: number,
y: number,
font: FontSpec,
opts?: RenderTextOpts
): void;In this way the method is managing all statements to render the text. Maybe it could add another method with the font as argument (and leaving this one as is) which will invoke the current one. /**
* Render text onto the canvas with font
*/
export function renderTextWithFont(ctx, text, x, y, font, opts = {}) {
if (font) {
ctx.font = font.string;
}
renderText(ctx, text, x, y, font.lineHeight, opts);
}I don't know exactly the community approach about the granularity of the methods. Maybe the approach is do not have methods like that and delegate the caller to do this kind of stuff. What do you think? |
|
I think that could work @stockiNail, the reason I didn't was mostly the performance aspect (potentially setting the font more times than needed) but as you mentioned, we already have to set the for each tick individually so having that in the API would be nicer. I would be OK to pass the font in, but if we make that change I would consider adding the styles to the |
@etimberg I fully agree! Also for those styles we should take care about the performance, as mentioned. Maybe the colors (for instance) are not used only the text but also for shapes but as you propose they could be optional and when you are in that case, the styles won't be passed. |
|
Ok, I made some good progress. I started by moving the styling into Size Change: +541 B (0%) Total Size: 216 kB
|
|
I like it! I'm looking forward for using it in annotation plugin |
|
I am using renderText() but seems like the values I pass to font are not taken into consideration. Is that a bug? |
Creates a new helper,
renderTextwhich should take care of drawing (including stroke), strikethrough, and underlines.If we like where this is going, I'll add some tests.