my-lib/classes/signature.ts
Properties |
Methods |
constructor(element: HTMLCanvasElement, render: Renderer2)
|
||||||||||||
Defined in my-lib/classes/signature.ts:46
|
||||||||||||
Parameters :
|
_getPosition | ||||||||
_getPosition(event: MouseEvent | TouchEvent)
|
||||||||
Defined in my-lib/classes/signature.ts:134
|
||||||||
Parameters :
Returns :
{ x: any; y: any; }
|
_renderCanvas |
_renderCanvas()
|
Defined in my-lib/classes/signature.ts:150
|
Returns :
void
|
_resetCanvas |
_resetCanvas()
|
Defined in my-lib/classes/signature.ts:159
|
Returns :
void
|
_resizeCanvas |
_resizeCanvas()
|
Defined in my-lib/classes/signature.ts:165
|
Returns :
void
|
clearCanvas |
clearCanvas()
|
Defined in my-lib/classes/signature.ts:125
|
Returns :
void
|
getDataURL |
getDataURL()
|
Defined in my-lib/classes/signature.ts:130
|
Returns :
any
|
init |
init()
|
Defined in my-lib/classes/signature.ts:89
|
Returns :
void
|
setAttr |
setAttr()
|
Defined in my-lib/classes/signature.ts:84
|
Returns :
void
|
setSettings | ||||||||
setSettings(settings: SignatureConfig)
|
||||||||
Defined in my-lib/classes/signature.ts:63
|
||||||||
Parameters :
Returns :
void
|
setStyle |
setStyle()
|
Defined in my-lib/classes/signature.ts:70
|
Returns :
void
|
canvas |
canvas:
|
Type : HTMLCanvasElement
|
Defined in my-lib/classes/signature.ts:24
|
ctx |
ctx:
|
Type : CanvasRenderingContext2D
|
Defined in my-lib/classes/signature.ts:26
|
currentPos |
currentPos:
|
Type : any
|
Defined in my-lib/classes/signature.ts:31
|
defaults |
defaults:
|
Type : SignatureConfig
|
Default value : {
lineColor: '#222222',
lineWidth: 1,
border: '1px dashed #AAAAAA',
background: '#FFFFFF',
width: 300,
height: 100,
autoFit: true
}
|
Defined in my-lib/classes/signature.ts:15
|
domsize |
domsize:
|
Type : DomSize
|
Defined in my-lib/classes/signature.ts:27
|
drawing |
drawing:
|
Type : boolean
|
Defined in my-lib/classes/signature.ts:29
|
lastPos |
lastPos:
|
Type : any
|
Defined in my-lib/classes/signature.ts:30
|
Public render |
render:
|
Type : Renderer2
|
Defined in my-lib/classes/signature.ts:50
|
settings |
settings:
|
Type : SignatureConfig
|
Defined in my-lib/classes/signature.ts:25
|
export interface SignatureConfig {
lineColor?: string;
lineWidth?: number;
border?: string;
background?: string;
width?: number;
height?: number;
autoFit?: boolean;
}
import { Renderer2, ViewChild, ElementRef, HostListener } from '@angular/core';
import { DomSize } from './dom';
import { fromEvent, merge } from '../rxjs';
export class SignatureAbstract {
defaults: SignatureConfig = {
lineColor: '#222222',
lineWidth: 1,
border: '1px dashed #AAAAAA',
background: '#FFFFFF',
width: 300,
height: 100,
autoFit: true
};
canvas: HTMLCanvasElement;
settings: SignatureConfig;
ctx: CanvasRenderingContext2D;
domsize: DomSize;
drawing: boolean;
lastPos: any;
currentPos: any;
@HostListener('touchstart', ['$event'])
touchstart(e) {
e.preventDefault();
}
@HostListener('touchmove', ['$event'])
touchmove(e) {
e.preventDefault();
}
@HostListener('touchend', ['$event'])
touchend(e) {
e.preventDefault();
}
constructor(
element: HTMLCanvasElement,
public render: Renderer2
) {
this.canvas = element;
this.settings = this.defaults;
this.domsize = new DomSize(this.canvas, this.render);
this.currentPos = {
x: 0,
y: 0
};
this.lastPos = this.currentPos;
}
setSettings(settings: SignatureConfig) {
this.settings = { ...this.settings, ...settings };
this.canvas.width = this.settings.width;
this.canvas.height = this.settings.height;
this.init();
}
setStyle() {
const style = {
boxSizing: 'border-box',
width: this.settings.width + 'px',
height: this.settings.height + 'px',
border: this.settings.border,
background: this.settings.background,
cursor: 'crosshair'
}
for (const k in style) {
this.render.setStyle(this.canvas, k, style[k]);
}
}
setAttr() {
this.render.setAttribute(this.canvas, 'width', '' + this.settings.width);
this.render.setAttribute(this.canvas, 'height', '' + this.settings.height);
}
init() {
this.setStyle();
if (this.settings.autoFit === true) {
this._resizeCanvas();
}
this._resetCanvas();
const end$ = merge(
fromEvent(this.canvas, 'mouseup'),
fromEvent(this.canvas, 'touchend')
);
end$.subscribe(res => {
this.drawing = false;
});
const start$ = merge(
fromEvent(this.canvas, 'mousedown'),
fromEvent(this.canvas, 'touchstart')
);
start$.map((e: any) => this._getPosition(e)).subscribe(res => {
this.drawing = true;
this.lastPos = this.currentPos = res;
});
const move$ = merge(
fromEvent(this.canvas, 'mousemove'),
fromEvent(this.canvas, 'touchmove')
);
move$.map((e: any) => this._getPosition(e)).subscribe((e: any) => {
this.currentPos = e;
this._renderCanvas();
});
}
clearCanvas() {
this.canvas.width = this.canvas.width;
this._resetCanvas();
}
getDataURL() {
return this.canvas.toDataURL();
}
_getPosition(event: MouseEvent | TouchEvent) {
let xPos, yPos, rect;
rect = this.canvas.getBoundingClientRect();
if (event instanceof TouchEvent) {
xPos = event.touches[0].clientX - rect.left;
yPos = event.touches[0].clientY - rect.top;
} else {
xPos = event.clientX - rect.left;
yPos = event.clientY - rect.top;
}
return {
x: xPos,
y: yPos
};
};
_renderCanvas() {
if (this.drawing) {
this.ctx.moveTo(this.lastPos.x, this.lastPos.y);
this.ctx.lineTo(this.currentPos.x, this.currentPos.y);
this.ctx.stroke();
this.lastPos = this.currentPos;
}
}
_resetCanvas() {
this.ctx = this.canvas.getContext("2d");
this.ctx.strokeStyle = this.settings.lineColor;
this.ctx.lineWidth = this.settings.lineWidth;
}
_resizeCanvas() {
const width = this.domsize.outerWidth(true);
this.render.setAttribute(this.canvas, 'width', width);
}
}
export class Signature extends SignatureAbstract {
}