parent
7573eeece1
commit
a4abaca5a7
@ -0,0 +1,41 @@
|
||||
import { IElementFillRect } from '../../../interface/Element'
|
||||
|
||||
export abstract class AbstractRichText {
|
||||
public fillRect: IElementFillRect
|
||||
public fillColor?: string
|
||||
|
||||
constructor() {
|
||||
this.fillRect = this.clearFillInfo()
|
||||
}
|
||||
|
||||
public clearFillInfo() {
|
||||
this.fillColor = undefined
|
||||
return this.fillRect = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 0,
|
||||
height: 0
|
||||
}
|
||||
}
|
||||
|
||||
public recordFillInfo(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height?: number, color?: string) {
|
||||
const isFirstRecord = !this.fillRect.width
|
||||
if (!isFirstRecord && this.fillColor && this.fillColor !== color) {
|
||||
this.render(ctx)
|
||||
this.clearFillInfo()
|
||||
return
|
||||
}
|
||||
if (isFirstRecord) {
|
||||
this.fillRect.x = x
|
||||
this.fillRect.y = y
|
||||
}
|
||||
if (height && this.fillRect.height < height) {
|
||||
this.fillRect.height = height
|
||||
}
|
||||
this.fillRect.width += width
|
||||
this.fillColor = color
|
||||
}
|
||||
|
||||
public abstract render(ctx: CanvasRenderingContext2D): void
|
||||
|
||||
}
|
||||
@ -1,21 +1,26 @@
|
||||
import { AbstractRichText } from './AbstractRichText'
|
||||
import { IEditorOption } from '../../../interface/Editor'
|
||||
import { Draw } from '../Draw'
|
||||
|
||||
export class Highlight {
|
||||
export class Highlight extends AbstractRichText {
|
||||
|
||||
private options: Required<IEditorOption>
|
||||
|
||||
constructor(draw: Draw) {
|
||||
super()
|
||||
this.options = draw.getOptions()
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, color: string, x: number, y: number, width: number, height: number) {
|
||||
public render(ctx: CanvasRenderingContext2D) {
|
||||
if (!this.fillRect.width) return
|
||||
const { highlightAlpha } = this.options
|
||||
const { x, y, width, height } = this.fillRect
|
||||
ctx.save()
|
||||
ctx.globalAlpha = highlightAlpha
|
||||
ctx.fillStyle = color
|
||||
ctx.fillStyle = this.fillColor!
|
||||
ctx.fillRect(x, y, width, height)
|
||||
ctx.restore()
|
||||
this.clearFillInfo()
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue