Canvas Text
HTML Canvas Text
Section titled “HTML Canvas Text”Drawing Text on the Canvas
Section titled “Drawing Text on the Canvas”The HTML Canvas API provides methods to render text directly onto the drawing surface. Key properties and methods for text drawing include:
font: A string property that defines the font settings, similar to the CSSfontproperty (e.g., ‘20px Arial’, ‘bold 16px sans-serif’). Default is ‘10px sans-serif’.fillStyle: Sets the color, gradient, or pattern used to fill the text. Applies tofillText().strokeStyle: Sets the color, gradient, or pattern used for the text’s stroke (outline). Applies tostrokeText().textAlign: Specifies the horizontal alignment of the text relative to the x-coordinate provided infillText()orstrokeText(). Possible values: ‘start’, ‘end’, ‘left’, ‘right’, ‘center’. Default is ‘start’.textBaseline: Specifies the vertical alignment of the text relative to the y-coordinate. Possible values: ‘top’, ‘hanging’, ‘middle’, ‘alphabetic’, ‘ideographic’, ‘bottom’. Default is ‘alphabetic’.fillText(text, x, y, [maxWidth]): Draws solid (filled)textat coordinates (x,y). Optionally,maxWidthcan be provided to constrain the text width, causing it to be compressed or a smaller font used if necessary.strokeText(text, x, y, [maxWidth]): Draws the outline of thetextat (x,y). Also supportsmaxWidth.measureText(text): Returns aTextMetricsobject containing information about the rendered width of the specifiedtext(e.g.,metrics.width). This is useful for positioning or layout calculations before drawing.
Using fillText()
Section titled “Using fillText()”This example draws the text “Hello Canvas!” filled with the default color (black).
// HTML: <canvas id="myTextCanvas1" width="300" height="100"></canvas>const canvasFill = document.getElementById('myTextCanvas1');const ctxFill = canvasFill.getContext('2d');
ctxFill.font = '30px Arial'; // Set font size and familyctxFill.fillText('Hello Canvas!', 10, 50); // text, x, yUsing strokeText()
Section titled “Using strokeText()”This example draws the outline of the text “Hello Canvas!”.
// HTML: <canvas id="myTextCanvas2" width="300" height="100"></canvas>const canvasStroke = document.getElementById('myTextCanvas2');const ctxStroke = canvasStroke.getContext('2d');
ctxStroke.font = '30px Verdana';ctxStroke.strokeStyle = 'blue'; // Set stroke colorctxStroke.strokeText('Hello Canvas!', 10, 50);Adding Color, Alignment, and Centering Text
Section titled “Adding Color, Alignment, and Centering Text”This example demonstrates how to set text color, change the font, and center the text horizontally and vertically within the canvas.
// HTML: <canvas id="myTextCanvas3" width="400" height="150" style="border:1px solid #ccc;"></canvas>const canvasCentered = document.getElementById('myTextCanvas3');const ctxCentered = canvasCentered.getContext('2d');
const text = 'Centered Text!';
// StylingctxCentered.font = 'italic bold 24px Comic Sans MS, cursive';ctxCentered.fillStyle = 'green';
// AlignmentctxCentered.textAlign = 'center'; // Horizontal centerctxCentered.textBaseline = 'middle'; // Vertical center
// Calculate center positionconst x = canvasCentered.width / 2;const y = canvasCentered.height / 2;
// Draw the textctxCentered.fillText(text, x, y);
// Example of using measureText()const metrics = ctxCentered.measureText(text);console.log('Text width:', metrics.width);// You could use metrics.width to, for example, draw a box around the text.Application Scenario: Displaying scores in a game, labeling charts and graphs, watermarking images, or creating custom text-based user interface elements.