Drawing on the Canvas
- Code to make the above drawing
- A Graphics API (Application Programming Interface) generally has a set of primitives and these primitives have attributes.
- Primitives
- The things the system can draw
- lines
- rectangles
- text
- images
- quadratic curves
- Attributes
- Values that change the way these primitives are drawn.
- For the HTML 5 canvas in the 2d context
- Primitives
- Rectangles
-
ctx.strokeRect(x,y,w,h); -
ctx.fillRect(x,y,w,h); -
ctx.clearRect(0,0,width,height)will clear the screen -
strokeStyleandfillStyleapply here - Other attributes apply as well
ctx.lineWidth -
const rectCanvas = document.getElementById("rects"); const rectCTX = rectCanvas.getContext('2d'); rectCTX.strokeStyle = "green" rectCTX.strokeRect(10,10,20,20) rectCTX.strokeStyle="red" rectCTX.fillStyle="blue" rectCTX.fillRect(40,40,10,30) rectCTX.lineWidth = 3 rectCTX.strokeStyle="black" rectCTX.fillStyle="cyan" rectCTX.fillRect(40,80,40,10) rectCTX.strokeRect(40,80,40,10) rectCTX.lineWidth = 4 rectCTX.strokeStyle="blue" rectCTX.fillStyle="red" rectCTX.strokeRect(50,20,40,10); rectCTX.fillRect(50,20,40,10); - The last one, (blue border red fill) looks like the border is not as wide as the previous, that is because?
-
- lines
- Lines are actually part of a path.
- Start with
ctx.beginPath()- This clears things out, so use this to start a "new" set of lines.
- Later for a polygon, we will use many lines.
- End with
ctx.stroke() - Move with drawing
ctx.moveTo() - Draw with drawing
ctx.lineTo() - They have many attributes, for now we want
-
ctx.strokeStyle -
ctx.lineWidth
-
-
<canvas id="lines" width="100" height="100" style="border:1px solid #000000;"></canvas> <script> const lineCanvas = document.getElementById("lines") const lineCTX = lineCanvas.getContext('2d') lineCTX.lineWidth = 10 lineCTX.beginPath() lineCTX.moveTo(0,0) lineCTX.lineTo(100,100) lineCTX.stroke() lineCTX.beginPath() lineCTX.strokeStyle="red" lineCTX.moveTo(100,0) lineCTX.lineTo(0,100) lineCTX.stroke() </script> - Notice the last element drawn is "on top".
- Other attributes include
- lineCap
- How do I draw the end of a line.
- "butt", Even with the end of the line
- "round", half circle at the end of the line
- "square" goes over the end of the line
- Line dashes are another attribute of lines.
- ctx.setLineDash()
- Takes a "pattern" array for dashes.
- This is expected to contain an even number of elements.
- If this is not the case, the pattern is doubled.
- If The array is empty, the line is solid.
- lineDashOffset controls where in the line you start.
- This is the number of pixels into the pattern.
- lineCap
- Drawing a filled shape
- Draw a path.
- End with
ctx.fill() - This uses the
cxt.fillStyleproperty.
- Text
-
ctx.strokeText(text, x, y); -
ctx.fillText(text, x, y); - Ends with
ctx.stroke()orctx.fill() - Uses fill or stroke attributes.
-
ctx.fillStyle, ctx.strokeStyle
-
- also
ctx.font = "font" -
ctx.measureText(string)allows us to find the dimensions of the text. - reference
- It reutrns a textMetric
- actualBoundingBoxAscent returns a distance from the baseline to the highest letter.
-
-
<canvas id="text" width="300" height="200" style="border:1px solid #000000;"></canvas> <script> const textCanvas = document.getElementById("text"); const textCTX = textCanvas.getContext('2d'); const textW = textCanvas.width; const textH = textCanvas.height; let height = 0; textCTX.strokeStyle = "green"; textCTX.beginPath(); for(let fs=10; fs <= 60; fs+=10) { let font = fs+"px sans"; textCTX.font= font; let atribs = textCTX.measureText("Hello"); height += 1.2*atribs.actualBoundingBoxAscent; textCTX.beginPath(); textCTX.strokeText("Hello", (textW-atribs.width)/2, height); textCTX.stroke(); } </script>
-
- Circle
-
ctx.arc(x,y, radius, start, stop, direction); - Angles are in radians
- 0 to 2*Math.PI
- Last arugment true -> counterclockwise
- needs a ctx.beginPath();
- ends with ctx.fill() or ctx.stroke().
-
<canvas id="circle" width="600" height="200" style="border:1px solid #000000;"></canvas> <script> const circleCanvas = document.getElementById("circle"); const circleCTX = circleCanvas.getContext('2d'); const circleW = circleCanvas.width; const circleH = circleCanvas.height; // 24 is arbitrary, it looked good. // needed to be > to 18 let r = circleW/24; let d = circleW/9; circleCTX.fillStyle = "blue"; circleCTX.font = "15pt sans"; for(let i=1; i <= 8; i++) { circleCTX.strokeStyle = "red"; circleCTX.beginPath(); // remember, we are upside down, so the second angle is a - circleCTX.arc(d*i, .5*d, r, 0, -Math.PI/4 *i, true); circleCTX.stroke(); circleCTX.fillStyle = "blue"; circleCTX.beginPath(); circleCTX.arc(d*i, 1.5*d, r, 0, -Math.PI/4 * i); circleCTX.fill(); circleCTX.fillStyle = "black"; circleCTX.beginPath(); // u00B0 is unicode for the degree symbol. circleCTX.fillText(" "+ i*45 + "\u00b0", d*(i-.25), 2.5*d); circleCTX.fill(); } </script>
-