Class NuvSignaturePad
- Namespace
- NuvTools.AspNetCore.Blazor.Components
- Assembly
- NuvTools.AspNetCore.Blazor.dll
A canvas to sign on with a finger, a stylus or the mouse, captured as an image.
public class NuvSignaturePad : ComponentBase, IComponent, IHandleEvent, IHandleAfterRender, IAsyncDisposable
- Inheritance
-
NuvSignaturePad
- Implements
- Inherited Members
Examples
<NuvSignaturePad @ref="_pad" Style="width:100%;height:200px;border:1px dashed #999" />
@code {
private NuvSignaturePad _pad = default!;
private async Task SignAsync()
{
var signature = await _pad.CaptureAsync();
if (signature is null) return; // nothing drawn
await SaveAsync(signature.Base64);
}
}
Remarks
Draws with pointer events, so touch, pen and mouse behave the same, and scales the drawing buffer to
the device pixel ratio so strokes stay sharp on high-density screens. It renders no layout of its own:
size it through Class/Style. Capture the component with @ref to
clear it, check whether anything was drawn, and capture the signature.
Properties
AdditionalAttributes
Any other attribute is applied to the canvas element.
[Parameter(CaptureUnmatchedValues = true)]
public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
Property Value
BackgroundColor
Background painted under the strokes, as any CSS colour; null keeps it transparent.
A filled background keeps the signature readable when the image is shown on a dark page.
[Parameter]
public string? BackgroundColor { get; set; }
Property Value
Class
CSS class applied to the canvas.
[Parameter]
public string? Class { get; set; }
Property Value
OnStarted
Raised when the first stroke of an empty pad begins.
[Parameter]
public EventCallback OnStarted { get; set; }
Property Value
PenColor
Stroke colour, as any CSS colour.
[Parameter]
public string PenColor { get; set; }
Property Value
PenWidth
Stroke width, in CSS pixels.
[Parameter]
public double PenWidth { get; set; }
Property Value
Style
Inline style applied to the canvas. Give it a size: the canvas has none of its own.
[Parameter]
public string? Style { get; set; }
Property Value
Methods
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
CaptureAsync(CancellationToken)
The signature as a PNG, or null when nothing was drawn.
public Task<SignatureCapture?> CaptureAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe.
Returns
ClearAsync(CancellationToken)
Erases every stroke.
public Task ClearAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe.
Returns
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
IsEmptyAsync(CancellationToken)
Whether nothing has been drawn since the pad was created or last cleared.
public Task<bool> IsEmptyAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe.
Returns
NotifyStartedAsync()
Called by the module when the first stroke of an empty pad begins.
[JSInvokable]
public Task NotifyStartedAsync()
Returns
OnAfterRenderAsync(bool)
Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.
This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.
Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRenderboolSet to
trueif this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwisefalse.
Returns
Remarks
The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods
are useful for performing interop, or interacting with values received from @ref.
Use the firstRender parameter to ensure that initialization work is only performed
once.