From 0994df11871d126d3f550383f7f8a4deaf7df4a2 Mon Sep 17 00:00:00 2001 From: Ian Howard Date: Fri, 13 Oct 2023 12:08:25 -0700 Subject: [PATCH] Remove need to set strokeStyle in context externally. --- README.md | 2 -- src/canvas-txt/index.ts | 9 +++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 56e9555..079c9bd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/canvas-txt/index.ts b/src/canvas-txt/index.ts index f980fde..81cc5c5 100644 --- a/src/canvas-txt/index.ts +++ b/src/canvas-txt/index.ts @@ -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 @@ -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