feat:优化计算性能

pr675
黄云飞 4 years ago
parent 6cc6ee72d4
commit fee2c7933e

@ -20,7 +20,7 @@
} }
.cursor { .cursor {
width: 2px; width: 1px;
height: 20px; height: 20px;
left: 0; left: 0;
right: 0; right: 0;

@ -48,6 +48,7 @@ export class Draw {
private textParticle: TextParticle private textParticle: TextParticle
private pageNumber: PageNumber private pageNumber: PageNumber
private innerWidth: number
private rowList: IRow[] private rowList: IRow[]
private painterStyle: IElementStyle | null private painterStyle: IElementStyle | null
private searchMatchList: number[][] | null private searchMatchList: number[][] | null
@ -88,6 +89,8 @@ export class Draw {
const globalEvent = new GlobalEvent(this, canvasEvent) const globalEvent = new GlobalEvent(this, canvasEvent)
globalEvent.register() globalEvent.register()
const { width, margins } = options
this.innerWidth = width - margins[1] - margins[3]
this.rowList = [] this.rowList = []
this.painterStyle = null this.painterStyle = null
this.searchMatchList = null this.searchMatchList = null
@ -225,13 +228,10 @@ export class Draw {
} }
private _computeRowList() { private _computeRowList() {
const { defaultSize, width } = this.options const { defaultSize, defaultRowMargin, defaultBasicRowMarginHeight } = this.options
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
const { margins, defaultRowMargin, defaultBasicRowMarginHeight } = this.options const innerWidth = this.innerWidth
const leftTopPoint: [number, number] = [margins[3], margins[0]]
const rightTopPoint: [number, number] = [width - margins[1], margins[0]]
const innerWidth = rightTopPoint[0] - leftTopPoint[0]
const rowList: IRow[] = [] const rowList: IRow[] = []
if (this.elementList.length) { if (this.elementList.length) {
rowList.push({ rowList.push({
@ -267,7 +267,7 @@ export class Draw {
} else { } else {
metrics.height = element.size || this.options.defaultSize metrics.height = element.size || this.options.defaultSize
ctx.font = this._getFont(element) ctx.font = this._getFont(element)
const fontMetrics = ctx.measureText(element.value) const fontMetrics = this.textParticle.measureText(ctx, element)
metrics.width = fontMetrics.width metrics.width = fontMetrics.width
metrics.boundingBoxAscent = element.value === ZERO ? defaultSize : fontMetrics.actualBoundingBoxAscent metrics.boundingBoxAscent = element.value === ZERO ? defaultSize : fontMetrics.actualBoundingBoxAscent
metrics.boundingBoxDescent = fontMetrics.actualBoundingBoxDescent metrics.boundingBoxDescent = fontMetrics.actualBoundingBoxDescent
@ -306,10 +306,9 @@ export class Draw {
} }
private _drawElement(positionList: IElementPosition[], rowList: IRow[], pageNo: number) { private _drawElement(positionList: IElementPosition[], rowList: IRow[], pageNo: number) {
const { margins } = this.options const { margins, width, height } = this.options
const canvas = this.pageList[pageNo]
const ctx = this.ctxList[pageNo] const ctx = this.ctxList[pageNo]
ctx.clearRect(0, 0, canvas.width, canvas.height) ctx.clearRect(0, 0, width, height)
// 绘制背景 // 绘制背景
this.background.render(ctx) this.background.render(ctx)
// 绘制页边距 // 绘制页边距
@ -323,7 +322,7 @@ export class Draw {
const curRow = rowList[i] const curRow = rowList[i]
// 计算行偏移量(行居左、居中、居右) // 计算行偏移量(行居左、居中、居右)
if (curRow.rowFlex && curRow.rowFlex !== RowFlex.LEFT) { if (curRow.rowFlex && curRow.rowFlex !== RowFlex.LEFT) {
const canvasInnerWidth = canvas.width - margins[1] - margins[3] const canvasInnerWidth = width - margins[1] - margins[3]
if (curRow.rowFlex === RowFlex.CENTER) { if (curRow.rowFlex === RowFlex.CENTER) {
x += (canvasInnerWidth - curRow.width) / 2 x += (canvasInnerWidth - curRow.width) / 2
} else { } else {
@ -375,11 +374,11 @@ export class Draw {
// 选区绘制 // 选区绘制
const { startIndex, endIndex } = this.range.getRange() const { startIndex, endIndex } = this.range.getRange()
if (startIndex !== endIndex && startIndex < index && index <= endIndex) { if (startIndex !== endIndex && startIndex < index && index <= endIndex) {
let width = metrics.width let rangeWidth = metrics.width
if (width === 0 && curRow.elementList.length === 1) { if (rangeWidth === 0 && curRow.elementList.length === 1) {
width = this.options.rangeMinWidth rangeWidth = this.options.rangeMinWidth
} }
this.range.render(ctx, x, y, width, curRow.height) this.range.render(ctx, x, y, rangeWidth, curRow.height)
} }
index++ index++
x += metrics.width x += metrics.width

@ -1,3 +1,4 @@
import { IElement } from "../../.."
import { IRowElement } from "../../../interface/Row" import { IRowElement } from "../../../interface/Row"
import { Draw } from "../Draw" import { Draw } from "../Draw"
@ -9,6 +10,7 @@ export class TextParticle {
private text: string private text: string
private curStyle: string private curStyle: string
private curColor?: string private curColor?: string
public cacheMeasureText: Map<string, TextMetrics>
constructor(draw: Draw) { constructor(draw: Draw) {
this.ctx = draw.getCtx() this.ctx = draw.getCtx()
@ -16,6 +18,18 @@ export class TextParticle {
this.curY = -1 this.curY = -1
this.text = '' this.text = ''
this.curStyle = '' this.curStyle = ''
this.cacheMeasureText = new Map()
}
public measureText(ctx: CanvasRenderingContext2D, element: IElement): TextMetrics {
const id = `${element.value}${ctx.font}`
const cacheTextMetrics = this.cacheMeasureText.get(id)
if (cacheTextMetrics) {
return cacheTextMetrics
}
const textMetrics = ctx.measureText(element.value)
this.cacheMeasureText.set(id, textMetrics)
return textMetrics
} }
public complete() { public complete() {

Loading…
Cancel
Save