News for hackers. Stuff that matters.
Re:Show HN: Alter – convert text/code to an image by yorwba on Saturday November 26, 2016 @04:33AM (#13042702)
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 and compare with a line containing the same number of Latin characters.

To also say something positive: At least it looks nice!

Re:Show HN: Alter – convert text/code to an image by kbr on Saturday November 26, 2016 @11:39AM (#13044466)
Thank 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

Re:Show HN: Alter – convert text/code to an image by samstave on Saturday November 26, 2016 @01:16PM (#13045062)
Dude - you're 13? You're awesome! Keep at it!
Re:Show HN: Alter – convert text/code to an image by kbr on Saturday November 26, 2016 @02:31PM (#13045437)
Thanks :D
Re:Show HN: Alter – convert text/code to an image by nunull on Saturday November 26, 2016 @06:53AM (#13043057)
Regarding 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.
Re:Show HN: Alter – convert text/code to an image by madflame991 on Saturday November 26, 2016 @08:35AM (#13043424)
No need to mutate/slice/sort anything

    Math.max(...lines.map((line) => ctx.measureText(line).width))
Re:Show HN: Alter – convert text/code to an image by ola on Saturday November 26, 2016 @08:29AM (#13043397)
I am of the same opinion, always go for correctness before performance, efficiency can be tuned later if it's even needed.
Re:Show HN: Alter – convert text/code to an image by bbcbasic on Saturday November 26, 2016 @04:12PM (#13045888)
Optimisation 1 might be premature. If most cases the text is short then the functional approach is arguably nicer even if a tad slower.
Re:Show HN: Alter – convert text/code to an image by zo1 on Saturday November 26, 2016 @06:29AM (#13042995)
Regarding 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.
Re:Show HN: Alter – convert text/code to an image by sanxiyn on Saturday November 26, 2016 @06:58AM (#13043081)
No, you are wrong. Unicode characters can be halfwidth or fullwidth even for monospaced fonts.
Re:Show HN: Alter – convert text/code to an image by zo1 on Saturday November 26, 2016 @05:37PM (#13046343)
Okay - I didn't know about those. I stand corrected.
Re:Show HN: Alter – convert text/code to an image by luchs on Saturday November 26, 2016 @08:42AM (#13043450)
Here's an example where it's broken: https://i.imgur.com/ugVBCJL.png
Re:Show HN: Alter – convert text/code to an image by kbr on Saturday November 26, 2016 @11:40AM (#13044476)
Would it be a bit better if I go through each line and instead of getting the length, get the actual width of it?
Re:Show HN: Alter – convert text/code to an image by nkkollaw on Saturday November 26, 2016 @04:44AM (#13042729)
How would you have written that?
Re:Show HN: Alter – convert text/code to an image by gumoro on Saturday November 26, 2016 @06:53AM (#13043054)
For point 1 I would use "reduce":

  lines.reduce((longest, line) => (longest.length > line.length) ? longest : line, "")
https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...

EDIT: sorry, initial code returned length, not line itself

Re:Show HN: Alter – convert text/code to an image by nunull on Saturday November 26, 2016 @06:54AM (#13043060)
This returns the length, not the line itself.

Edit: `lines.reduce((line, line_) => line_.length > line.length ? line_ : line, '')`

Re:Show HN: Alter – convert text/code to an image by gumoro on Saturday November 26, 2016 @06:58AM (#13043079)
Thanks, edited.
Re:Show HN: Alter – convert text/code to an image by paradite on Saturday November 26, 2016 @01:53PM (#13045275)
It 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.