Let there be color in the browser
Filed under: Development, Firefox, GUI, Technical
By Fabian Jakobs @ July 7, 2008 10:01 pm
I always wanted to play with the browser's canvas element but never really found the right toy project. Then I read Ariya Hidayat's blog post "Let there be color". He has implemented the HSL color pie using Qt's 2D drawing canvas. How hard would it be to put something like this into the browser? I decided to try the port of his code to JavaScript and render the pie using only canvas. If this worked out maybe I could embed it into nice little qooxdoo windows.
Once I figured out how to realize "putPixel" and "getPixel" functions in canvas the port was merely a copy and paste with some minor changes. Right now I use the canvas methods "getImageData" and "putImageData" to obtain and render a pixel buffer. Unfortunately these methods are only available in Firefox. I'm still looking for an alternative for Opera and Safari. The last missing part was the conversion from HSV to RGB but luckily I could drop in a modified version of qooxdoo's hsbToRgb method.
Now the port could start. What really intrigued me was that the main algorithm was nearly the same as the C++ code. Just take this code fragment from the original code:
for (int i = 0; i < radius; i++) { qreal hue = 1 - init; qreal sat = 1 - qreal(i) / radius; for (int d = 0; d < depth; d++) { qreal value = 1 - qreal(d) / depth; QColor color = QColor::fromHsvF(hue, sat, value); img->setPixel(width / 2 - radius + i + 1, center + d, color.rgb()); } }
and compare it to the ported JavaScript:
for (var i = 0; i < radius; i++) { var hue = 1 - init; var sat = 1 - i / radius; for (var d = 0; d < depth; d++) { var value = 1 - d / depth; var color = hsbToRgb(hue, sat, value); setPixel(img, width / 2 - radius + i + 1, center + d, color); } }
Basically only the variable declaration is different. The same is true for almost all of the relevant code. My first version was an all in one HTML file with the JavaScript code embedded. I used no framework, just plain JavaScript.
Of course I wanted to use some qooxdoo, so the next iteration was to put this into a qooxdoo application. Up to now qooxdoo had no support for embedding canvas elements into a qooxdoo widget. I used much of the new 0.8 widget infrastructure to create a canvas embedding widget. With this widget I could take the code from my first version and embed it into a qooxdoo window. You can see this application life or download the sources. This code is based on the latest qooxdoo 0.8 trunk.
Have fun!


Comment by Wayne Si
Nice job but doesn’t work on IE. I have a feeling that the curent qooxdoo development is focusing on Firefox or WebKit families more and more, say source code tab in Demo Browser (v0.8) stopped working in IE for a long time last month. I don’t think it’s some kind of good sign on this cross-browser framework.
July 9, 2008 11:00 am
Comment by Fabian Jakobs
It is an experiment of mine and I did not take cross browser issues into account. As a matter of fact, the program currently does only work in Firefox and you need Firefox 3 to get decent performance.
This is definitively not the direction of qooxdoo. We take IE support very seriously and any qooxdoo 0.8 final will have full support for IE 6/7, Opera 9.5, Firefox and Safari 3. As we use Firefox as our main development platform other browsers may lag a bit behind during development but not in the final release.
July 9, 2008 12:10 pm
Comment by Andreas Ecker
Hi Wayne, I’d also like to ensure you that qooxdoo continues to be fully cross-browser. Don’t know why this should be at question at all??
qooxdoo offers cross-browser look&feel, development support and API, even for advanced features that you’ll hardly find in other frameworks. Of course, the development snapshots (or the intermediate releases before 0.8 final) are not always fully functional (not browser-specific), but that is nothing to worry about. If you find any issues, please report them at http://bugzilla.qooxdoo.org . Thanks.
July 10, 2008 9:24 pm
Comment by petr
in HsvPie constructor is:
this.set({
syncDimension : false,
canvasHeight : width,
canvasWidth : height
});
is this correct
?
July 14, 2008 11:32 pm
Comment by Fabian Jakobs
Yes that is correct. The trick in this application is to never change the internal dimension of the canvas by setting “syncDimension” to false. This way the contents of the canvas is scaled to the size of the outer DOM element and no redraw is needed. The redraw is really expensive as you can see be the delay at page load.
July 15, 2008 9:07 am
Pingback by Bookmarks about Javascript
[...] – bookmarked by 4 members originally found by konno110 on 2008-10-15 Let there be color in the browser http://news.qooxdoo.org/let-there-be-color-in-the-browser – bookmarked by 4 members originally [...]
November 3, 2008 11:00 am