Skip to content
Open
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
19 changes: 9 additions & 10 deletions tiny-whiteboard/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,12 @@ export const splitTextLines = text => {
let textCheckEl = null
export const getTextActWidth = (text, style) => {
if (!textCheckEl) {
textCheckEl = document.createElement('div')
textCheckEl.style.position = 'fixed'
textCheckEl.style.left = '-99999px'
document.body.appendChild(textCheckEl)
textCheckEl = createCanvas(300,200)
textCheckEl.ctx.textBaseline = "middle"
}
let { fontSize, fontFamily } = style
textCheckEl.innerText = text
textCheckEl.style.fontSize = fontSize + 'px'
textCheckEl.style.fontFamily = fontFamily
let { width } = textCheckEl.getBoundingClientRect()
return width
textCheckEl.ctx.font = `${style.fontSize}px ${style.fontFamily}`
textCheckEl.ctx.fillText(text, 0, 0)
return textCheckEl.ctx.measureText(text).width
}

// 计算固定宽度内能放下所有文字的最大字号
Expand All @@ -278,6 +273,10 @@ export const getWrapTextActWidth = element => {
if (width > maxWidth) {
maxWidth = width
}
if (textCheckEl) {
const {canvas, ctx} = textCheckEl
ctx.clearRect(-canvas.width / 2, -canvas.width / 2, canvas.width * 2, canvas.width * 2)
}
})
return maxWidth
}
Expand Down