| Start |
|---|
| Overview of the basic path drawing commands: lines and curves ================== TODO: Link each of the drawing commands below to their individual examples. I don't know how to do this since the links to the individual examples point towards the "draft" folder. TODO: Add examples for these path "action" commands: stroke(), fill(), clip() ================== Path A path defines a set of lines and curves which can be visibly drawn on the Canvas. A path is not automatically drawn on the Canvas. But the path's lines & curves can be drawn onto the Canvas using a styleable stroke. And the shape created by the lines and curves can also be filled with a styleable fill. Paths have uses beyond drawing on the Canvas:
The basic path drawing commands are:
Description of the basic drawing commands:beginPath Begins assembling a new set of path commands and also discards any previously assembled path. The discarding is an important and often overlooked point. If you don't begin a new path, any previously issued path commands will automatically be redrawn. It also moves the drawing "pen" to the top-left origin of the canvas (==coordinate[0,0]). moveTo Moves the current pen location to the coordinate [startX,startY]. By default all path drawings are connected together. So the ending point of one line or curve is the starting point of the next line or curve. This can cause an unexpected line to be drawn connecting two adjacent drawings. The lineTo Draws a line segment from the current pen location to coordinate [endX,endY] You can assemble multiple arc Draws a circular arc given a centerpoint, radius and starting & ending angles. The angles are expressed as radians. To convert degrees to radians you can use this formula: Angle 0 faces directly rightward from the center of the arc. To draw a complete circle you can make endingAngle = startingAngle + 360 degrees (360 degrees == Math.PI2): `context.arc(10,10,20,0,Math.PI2); By default, the arc is drawn clockwise, An optional [true|false] parameter instructs the arc to be drawn counter-clockwise: quadraticCurveTo Draws a quadratic curve starting at the current pen location to a given ending coordinate. Another given control coordinate determines the shape (curviness) of the curve. bezierCurveTo Draws a cubic Bezier curve starting at the current pen location to a given ending coordinate. Another 2 given control coordinates determine the shape (curviness) of the curve. arcTo Draws a circular arc with a given radius. The arc is drawn clockwise inside the wedge formed by the current pen location and given two points: Point1 & Point2. A line connecting the current pen location and the start of the arc is automatically drawn preceding the arc. rect Draws a rectangle given a top-left corner and a width & height. The closePath Draws a line from the current pen location back to the beginning path coordinate. For example, if you draw 2 lines forming 2 legs of a triangle, This command's name often causes it to be misunderstood. |
lineTo (a path command) Draws a line segment from the current pen location to coordinate [endX,endY] You can assemble multiple .lineTo commands to draw a polyline. For example, you could assemble 3 line segments to form a triangle. |
arc (a path command) Draws a circular arc given a centerpoint, radius and starting & ending angles. The angles are expressed as radians. To convert degrees to radians you can use this formula: Angle 0 faces directly rightward from the center of the arc. By default, the arc is drawn clockwise, An optional [true|false] parameter instructs the arc to be drawn counter-clockwise: To draw a complete circle you can make endingAngle = startingAngle + 360 degrees (360 degrees == Math.PI2). |
quadraticCurveTo (a path command) Draws a quadratic curve starting at the current pen location to a given ending coordinate. Another given control coordinate determines the shape (curviness) of the curve. |
bezierCurveTo (a path command) Draws a cubic Bezier curve starting at the current pen location to a given ending coordinate. Another 2 given control coordinates determine the shape (curviness) of the curve. |
arcTo (a path command) Draws a circular arc with a given radius. The arc is drawn clockwise inside the wedge formed by the current pen location and given two points: Point1 & Point2. A line connecting the current pen location and the start of the arc is automatically drawn preceding the arc. |
rect (a path command) Draws a rectangle given a top-left corner and a width & height. The These disconnected rectangles are not automatically connected by lines. |
closePath (a path command) Draws a line from the current pen location back to the beginning path coordinate. For example, if you draw 2 lines forming 2 legs of a triangle, closePath will "close" the triangle by drawing the third leg of the triangle from the 2nd leg's endpoint back to the first leg's starting point. A Misconception explained! This command's name often causes it to be misunderstood.
Again, the closePath command draws a line -- it does not "close" a beginPath. This example draws 2 legs of a triangle and uses |
beginPath (a path command) Begins assembling a new set of path commands and also discards any previously assembled path. It also moves the drawing "pen" to the top-left origin of the canvas (==coordinate[0,0]). Although optional, you should ALWAYS start a path with The discarding is an important and often overlooked point. If you don't begin a new path with These 2 demos both attempt to draw an "X" with one red stroke and one blue stroke. This first demo correctly uses This second demo incorrectly leaves out The second But without a second Since the second |
lineCap (a path styling attribute) Sets the cap style of line starting points and ending points. |
lineJoin (a path styling attribute) Sets the style used to connect adjoining line segments.
|
strokeStyle (a path styling attribute) Sets the color that will be used to stroke the outline of the current path. These are You can also specify these color options (these options are objects created by the context): |
fillStyle (a path styling attribute) Sets the color that will be used to fill the interior of the current path. These are color options (these must be quoted): You can also specify these color options (these options are objects created by the context):
|
lineWidth (A path styling attribute) Sets the width of the line that will stroke the outline of the path |
shadowColor, shadowBlur, shadowOffsetX, shadowOffsetY (path styling attributes) This set of attributes will add a shadow around a path. Both filled paths and stroked paths may have a shadow. The shadow is darkest (opaque) at the path perimeter and becomes gradiently lighter as it extends away from the path perimeter.
About shadowOffsetX & shadowOffsetY It's important to note that the whole shadow is shifted in its entirety. This will cause part of the shadow to shift underneath filled paths and therefore part of the shadow will not be visible. About shadowed strokes When shadowing a stroke, both the inside and the outside of the stroke are shadowed. The shadow is darkest at the stroke and lightens as the shadow extends outward in both directions from the stroke. Turning off shadowing when done After you have drawn your shadows, you might want to turn shadowing off to draw more paths. To turn shadowing off you set the Performance considerations Shadows (like gradients) requires extensive computations and therefore you should use shadows sparingly. Be especially cautious when animating because drawing shadows many times per second will greatly impact performance. A workaround if you need to animate shadowed paths is to pre-create the shadowed path on a second "shadow-canvas". The shadow-canvas is a normal canvas that is created in memory with |
createLinearGradient (creates a path styling object) Creates a reusable linear gradient (object). The object can be assigned to any Then stroke() or fill() will color the Path with the gradient colors of the object. Creating a gradient object is a 2-step process:
Arguments: CssColor is a CSS color assigned to this particular color stop. The gradient object is an object that you can use (and reuse!) to make your path strokes and fills become gradient colored. Side Note: The gradient object is not internal to the Canvas element nor it's Context. It is a separate and reusable JavaScript object that you can assign to any Path you desire. You can even use this object to color a Path on a different Canvas element(!) Color stops are (percentage) waypoints along the gradient line. At each color stop waypoint, the gradient is fully (==opaquely) colored with it's assigned color. Interim points along the gradient line between color stops are colored as gradients of the this and the previous color. Important hint about Canvas gradients! When you create a gradient object, the entire canvas is "invisibly" filled with that gradient. When you |
createRadialGradient (creates a path styling object) Creates a reusable radial gradient (object). The gradient object is an object that you can use (and reuse!) to make your path strokes and fills become gradient colored. About... The Canvas radial gradient is extremely different from traditional radial gradients. The "official" (almost undecipherable!) definition of Canvas's radial gradient is at the bottom of this posting. Don't look at it if you have a weak disposition!! In (almost understandable) terms:
Creating a gradient object is a 2-step process:
Arguments:
CssColor is a CSS color assigned to this particular color stop. Side Note: The gradient object is not internal to the Canvas element nor it's Context. It is a separate and reusable JavaScript object that you can assign to any Path you desire. You can even use this object to color a Path on a different Canvas element(!) Color stops are (percentage) waypoints along the gradient line. At each color stop waypoint, the gradient is fully (==opaquely) colored with it's assigned color. Interim points along the gradient line between color stops are colored as gradients of the this and the previous color. Important hint about Canvas gradients! When you create a gradient object, the entire radial gradient is "invisibly" cast upon the canvas. When you
The scary official detailsWho decides what `createRadialGradient does? The W3C (opens new window) issues the official recommended specifications that browsers use to build the Html5 Canvas element. The W3C specification for What does `createRadialGradient` … effectively creates a cone, touched by the two circles defined in the creation of the gradient, with the part of the cone before the start circle (0.0) using the color of the first offset, the part of the cone after the end circle (1.0) using the color of the last offset, and areas outside the cone untouched by the gradient (transparent black). How does it work internally The `createRadialGradient(x0, y0, r0, x1, y1, r1)` method takes six arguments, the first three representing the start circle with origin (x0, y0) and radius r0, and the last three representing the end circle with origin (x1, y1) and radius r1. The values are in coordinate space units. If either of r0 or r1 are negative, an IndexSizeError exception must be thrown. Otherwise, the method must return a radial CanvasGradient initialized with the two specified circles. Radial gradients must be rendered by following these steps:
|
createPattern (creates a path styling object) Creates a reusable pattern (object). The object can be assigned to any Then stroke() or fill() will paint the Path with the pattern of the object. Arguments:
repeat determines how the imageObject will be repeated across the canvas (much like a CSS background). This argument must be quote delimited and valid values are: The pattern object is an object that you can use (and reuse!) to make your path strokes and fills become patterned. Side Note: The pattern object is not internal to the Canvas element nor it's Context. It is a separate and reusable JavaScript object that you can assign to any Path you desire. You can even use this object to apply pattern to a Path on a different Canvas element(!) Important hint about Canvas patterns! When you create a pattern object, the entire canvas is "invisibly" filled with that pattern (subject to the When you |
stroke (a path command) Causes the perimeter of the Path to be stroked according to the current Prior to executing The unusual way strokes are drawnConsider this example Path that draws a 1 pixel black line from Question: What does the browser actually draw on the canvas? You probably expect to get 6 black pixels on y=5 But(!) ... Canvas always draws strokes half-way to either side of the it's defined path! So since the line is defined at But, again(!) ... The computer display cannot draw half-pixels! So what is to be done with the undesired half-pixels (shown in blue below)? The answer is that Canvas actually orders the display to draw a 2 pixel wide line from An adjusting trick that ONLY works for exactly horizontal and vertical strokes You can get a 1 pixel solid black line by specifying the line be drawn on the half-pixel: Example code using |
fill (a path command) Causes the inside of the Path to be filled according to the current Prior to executing Example code using |
clip (a path command) Limits any future drawings to display only inside the current Path. Example: Clip this image into a triangular Path Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference