37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { PDFPage, rgb } from 'pdf-lib';
|
|
import { PDFElement } from './PDFElement.js';
|
|
|
|
export class Checkbox extends PDFElement {
|
|
private borderWidth: number = 1;
|
|
|
|
constructor(protected _width: number, protected _height: number, public checked: boolean = false) {
|
|
super();
|
|
}
|
|
|
|
addChild(...children: PDFElement[]): void {
|
|
throw new Error('Method not supported.');
|
|
}
|
|
|
|
async draw(page: PDFPage, x: number, y: number) {
|
|
page.drawRectangle({
|
|
x: x + this.borderWidth,
|
|
// NOTE: Keine Ahnung warum * 1.5 aber dann passt es...
|
|
y: y - this._height - this.borderWidth * 1.5,
|
|
width: this._width,
|
|
height: this._height,
|
|
borderColor: rgb(0, 0, 0),
|
|
borderWidth: this.borderWidth
|
|
});
|
|
|
|
if (this.checked) {
|
|
page.drawSvgPath(`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M20.285 2l-11.285 11.567-5.286-5.011-3.714 3.716 9 8.728 15-15.285z"/></svg>`, {
|
|
x: x + this.borderWidth,
|
|
y: y - this.borderWidth * 1.5,
|
|
borderColor: rgb(0,0,0),
|
|
color: rgb(0,0,0),
|
|
scale: this._width / 24
|
|
})
|
|
}
|
|
}
|
|
}
|