Skip to content

Commit

Permalink
Remove need to set strokeStyle in context externally.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Howard-R20 committed Oct 13, 2023
1 parent 55b199c commit 0994df1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ const { drawText, getTextHeight, splitText } = window.canvasTxt
| `fontWeight` | `''` | Font weight, same as css font-weight. Examples: `bold`, `100` |
| `lineHeight` | `null` | Line height of the text, if set to null it tries to auto-detect the value |
| `justify` | `false` | Justify text if `true`, it will insert spaces between words when necessary. |
| `stroke` | `true` | Stroke text if `true`, it will add stroke outer the text. |
| `strokeWidth` | `1` | Stroke line width. |
| `strokeColor` | `black` | Stroke color. |

## Methods

Expand Down
9 changes: 7 additions & 2 deletions src/canvas-txt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function drawText(
const xEnd = x + width
const yEnd = y + height

const { fontStyle, fontVariant, fontWeight, fontSize, font, strokeWidth } = config
const { fontStyle, fontVariant, fontWeight, fontSize, font, strokeColor, strokeWidth } = config
const style = `${fontStyle} ${fontVariant} ${fontWeight} ${fontSize}px ${font}`
ctx.font = style

Expand Down Expand Up @@ -98,11 +98,16 @@ function drawText(
debugY = y + height / 2
txtY -= negOffset
}
if (strokeColor && strokeWidth) {
ctx.strokeStyle = strokeColor;
ctx.lineWidth = strokeWidth;
}

//print all lines of text
textArray.forEach((txtline) => {
txtline = txtline.trim()
ctx.fillText(txtline, textAnchor, txtY)
if (strokeWidth) {
if (strokeColor && strokeWidth) {
ctx.strokeText(txtline, textAnchor, txtY)
}
txtY += charHeight
Expand Down

0 comments on commit 0994df1

Please sign in to comment.