Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const { drawText, getTextHeight, splitText } = window.canvasTxt
| `debug` | `false` | Shows the border and align gravity for debugging purposes |
| `align` | `center` | Text align. Other possible values: `left`, `right` |
| `vAlign` | `middle` | Text vertical align. Other possible values: `top`, `bottom` |
| `drawStyle` | `fill` | Text draw style. Other possible values: `outline` |
| `font` | `Arial` | Font family of the text |
| `fontSize` | `14` | Font size of the text in px |
| `fontStyle` | `''` | Font style, same as css font-style. Examples: `italic`, `oblique 40deg` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canvas-txt",
"version": "4.1.1",
"version": "4.2.0",
"description": "Render multiline textboxes in HTML5 canvas with auto line breaks and better alignment system",
"files": [
"dist"
Expand Down
12 changes: 10 additions & 2 deletions src/canvas-txt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CanvasTextConfig {
debug?: boolean
align?: 'left' | 'center' | 'right'
vAlign?: 'top' | 'middle' | 'bottom'
drawStyle?: 'fill' | 'outline'
fontSize?: number
fontWeight?: string
fontStyle?: string
Expand All @@ -22,6 +23,7 @@ const defaultConfig = {
debug: false,
align: 'center',
vAlign: 'middle',
drawStyle: 'fill',
fontSize: 14,
fontWeight: '',
fontStyle: '',
Expand All @@ -34,7 +36,7 @@ const defaultConfig = {
function drawText(
ctx: CanvasRenderingContext2D,
myText: string,
inputConfig: CanvasTextConfig
inputConfig: CanvasTextConfig,
) {
const { width, height, x, y } = inputConfig
const config = { ...defaultConfig, ...inputConfig }
Expand Down Expand Up @@ -95,10 +97,16 @@ function drawText(
debugY = y + height / 2
txtY -= negOffset
}

const drawFunction =
config.drawStyle === 'fill'
? ctx.fillText.bind(ctx)
: ctx.strokeText.bind(ctx)

//print all lines of text
textArray.forEach((txtline) => {
txtline = txtline.trim()
ctx.fillText(txtline, textAnchor, txtY)
drawFunction(txtline, textAnchor, txtY)
txtY += charHeight
})

Expand Down