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
14 changes: 14 additions & 0 deletions src/canvas-txt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CanvasTextConfig {
font?: string
lineHeight?: number
justify?: boolean
overflow?: boolean
}

const defaultConfig = {
Expand All @@ -29,6 +30,7 @@ const defaultConfig = {
font: 'Arial',
lineHeight: null,
justify: false,
overflow: true,
}

function drawText(
Expand Down Expand Up @@ -95,13 +97,25 @@ function drawText(
debugY = y + height / 2
txtY -= negOffset
}

ctx.save()

// clip the text to the width and height
if (!config.overflow) {
ctx.beginPath()
ctx.rect(x, y, width, height)
ctx.clip()
}

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

ctx.restore()

if (config.debug) {
const debugColor = '#0C8CE9'

Expand Down
9 changes: 7 additions & 2 deletions src/docs/AppCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const initialConfig = {
align: 'center',
vAlign: 'middle',
justify: false,
overflow: true,
min: 0,
max: 800,
}
Expand Down Expand Up @@ -68,6 +69,7 @@ function renderText() {
align: config.align as CanvasTextConfig['align'],
vAlign: config.vAlign as CanvasTextConfig['vAlign'],
justify: config.justify,
overflow: config.overflow,
}

const { height } = drawText(ctx, config.text, myConfig)
Expand Down Expand Up @@ -176,10 +178,13 @@ onMounted(() => {
<br />

<el-row>
<el-col :span="12">
<el-col :span="8">
<el-checkbox v-model="config.justify" label="Justify Text" />
</el-col>
<el-col :span="12">
<el-col :span="8">
<el-checkbox v-model="config.overflow" label="Text overflow" />
</el-col>
<el-col :span="8">
<el-checkbox v-model="config.debug" label="Debug mode" />
</el-col>
</el-row>
Expand Down