feat:add watermark
This commit is contained in:
@@ -31,6 +31,7 @@ import { Header } from "./frame/Header"
|
|||||||
import { SuperscriptParticle } from "./particle/Superscript"
|
import { SuperscriptParticle } from "./particle/Superscript"
|
||||||
import { SubscriptParticle } from "./particle/Subscript"
|
import { SubscriptParticle } from "./particle/Subscript"
|
||||||
import { SeparatorParticle } from "./particle/Separator"
|
import { SeparatorParticle } from "./particle/Separator"
|
||||||
|
import { Watermark } from "./frame/Watermark"
|
||||||
|
|
||||||
export class Draw {
|
export class Draw {
|
||||||
|
|
||||||
@@ -59,6 +60,7 @@ export class Draw {
|
|||||||
private tableParticle: TableParticle
|
private tableParticle: TableParticle
|
||||||
private tableTool: TableTool
|
private tableTool: TableTool
|
||||||
private pageNumber: PageNumber
|
private pageNumber: PageNumber
|
||||||
|
private waterMark: Watermark
|
||||||
private header: Header
|
private header: Header
|
||||||
private hyperlinkParticle: HyperlinkParticle
|
private hyperlinkParticle: HyperlinkParticle
|
||||||
private separatorParticle: SeparatorParticle
|
private separatorParticle: SeparatorParticle
|
||||||
@@ -102,6 +104,7 @@ export class Draw {
|
|||||||
this.tableParticle = new TableParticle(this)
|
this.tableParticle = new TableParticle(this)
|
||||||
this.tableTool = new TableTool(this)
|
this.tableTool = new TableTool(this)
|
||||||
this.pageNumber = new PageNumber(this)
|
this.pageNumber = new PageNumber(this)
|
||||||
|
this.waterMark = new Watermark(this)
|
||||||
this.header = new Header(this)
|
this.header = new Header(this)
|
||||||
this.hyperlinkParticle = new HyperlinkParticle(this)
|
this.hyperlinkParticle = new HyperlinkParticle(this)
|
||||||
this.separatorParticle = new SeparatorParticle()
|
this.separatorParticle = new SeparatorParticle()
|
||||||
@@ -697,6 +700,10 @@ export class Draw {
|
|||||||
if (this.searchMatchList) {
|
if (this.searchMatchList) {
|
||||||
this.search.render(ctx, pageNo)
|
this.search.render(ctx, pageNo)
|
||||||
}
|
}
|
||||||
|
// 绘制水印
|
||||||
|
if (this.options.watermark.data) {
|
||||||
|
this.waterMark.render(ctx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public render(payload?: IDrawOption) {
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+11
-1
@@ -16,6 +16,7 @@ import { IContextMenuContext, IRegisterContextMenu } from './interface/contextme
|
|||||||
import { EditorComponent } from './dataset/enum/Editor'
|
import { EditorComponent } from './dataset/enum/Editor'
|
||||||
import { EDITOR_COMPONENT } from './dataset/constant/Editor'
|
import { EDITOR_COMPONENT } from './dataset/constant/Editor'
|
||||||
import { IHeader } from './interface/Header'
|
import { IHeader } from './interface/Header'
|
||||||
|
import { IWatermark } from './interface/Watermark'
|
||||||
|
|
||||||
export default class Editor {
|
export default class Editor {
|
||||||
|
|
||||||
@@ -31,6 +32,14 @@ export default class Editor {
|
|||||||
font: 'Yahei',
|
font: 'Yahei',
|
||||||
...options.header
|
...options.header
|
||||||
}
|
}
|
||||||
|
const waterMarkOptions: Required<IWatermark> = {
|
||||||
|
data: '',
|
||||||
|
color: '#AEB5C0',
|
||||||
|
opacity: 0.3,
|
||||||
|
size: 200,
|
||||||
|
font: 'Yahei',
|
||||||
|
...options.watermark
|
||||||
|
}
|
||||||
const editorOptions: Required<IEditorOption> = {
|
const editorOptions: Required<IEditorOption> = {
|
||||||
defaultType: 'TEXT',
|
defaultType: 'TEXT',
|
||||||
defaultFont: 'Yahei',
|
defaultFont: 'Yahei',
|
||||||
@@ -62,7 +71,8 @@ export default class Editor {
|
|||||||
defaultHyperlinkColor: '#0000FF',
|
defaultHyperlinkColor: '#0000FF',
|
||||||
headerTop: 50,
|
headerTop: 50,
|
||||||
...options,
|
...options,
|
||||||
header: headerOptions
|
header: headerOptions,
|
||||||
|
watermark: waterMarkOptions
|
||||||
}
|
}
|
||||||
formatElementList(elementList)
|
formatElementList(elementList)
|
||||||
// 监听
|
// 监听
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { IElement } from ".."
|
import { IElement } from ".."
|
||||||
import { IHeader } from "./Header"
|
import { IHeader } from "./Header"
|
||||||
|
import { IWatermark } from "./Watermark"
|
||||||
|
|
||||||
export interface IEditorOption {
|
export interface IEditorOption {
|
||||||
defaultType?: string;
|
defaultType?: string;
|
||||||
@@ -32,6 +33,7 @@ export interface IEditorOption {
|
|||||||
defaultHyperlinkColor?: string;
|
defaultHyperlinkColor?: string;
|
||||||
headerTop?: number;
|
headerTop?: number;
|
||||||
header?: IHeader;
|
header?: IHeader;
|
||||||
|
watermark?: IWatermark;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IEditorResult {
|
export interface IEditorResult {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export interface IWatermark {
|
||||||
|
data: string;
|
||||||
|
color?: string;
|
||||||
|
opacity?: number;
|
||||||
|
size?: number;
|
||||||
|
font?: string;
|
||||||
|
}
|
||||||
@@ -211,6 +211,9 @@ window.onload = function () {
|
|||||||
margins: [100, 120, 100, 120],
|
margins: [100, 120, 100, 120],
|
||||||
header: {
|
header: {
|
||||||
data: '人民医院门诊'
|
data: '人民医院门诊'
|
||||||
|
},
|
||||||
|
watermark: {
|
||||||
|
data: '严禁复制'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log('实例: ', instance)
|
console.log('实例: ', instance)
|
||||||
|
|||||||
Reference in New Issue
Block a user