Hacker News new | ask | show | jobs
by hnlmorg 25 days ago
This looks slick but I think you need a better way to market it.

The 2D canvas part is the norm for anyone who wants performance. Working with the DOM becomes painfully slow and resource intensive the moment you add syntax highlighting, spell checking, and other visual chrome, to large documents.

This is why Monaco (the editor widget behind VSCode) is a canvas. Why terminal emulators built with web technologies use canvas. And so on.

So I wouldn’t say the canvas part of the heading is a differentiator.

Edit: apologies, seems I was wildly wrong here. Please disregard the above.

2 comments

> The 2D canvas part is the norm for anyone who wants performance. Working with the DOM becomes painfully slow and resource intensive the moment you add syntax highlighting, spell checking, and other visual chrome, to large documents.

This is wildly wrong. 2D canvas is consistently slower than letting the browser handle the rendering, so long as you avoid making the DOM too complex, and possibly monitor the viewport and only keep some of the content in the DOM at a time (though for most applications this practice is somewhere between harmful and not useful, and it definitely adds a lot of complexity).

> This is why Monaco (the editor widget behind VSCode) is a canvas.

It isn’t.

> Why terminal emulators built with web technologies use canvas.

Why on earth would you build with web technology, if you’re then going to use canvas? I can’t immediately think of any that match your description (though I have no doubt that in-browser ones using canvas exist).

> This is wildly wrong

You’re right. I’ve been experimenting with Monaco lots lately and could have sworn it used a canvas. But it doesn’t.

> 2D canvas is consistently slower than letting the browser handle the rendering, so long as you avoid making the DOM too complex, and possibly monitor the viewport and only keep some of the content in the DOM at a time (though for most applications it’s somewhere between harmful and not useful, and it definitely adds a lot of complexity).

Interesting. I’ve found the reverse to be true. But it sounds like this is human error on my part. (probably doesn’t help that I’ve had more experience building high performance GPU applications in Dx, SDL, and similar than I have building high performance web apps.

Thanks for the correction.

> Why on earth would you build with web technology, if you’re then going to use canvas? I can’t immediately think of any that match your description (though I have no doubt that in-browser ones using canvas exist).

You answered your own question.

And I do know of at least one written using the canvas. Because I wrote it. I’m now interested in how others work though.

Most in-browser terminal emulators just use plain old boring DOM, and ignore the issue of perfect columns. Some detect it and wrap offending characters in fixed-width spans. But the entire concept of columns is unsound, doesn’t work at all for a lot of scripts.
It might seem unsound but it’s the concept we’ve built our terminals around.

And there are already definitions and APIs to decide the terminal cell width of different Unicode characters.

I’m not saying it’s easy, because you end up with 3 different string widths:

1. memory length in bytes

2. Number of Unicode characters (which is annoyingly also different to the number of Unicode code points since some codes can be combined)

3. terminal cell width

But as annoying as it is, there are a few relatively mature libraries you can use to solve this problem.

> And there are already definitions and APIs to decide the terminal cell width of different Unicode characters.

And how! I know of four different and mutually-incompatible definitions/behaviours, and I’m confident there will be more; and once you get to things like Indic scripts and Right-to-Left there are no good answers, and no cell-based terminal produces even vaguely acceptable results.

As for meanings of string width, I’d count at least four: Unicode code units, Unicode scalar values, Unicode extended grapheme clusters, and terminal cells.

> And how! I know of four different and mutually-incompatible definitions/behaviours, and I’m confident there will be more;

I’ve not found that to be the case. Usually the issue is more around different libraries being out of date (ie pinned to an earlier version of Unicode)

> and once you get to things like Indic scripts and Right-to-Left there are no good answers, and no cell-based terminal produces even vaguely acceptable results.

That’s because right-to-left isn’t supported and in my opinion shouldn’t be supported my the terminal emulator itself.

Right-to-left support would be handled by the client application, not the terminal emulator. The terminal is just there to offer a grid of Unicode cells. It’s not a writing composition suite.

Just like how the terminal emulator isn’t readline, it isn’t emacs, it isn’t hexdump, and it isn’t vim.

> As for meanings of string width, I’d count at least four: Unicode code units, Unicode scalar values, Unicode extended grapheme clusters, and terminal cells.

I’d already touched on the fact that Unicode widths can vary depending on what you’re measuring. But your Unicode library would/should handle most of the pain there.

When writing software for a terminal, you’re really only interested in the characters you need to loop through, their cell width, and, in some languages, the number of bytes.

As I’ve said previously, I’ve written terminal emulators targeting a variety of different renderers like SDL (so not just js+canvas), as well as complex terminal software like $SHELLs. From what I’ve built over the decades, honestly Unicode support isnt as big problem as it used to be. Sure, there’s plenty of footguns that you need to be aware of, but that’s just the nature of working with technology originally designed in the 60s/70s. Like how you need to be aware of cell widths if you intend to do any manual text alignment in client software. But at least there are libraries to make that task easier.

And if you don’t case about text alignment, then the terminal emulator will handle that for you.

As far as I know, Monaco, the editor behind VS Code, does not use canvas. I noticed that Google Docs uses canvas, and that was the start of my project a year ago. I think that if I had used another context, like WebGL or WebGPU, it would have been much more performant, but it is really hard to render text and parse font files that way.

Anyway, I have gotten good enough results, and I don’t think normal usage will exceed the limits of the 2D canvas.