- Drawing in a Page
- Understanding the Canvas
- Drawing a Bzier Curve
- Browser Support
Drawing a Bézier Curve
Bézier curves are one of the most important drawing primitives in the PostScript model. Béziers curves are defined by two end points and an arbitrary number of control points. PostScript only supports cubic Béziers, which are defined by two control points. The gradient at one end of the curve is equal to the gradient of the line defined by the first control point and the first end point. The gradient at the other end is defined similarly by the other two points.
PostScript fonts are just simple PostScript programs, and therefore generally construct individual glyphs from Bézier curves. This technique was adopted by other font formats, and now rendering text is one of the most common uses for Bézier curves. You can represent any shape using a sequence of Bézier curves, although it's computationally convenient to treat some special casessuch as straight linesdifferently.
The <canvas> tag also has a special case for quadratic Bézier curves, which are equivalent to cubic Béziers in which both of the control points are the same. A straight-line segment, sometimes referred to as a linear Bézier curve, has no control points other than the end points, and is equivalent to a quadratic Bézier with the control point halfway along its length, or a cubic Bézier with both control points halfway along its length.
To see Bézier curves in action, open the bezier.html file. This simple demonstration of the <canvas> tag allows you to define a path by clicking the four control points.
One thing that you'll notice about this example is that the shapes remain on the canvas indefinitely. The canvas uses a buffered drawing model, so you don't need to worry about redrawing in response to expose events or something similar.
The drawing code in this example runs in response to a click event. The canvas is initialized in this function:
function load() { canvas = document.getElementById('canvas'); canvas.addEventListener('click', click, false); context = canvas.getContext('2d'); }
This function stores the canvas element and context in global variables and registers a handler for the click event. Like most other HTML elements, canvas elements support a number of different events; for example, you can find games that register for keydown and keyup events to allow the user to play the game from the keyboard.
Each time the mouse is clicked on this canvas, it records the click in an array. If you've clicked four times, it enters the drawing code and draws a Bézier curve, using those four points as control points. Here's the relevant part of the drawing code:
context.beginPath(); context.strokeStyle = "black"; context.moveTo(points[0].x, points[0].y); context.bezierCurveTo(points[1].x, points[1].y, points[2].x, points[2].y, points[3].x, points[3].y); context.stroke();
We've already seen the first line, which begins a new path by deleting all of the existing subpaths from the current path. The second line is more interesting: It manipulates the drawing state, setting the style for the stroke. This line reuses CSS syntax for specifying colors, and allows you to specify a gradient or pattern object.
The next two lines append two drawing commands to the current path. The first command sets the initial point for drawing, and the second adds a Bézier curve. The final line draws the current path, which has the effect of drawing the curve.
This example draws three componentsthe Bézier itself, along with red and green lines going from the end points to the control points. Because these are different colors, they can't be part of the same path. The current path can only be drawn with the current graphics state, and the stroke color is an attribute of the graphics state, not of a path segment, so a path can be drawn with only one color (although that color can be a gradient or pattern).
With this example, I've shown you the very basic features of the <canvas> tag, but a lot more is available. You can do pretty much anything with the <canvas> tag that you can with PDF, including reusing components. If you're creating a function that does some reusable drawing, remember to save() the graphics state at the start and restore() it later. Then you can draw this component in different places and at different sizes, simply by modifying the transform matrix before calling the drawing function.
The canvas element also supports a rich set of operations for compositing raster images, which we haven't addressed in this article. Again, if you've used Cairo, Core Graphics, or AppKit, or you've done any PostScript or PDF generation in your code, these operations will appear very familiar to you.