feat:优化渲染速度

pr675
黄云飞 4 years ago
parent 37c44f7986
commit 742359edf3

@ -175,8 +175,8 @@ export class Draw {
rowFlex: this.elementList?.[1]?.rowFlex rowFlex: this.elementList?.[1]?.rowFlex
}) })
} }
this.ctx.save()
for (let i = 0; i < this.elementList.length; i++) { for (let i = 0; i < this.elementList.length; i++) {
this.ctx.save()
const curRow: IRow = rowList[rowList.length - 1] const curRow: IRow = rowList[rowList.length - 1]
const element = this.elementList[i] const element = this.elementList[i]
const rowMargin = defaultBasicRowMarginHeight * (element.rowMargin || defaultRowMargin) const rowMargin = defaultBasicRowMarginHeight * (element.rowMargin || defaultRowMargin)
@ -236,8 +236,8 @@ export class Draw {
} }
curRow.elementList.push(rowElement) curRow.elementList.push(rowElement)
} }
this.ctx.restore()
} }
this.ctx.restore()
this.rowList = rowList this.rowList = rowList
} }
@ -248,6 +248,18 @@ export class Draw {
isSetCursor = true, isSetCursor = true,
isComputeRowList = true isComputeRowList = true
} = payload || {} } = payload || {}
// 计算行信息
const { margins } = this.options
if (isComputeRowList) {
this._computeRowList()
// 计算高度是否超出
const rowHeight = this.rowList.reduce((pre, cur) => cur.height + pre, 0)
if (rowHeight > this.canvas.height - margins[0] - margins[2]) {
const height = Math.ceil(rowHeight + margins[0] + margins[2])
this.canvas.height = height
this.canvas.style.height = `${height}px`
}
}
// 清除光标等副作用 // 清除光标等副作用
this.cursor.recoveryCursor() this.cursor.recoveryCursor()
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
@ -258,13 +270,8 @@ export class Draw {
// 绘制背景 // 绘制背景
this.background.render(canvasRect) this.background.render(canvasRect)
// 绘制页边距 // 绘制页边距
const { margins } = this.options
const leftTopPoint: [number, number] = [margins[3], margins[0]] const leftTopPoint: [number, number] = [margins[3], margins[0]]
this.margin.render(canvasRect) this.margin.render(canvasRect)
// 计算行信息
if (isComputeRowList) {
this._computeRowList()
}
// 渲染元素 // 渲染元素
let x = leftTopPoint[0] let x = leftTopPoint[0]
let y = leftTopPoint[1] let y = leftTopPoint[1]
@ -333,7 +340,6 @@ export class Draw {
x = leftTopPoint[0] x = leftTopPoint[0]
y += curRow.height y += curRow.height
} }
// 搜索匹配绘制 // 搜索匹配绘制
if (this.searchMatchList) { if (this.searchMatchList) {
this.search.render() this.search.render()
@ -346,19 +352,6 @@ export class Draw {
this.position.setCursorPosition(positionList[curIndex!] || null) this.position.setCursorPosition(positionList[curIndex!] || null)
this.cursor.drawCursor() this.cursor.drawCursor()
} }
// canvas高度自适应计算
const lastPosition = positionList[positionList.length - 1]
const { coordinate: { leftBottom, leftTop } } = lastPosition
if (leftBottom[1] > this.canvas.height - margins[2]) {
const height = Math.ceil(leftBottom[1] + (leftBottom[1] - leftTop[1]) + margins[2])
this.canvas.height = height
this.canvas.style.height = `${height}px`
this.render({
curIndex,
isSubmitHistory: false,
isComputeRowList: false
})
}
// 历史记录用于undo、redo // 历史记录用于undo、redo
if (isSubmitHistory) { if (isSubmitHistory) {
const self = this const self = this

Loading…
Cancel
Save