commit
3f23691cc9
|
After Width: | Height: | Size: 602 B |
@ -0,0 +1,33 @@
|
|||||||
|
import { IEditorOption } from "../../.."
|
||||||
|
import { DeepRequired } from "../../../interface/Common"
|
||||||
|
import { Draw } from "../Draw"
|
||||||
|
|
||||||
|
export class Watermark {
|
||||||
|
|
||||||
|
private draw: Draw
|
||||||
|
private options: DeepRequired<IEditorOption>
|
||||||
|
|
||||||
|
constructor(draw: Draw) {
|
||||||
|
this.draw = draw
|
||||||
|
this.options = <DeepRequired<IEditorOption>>draw.getOptions()
|
||||||
|
}
|
||||||
|
|
||||||
|
public render(ctx: CanvasRenderingContext2D) {
|
||||||
|
const { watermark: { data, opacity, font, size, color }, scale } = this.options
|
||||||
|
const width = this.draw.getWidth()
|
||||||
|
const height = this.draw.getHeight()
|
||||||
|
const x = width / 2
|
||||||
|
const y = height / 2
|
||||||
|
ctx.save()
|
||||||
|
ctx.globalAlpha = opacity
|
||||||
|
ctx.font = `${size * scale}px ${font}`
|
||||||
|
ctx.fillStyle = color
|
||||||
|
// 移动到中心位置再旋转
|
||||||
|
const measureText = ctx.measureText(data)
|
||||||
|
ctx.translate(x, y)
|
||||||
|
ctx.rotate(-45 * Math.PI / 180)
|
||||||
|
ctx.fillText(data, - measureText.width / 2, measureText.actualBoundingBoxAscent - size / 2)
|
||||||
|
ctx.restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
import { IHeader } from "../../interface/Header"
|
||||||
|
|
||||||
|
export const defaultHeaderOption: Readonly<Required<IHeader>> = {
|
||||||
|
data: '',
|
||||||
|
color: '#AAAAAA',
|
||||||
|
size: 14,
|
||||||
|
font: 'Yahei'
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
import { IWatermark } from "../../interface/Watermark"
|
||||||
|
|
||||||
|
export const defaultWatermarkOption: Readonly<Required<IWatermark>> = {
|
||||||
|
data: '',
|
||||||
|
color: '#AEB5C0',
|
||||||
|
opacity: 0.3,
|
||||||
|
size: 200,
|
||||||
|
font: 'Yahei'
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
export interface IWatermark {
|
||||||
|
data: string;
|
||||||
|
color?: string;
|
||||||
|
opacity?: number;
|
||||||
|
size?: number;
|
||||||
|
font?: string;
|
||||||
|
}
|
||||||
Loading…
Reference in new issue