5 ms·
I looked at the code. var lineLengthOrder = lines.slice(0).sort(function(a, b) { return b.length - a.length; }); ctx.canvas.width = ctx.measureText
by yorwba 10y ago
I looked at the code.
var lineLengthOrder = lines.slice(0).sort(function(a, b) {
return b.length - a.length;
});
ctx.canvas.width = ctx.measureText(lineLengthOrder[0]).width + 25;
Ok, written by a beginner. A few tips:
1. Sorting all the lines just to get the longest is pretty wasteful. Try an explicit loop that iterates over the array once, keeping track of the longest line seen so far.
2. Using measureText to get the visible length of a line is a good idea, but doesn't mesh with using the .length property to find the longest line. Some characters may be wider than others, so a line containing fewer characters (shorter .length) could actually be longer (using measureText) than the one with the most characters. Try copy-pasting some Chinese from https://zh.wikipedia.org https://zh.wikipedia.org and compare with a line containing the same number of Latin characters.
To also say something positive: At least it looks nice!
- nkkollaw 10y agoHow would you have written that?
- gumoro 10y agoFor point 1 I would use "reduce": lines.reduce((longest, line) => (longest.length > line.length) ? longest : line, "") https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... EDIT: sorry, initial code returned length, not line itself
- nunull 10y agoThis returns the length, not the line itself. Edit: `lines.reduce((line, line_) => line_.length > line.length ? line_ : line, '')`
- gumoro 10y agoThanks, edited.
- paradite 10y agoIt is a good use of reduce (accumulate as I call it), but maybe slightly overkill for this case compared to a simple iteration or a max function.
- zo1 10y agoRegarding point number 2, it should be noted that this only applies for non mono-spaced fonts. If you were using a mono-spaced font, then each character glyph uses the same physical canvas width as all others.
- sanxiyn 10y agoNo, you are wrong. Unicode characters can be halfwidth or fullwidth even for monospaced fonts.
- zo1 10y agoOkay - I didn't know about those. I stand corrected.
- luchs 10y agoHere's an example where it's broken: https://i.imgur.com/ugVBCJL.png https://i.imgur.com/ugVBCJL.png
- kbr 10y agoWould it be a bit better if I go through each line and instead of getting the length, get the actual width of it?
- nunull 10y agoRegarding 1: I think the sorting approach is actually really nice in this case. Clearly it's not as efficient as a simple loop, but it's safer and more readable. Explicit looping and mutation can introduce nasty bugs which this kind of declarative approach can't.
- ola 10y agoI am of the same opinion, always go for correctness before performance, efficiency can be tuned later if it's even needed.
- madflame991 10y agoNo need to mutate/slice/sort anything Math.max(...lines.map((line) => ctx.measureText(line).width))
- kbr 10y agoThank you so much! I'm not the best at HTML5 canvas, more of front end. (I'm also 13). I'll iterate through the array instead. If you have any other suggestions, the repo is at https://github.com/KingPixil/alter https://github.com/KingPixil/alter
- bbcbasic 10y agoOptimisation 1 might be premature. If most cases the text is short then the functional approach is arguably nicer even if a tad slower.