@antv/x6
Version:
JavaScript diagramming library that uses SVG and HTML for rendering
37 lines (30 loc) • 970 B
text/typescript
import { toRad } from '../../geometry'
import type { BackgroundCommonOptions, BackgroundDefinition } from './index'
export interface WatermarkOptions extends BackgroundCommonOptions {
angle?: number
}
export const watermark: BackgroundDefinition<WatermarkOptions> = (
img,
options,
) => {
const width = img.width
const height = img.height
const canvas = document.createElement('canvas')
canvas.width = width * 3
canvas.height = height * 3
const ctx = canvas.getContext('2d')!
const angle = options.angle != null ? -options.angle : -20
const radians = toRad(angle)
const stepX = canvas.width / 4
const stepY = canvas.height / 4
for (let i = 0; i < 4; i += 1) {
for (let j = 0; j < 4; j += 1) {
if ((i + j) % 2 > 0) {
ctx.setTransform(1, 0, 0, 1, (2 * i - 1) * stepX, (2 * j - 1) * stepY)
ctx.rotate(radians)
ctx.drawImage(img, -width / 2, -height / 2, width, height)
}
}
}
return canvas
}