feat:add watermark
This commit is contained in:
@@ -31,6 +31,7 @@ import { Header } from "./frame/Header"
|
||||
import { SuperscriptParticle } from "./particle/Superscript"
|
||||
import { SubscriptParticle } from "./particle/Subscript"
|
||||
import { SeparatorParticle } from "./particle/Separator"
|
||||
import { Watermark } from "./frame/Watermark"
|
||||
|
||||
export class Draw {
|
||||
|
||||
@@ -59,6 +60,7 @@ export class Draw {
|
||||
private tableParticle: TableParticle
|
||||
private tableTool: TableTool
|
||||
private pageNumber: PageNumber
|
||||
private waterMark: Watermark
|
||||
private header: Header
|
||||
private hyperlinkParticle: HyperlinkParticle
|
||||
private separatorParticle: SeparatorParticle
|
||||
@@ -102,6 +104,7 @@ export class Draw {
|
||||
this.tableParticle = new TableParticle(this)
|
||||
this.tableTool = new TableTool(this)
|
||||
this.pageNumber = new PageNumber(this)
|
||||
this.waterMark = new Watermark(this)
|
||||
this.header = new Header(this)
|
||||
this.hyperlinkParticle = new HyperlinkParticle(this)
|
||||
this.separatorParticle = new SeparatorParticle()
|
||||
@@ -697,6 +700,10 @@ export class Draw {
|
||||
if (this.searchMatchList) {
|
||||
this.search.render(ctx, pageNo)
|
||||
}
|
||||
// 绘制水印
|
||||
if (this.options.watermark.data) {
|
||||
this.waterMark.render(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
public render(payload?: IDrawOption) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user