parent
fab200531f
commit
5043dfbd6b
@ -0,0 +1,31 @@
|
|||||||
|
import { DeepRequired } from "../../../interface/Common"
|
||||||
|
import { IEditorOption } from "../../../interface/Editor"
|
||||||
|
import { Draw } from "../Draw"
|
||||||
|
|
||||||
|
export class Header {
|
||||||
|
|
||||||
|
private draw: Draw
|
||||||
|
private options: DeepRequired<IEditorOption>
|
||||||
|
|
||||||
|
constructor(draw: Draw) {
|
||||||
|
this.draw = draw
|
||||||
|
this.options = <DeepRequired<IEditorOption>>draw.getOptions()
|
||||||
|
}
|
||||||
|
|
||||||
|
public render(ctx: CanvasRenderingContext2D) {
|
||||||
|
const { header: { data, size, color, font }, scale } = this.options
|
||||||
|
if (!data) return
|
||||||
|
const width = this.draw.getWidth()
|
||||||
|
const top = this.draw.getHeaderTop()
|
||||||
|
ctx.save()
|
||||||
|
ctx.fillStyle = color
|
||||||
|
ctx.font = `${size! * scale}px ${font}`
|
||||||
|
// 文字长度
|
||||||
|
const textWidth = ctx.measureText(`${data}`).width
|
||||||
|
// 偏移量
|
||||||
|
const left = (width - textWidth) / 2
|
||||||
|
ctx.fillText(`${data}`, left < 0 ? 0 : left, top)
|
||||||
|
ctx.restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
export type Primitive = string | number | boolean | bigint | symbol | undefined | null
|
||||||
|
|
||||||
|
export type Builtin = Primitive | Function | Date | Error | RegExp
|
||||||
|
|
||||||
|
export type DeepRequired<T> = T extends Error
|
||||||
|
? Required<T>
|
||||||
|
: T extends Builtin
|
||||||
|
? T
|
||||||
|
: T extends Map<infer K, infer V>
|
||||||
|
? Map<DeepRequired<K>, DeepRequired<V>>
|
||||||
|
: T extends ReadonlyMap<infer K, infer V>
|
||||||
|
? ReadonlyMap<DeepRequired<K>, DeepRequired<V>>
|
||||||
|
: T extends WeakMap<infer K, infer V>
|
||||||
|
? WeakMap<DeepRequired<K>, DeepRequired<V>>
|
||||||
|
: T extends Set<infer U>
|
||||||
|
? Set<DeepRequired<U>>
|
||||||
|
: T extends ReadonlySet<infer U>
|
||||||
|
? ReadonlySet<DeepRequired<U>>
|
||||||
|
: T extends WeakSet<infer U>
|
||||||
|
? WeakSet<DeepRequired<U>>
|
||||||
|
: T extends Promise<infer U>
|
||||||
|
? Promise<DeepRequired<U>>
|
||||||
|
: T extends {}
|
||||||
|
? { [K in keyof T]-?: DeepRequired<T[K]> }
|
||||||
|
: Required<T>
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
export interface IHeader {
|
||||||
|
data: string;
|
||||||
|
color?: string;
|
||||||
|
size?: number;
|
||||||
|
font?: string;
|
||||||
|
}
|
||||||
Loading…
Reference in new issue