(text will look like this) |
Although it isn't apparent, AJAX can draw as well, and in this case study we'll see how. Our goal here is to build an online whiteboard application, where your visitors can draw and publicly express their artistic skills.
Implementing a whiteboard certainly isn't the best real-world scenario for using AJAX, because technologies such as Flash or Java do a much better job at handling complex graphics. However, implementing a whiteboard with AJAX allows us to study some of the more sensitive areas of AJAX development, such as:
For the whiteboard application we'll build in this chapter, we have the following requirements:
One might say that these are the characteristics of a desktop application, more or less. One well-known example of a whiteboard is NetMeeting from Microsoft. Popular instant messenger applications such as Yahoo Messenger and MSN Messenger support this feature as well. After having established the base for developing this application, we need to examine the context: What does JavaScript have to say about graphics?
JavaScript isn't very effective at drawing with HTML because HTML wasn't built to draw graphics; hence we need to find "special" ways to draw into the web page.
It's worth taking a look at the Vector Graphics Library that can be found at http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm. This library provides a large number of basic functions for drawing, such as: setColor, setStroke, drawLine, drawPolyline, drawRect, fillRect, drawPolygon, fillPolygon, drawEllipse, fillEllipse, setFont, drawString, and drawRectString. These functions provide most functionality that you need for basic drawings. The technique behind this library resides on building many div elements that form the desired shapes. Besides using fast Bresenham algorithms to compute the shapes, it also tries to minimize the number of div elements that are used for creating a shape by grouping pixels together.
An alternative to using div elements in drawing is using Scalable Vector Graphics (SVG). You'll learn how to work with SVG in Chapter 7, AJAX Real-Time Charting with SVG.
Before getting our hands on the source code and on the theoretical aspects of this application, let's see what the main technical features are:
In order to have this example working, you need to enable support for the GD PHP library. The installation instructions in Appendix A include support for the GD library.
The application you'll create will look as the one in Figure 2. Keep in mind that a JavaScript whiteboard will never work as well as a Java or Flash whiteboard. The mission of the case study is to analyze AJAX-related programming techniques.The complete AJAX and PHP tutorial features even more AJAX web development examples.
PHP developer? Learn PHP SEO programming. (ASP.NET SEO tutorial forthcoming)