Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add canvasContextOptions API for use by the getContext #761

Merged
merged 7 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ signaturePad.on();
<dd>(string) Color used to draw the lines. Can be any color format accepted by <code>context.fillStyle</code>. Defaults to <code>"black"</code>.</dd>
<dt>velocityFilterWeight</dt>
<dd>(float) Weight used to modify new velocity based on the previous velocity. Defaults to <code>0.7</code>.</dd>
<dt>canvasContextOptions</dt>
<dd>(CanvasRenderingContext2DSettings) part of the Canvas API, provides the 2D rendering context for the drawing surface of a <code>canvas</code> element. It is used for drawing shapes, text, images, and other objects (<a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getContextAttributes">MDN</a>).</dd>
</dl>

You can set options during initialization:
Expand Down
5 changes: 3 additions & 2 deletions docs/js/signature_pad.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/signature_pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Options extends Partial<PointGroupOptions> {
minDistance?: number;
backgroundColor?: string;
throttle?: number;
canvasContextOptions?: CanvasRenderingContext2DSettings;
}

export interface PointGroup extends PointGroupOptions {
Expand All @@ -65,6 +66,7 @@ export default class SignaturePad extends SignatureEventTarget {
public compositeOperation: GlobalCompositeOperation;
public backgroundColor: string;
public throttle: number;
public canvasContextOptions: CanvasRenderingContext2DSettings;

// Private stuff
/* tslint:disable: variable-name */
Expand Down Expand Up @@ -94,11 +96,17 @@ export default class SignaturePad extends SignatureEventTarget {
this.penColor = options.penColor || 'black';
this.backgroundColor = options.backgroundColor || 'rgba(0,0,0,0)';
this.compositeOperation = options.compositeOperation || 'source-over';
this.canvasContextOptions = (
'canvasContextOptions' in options ? options.canvasContextOptions : {}
) as CanvasRenderingContext2DSettings;

this._strokeMoveUpdate = this.throttle
? throttle(SignaturePad.prototype._strokeUpdate, this.throttle)
: SignaturePad.prototype._strokeUpdate;
this._ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
this._ctx = canvas.getContext(
'2d',
this.canvasContextOptions,
) as CanvasRenderingContext2D;

this.clear();

Expand Down
Loading
Loading