Skip to content

Commit

Permalink
Adds text shadow option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Howard-R20 committed Nov 21, 2023
1 parent c964031 commit bc78d45
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ const { drawText, getTextHeight, splitText } = window.canvasTxt
| `justify` | `false` | Justify text if `true`, it will insert spaces between words when necessary. |
| `strokeWidth` | `1` | Stroke line width. |
| `strokeColor` | `black` | Stroke color. |
| `shadowBlur` | `0` | Amount of shadow blur. |
| `shadowColor` | `black` | Color of shadow blur. |

## Methods

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": "@roll20/canvas-txt",
"version": "4.2.3",
"version": "4.2.4",
"description": "Render multiline textboxes in HTML5 canvas with auto line breaks and better alignment system",
"files": [
"dist"
Expand Down
11 changes: 10 additions & 1 deletion src/canvas-txt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface CanvasTextConfig {
justify?: boolean
strokeWidth?: number,
strokeColor?: string,
shadowColor?: string,
shadowBlur?: number,
}

const defaultConfig = {
Expand All @@ -32,6 +34,8 @@ const defaultConfig = {
lineHeight: null,
justify: false,
strokeWidth: 0,
shadowColor: '#000000',
shadowBlur: 0,
}

function drawText(
Expand All @@ -51,7 +55,7 @@ function drawText(
const xEnd = x + width
const yEnd = y + height

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

Expand Down Expand Up @@ -103,6 +107,11 @@ function drawText(
ctx.lineWidth = strokeWidth;
}

if (shadowBlur && shadowColor) {
ctx.shadowBlur = shadowBlur;
ctx.shadowColor = shadowColor;
}

//print all lines of text
textArray.forEach((txtline) => {
ctx.fillText(txtline, textAnchor, txtY)
Expand Down

0 comments on commit bc78d45

Please sign in to comment.