Skip to content

Commit

Permalink
fix: 解决插入的文本显示模糊问题 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Laomai-codefee committed Jul 25, 2024
1 parent ea8e6a5 commit 4edd0be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/const/definitions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const DefaultColors = ['#FF0000', '#FFBE00', '#CC0000', '#FFFF00', '#83D3

// 配置默认字体大小
// 提供一组默认的字体大小选项
export const DefaultFontSize = [14, 16, 18, 20, 22, 24]
export const DefaultFontSize = [12, 14, 16, 18, 20, 22, 24]

// 配置默认的签名设置
export const DefaultSignatureSetting = {
Expand Down
27 changes: 14 additions & 13 deletions src/painter/editor/editor_free_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class EditorFreeText extends Editor {
*/
private createInputArea(e: KonvaEventObject<PointerEvent>) {
const pos = this.konvaStage.getRelativePointerPosition()
const { y: scaleY } = this.konvaStage.scale()
const { x, y } = this.konvaStage.scale()

// 创建和配置 textarea 元素
const inputArea = document.createElement('textarea')
Expand All @@ -53,7 +53,7 @@ export class EditorFreeText extends Editor {
x: e.evt.offsetX,
y: e.evt.offsetY
},
scaleY
y
)
this.konvaStage.container().append(inputArea)

Expand All @@ -64,7 +64,7 @@ export class EditorFreeText extends Editor {
}, 100)

// 注册事件监听器
this.addInputAreaEventListeners(inputArea, scaleY, pos)
this.addInputAreaEventListeners(inputArea, { x, y }, pos)
}

/**
Expand Down Expand Up @@ -93,7 +93,7 @@ export class EditorFreeText extends Editor {
* @param scaleY Y 轴缩放比例
* @param pos 相对位置坐标
*/
private addInputAreaEventListeners(inputArea: HTMLTextAreaElement, scaleY: number, pos: { x: number; y: number }) {
private addInputAreaEventListeners(inputArea: HTMLTextAreaElement, scale: { x: number; y: number }, pos: { x: number; y: number }) {
// 动态调整 textarea 的高度以适应输入内容
inputArea.addEventListener('input', e => this.adjustTextareaHeight(e))

Expand All @@ -103,12 +103,12 @@ export class EditorFreeText extends Editor {
if (target.getAttribute('del') === 'true') {
this.removeInputArea(target)
} else {
this.inputDoneHandler(target, scaleY, pos)
this.inputDoneHandler(target, scale, pos)
}
})

// 处理键盘事件
inputArea.addEventListener('keydown', e => this.handleInputAreaKeydown(e, scaleY))
inputArea.addEventListener('keydown', e => this.handleInputAreaKeydown(e, scale.y))
}

/**
Expand Down Expand Up @@ -156,7 +156,7 @@ export class EditorFreeText extends Editor {
* @param scaleY Y 轴缩放比例
* @param pos 相对位置坐标
*/
private async inputDoneHandler(inputArea: HTMLTextAreaElement, scaleY: number, pos: { x: number; y: number }) {
private async inputDoneHandler(inputArea: HTMLTextAreaElement, scale: { x: number; y: number }, pos: { x: number; y: number }) {
const value = inputArea.value.trim()
inputArea.remove()

Expand All @@ -167,10 +167,11 @@ export class EditorFreeText extends Editor {
}
const text = new Konva.Text({
x: pos.x,
y: pos.y,
y: pos.y + 2,
text: value,
fontSize: this.currentAnnotation.style.fontSize / scaleY,
fill: this.currentAnnotation.style.color
fontSize: this.currentAnnotation.style.fontSize,
fill: this.currentAnnotation.style.color,
padding: 2
})

this.currentShapeGroup.konvaGroup.add(text)
Expand All @@ -184,15 +185,15 @@ export class EditorFreeText extends Editor {

// 使用生成的 imageUrl 创建 Konva.Image
Konva.Image.fromURL(imageUrl, async image => {
const { width: width_rec, height: height_rec } = text.getClientRect()
// 删除之前的文本节点
this.getGroupNodesByClassName(this.currentShapeGroup.konvaGroup, 'Text')[0]?.destroy()
this.currentShapeGroup.konvaGroup.add(image)
const { width: width_rec, height: height_rec } = image.getClientRect()
image.setAttrs({
x: pos.x,
y: pos.y,
width: width_rec,
height: height_rec,
width: width_rec / scale.x,
height: height_rec / scale.y,
base64: imageUrl
})

Expand Down

0 comments on commit 4edd0be

Please sign in to comment.