<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>
ctx.fill()
cxt.fillStyle
property.
ctx.strokeText(text, x, y);
ctx.fillText(text, x, y);
ctx.stroke()
or ctx.fill()
<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>
<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>