Wednesday, February 18th, 2009
>The following is a lightly edited repost from an entry on my personal blog.
Culling through the Bespin feedback we received post-launch, it's clear that a few aspects of the project are controversial. I'd like to address one of them: our choice of canvas as the rendering engine for the editor.
(In addition to feedback, we've already seen some interesting community efforts at extending Bespin; check out our blog post on the Labs website for details.)
Bespin and Canvas
// all drawing performed via the context
var ctx = document.getElementById("canvasId").getContext("2d");
ctx.fillStyle = "black"; // accepts same values as CSS "background" property
ctx.fillRect(0, 0, 50, 50); // draws a black 50x50 rectange in the canvas
Canvas is available in the currently released versions of Safari, Opera, Firefox, and Chrome; it has been a part of the HTML 5 draft specification for a long time. It is not available in Internet Explorer.
Bespin builds on this API to recreate a text editor from scratch. That's right; rather than use the native facilities of every modern operating system that give you word processing features for free, Bespin rolls them from scratch, including such basic constructs as a blinking cursor and text selection.
One of the the features we didn't have to re-implement is text rendering; the HTML 5 specification includes an API to render text on a canvas. Unfortunately, this portion of the canvas API has only been implemented by Firefox, though it's already in WebKit nightly builds (and thus will very likely be in a future version of Safari) and Chromium nightlies (from which Chrome versions are derived). We expect Opera will soon as well.
But Does It Scale?
To be sure, for some users, the performance does indeed suck–we're looking into exactly why (it appears that certain video drivers cause performance problems on Linux; Google Chrome's multi-process architecture appears to slow down performance on that browser). The interesting thing is that for large classes of users, performance is really, really good. There's a lot to say about topic; we'll touch on that more in a future blog post.
The first time we demoed Bespin to someone who understand these implementation details, the reaction was "Wow, you're crazy!" followed by "That's really cool." Pretty much everyone else I've spoken with has the same first reaction (the crazy bit) but the follow-up is not always positive (ranging from the aforementioned to "WTF!?").
Surely there's a higher-level abstraction we could have built on? Something above pixel painting?
Well, let's get one thing out of the way first-up: there may very well be a more effective way to do what we've done than with canvas. I'm not saying this is the best way to do it; I just want to explain why we did it.
Control, Control, We Must Have Control
One of our goals with Bespin is to be able to (at some point in the future) deliver a fantastically compelling user experience for editing code on the Web. We don't want Bespin to be the editor you use when you can't get to your desktop; we want it to be your editor of choice in any context.
It turns out that's a tall order. Some users like word-wrapping; some don't. Some users actually like column selection modes while other (more sane) users like line-oriented selection (I kid, I kid). Developers are notoriously finicky with their tools–especially their code editor–and we didn't want to re-educate the world on how our editor worked; we want to make Bespin adaptable to as wide an audience as we could.
We felt that if we built Bespin on something like <textarea> or the "content editable" feature of HTML, we'd have a built-in set of limitations over which we'd have very little or no control.
To build on the last paragraph, we considered three different alternatives to canvas:
- "line blocks"
The "control" argument above is one reason we dismissed textarea and contentEditable; further, we've seen a large number of other web editor implementations based on these approaches and based on playing with those and by the many horror stories floating around the Web from their authors, we were concerned that we couldn't overcome performance and compatibility problems, especially when editing large files.
"Line blocks" are a different story. What if we had a bunch of <div> elements stacked vertically and treated each one as a rendering surface for a line of text in the document being edited? We played with that approach a bit but couldn't get as much performance out of it as we could with canvas, especially for wide files (>80 columns).
We've taken some flak for claiming that Bespin is based on "Open Web technologies" but only supporting Firefox and WebKit nightly. Earlier in this post, I explained the state of canvas support in the browsers and how we're hopeful that our canvas approach will work in future versions of Chrome, Safari, and Opera.
However, we have a plan in case browsers don't implement the proper text rendering APIs: bitmap font rendering. By pre-rendering fonts into images, we can then render the images on the canvas. There are a number of proof-of-concept implementations floating around that demonstrate this concept (though unlike some, we wouldn't render the fonts as vectors–there's no way it would perform to our needs).
What about Internet Explorer? It's hard to say what the future of canvas support is for IE; it certainly isn't coming in IE8. However, there are a number of projects that emulate canvas by mapping it to VML (an abandoned IE proprietary rendering technology), Flash, and Silverlight. So far, none of these bridges offers adequate performance for us to seriously considering supporting it. We're aware of promising developments in this area and we'll experiment with IE support as these technologies evolve.
Mobile Support: iPhone, Android, Fennec, and More
We're also very excited to get Bespin working on mobile devices with fully-featured browsers. The iPhone's browser–"mobile Safari"–does support canvas, but does not yet have the text rendering APIs. We may implement the aforementioned bitmap font rendering just to get on the iPhone. We are also looking at other mobile platforms, such as Android, Fennec, Palm, and so forth.
How cool would it be to change your code and redeploy it from the beach?
Obviously, it will take some interface changes for Bespin to be usable on a mobile device. We have some ideas here and we look forward to working with the community on developing the concepts further.
What do you think?
What do you think of our use of canvas in Bespin's editor?
Posted by Ben Galbraith at 8:00 am