User interaction in EJ2 TypeScript Signature control

15 May 20239 minutes to read

The below interactions were available in Signature, and we can walk through one by one.

  • Undo and Redo
  • Clear
  • Disabled
  • ReadOnly

Undo and Redo

In the Signature, every action can be maintained as a snap for undo and redo operations. And maintained SnapIndex for indexing the snap collection.

The undo method reverts the last action of signature by decreasing SnapIndex value to index previous snap. Here, canUndo method is used to ensure whether undo can be performed or not.

The redo method reverts the last undo action of the signature by increasing the SnapIndex to get the next snap. Here, canRedo method is used to ensure whether redo can be performed or not.

Clear

The clear method is used to clears the signature and makes the canvas empty. This is also considered in Undo/ Redo. Here, isEmpty method is used to ensure whether the signature is empty or not.

Disabled

The disabled property is used to enables/disables the signature control. In the disabled state, the user is not allowed to draw signature. And it can’t be focused until the user enabled the signature.

ReadOnly

The isReadOnly property is used to enables/disables the ReadOnly Signature. It can be focused but it prevents drawing in Signature.

The following sample explains about user interactions available in signature.

import { Signature, SignatureChangeEventArgs } from '@syncfusion/ej2-inputs';
import { CheckBox, ChangeEventArgs, Button } from '@syncfusion/ej2-buttons';

let signature: Signature = new Signature({change: changeEvent});
signature.appendTo('#signature');

let undoButton: Button = new Button({cssClass: 'e-primary', disabled: true}, '#undo');
undoButton.element.onclick = function (e) {
    if (!signature.disabled && !signature.isReadOnly) {
        signature.undo();
    }
};

let redoButton: Button = new Button({cssClass: 'e-primary', disabled: true}, '#redo');
redoButton.element.onclick = function (e) {
    if (!signature.disabled && !signature.isReadOnly) {
        signature.redo();
    }
};

let clearButton: Button = new Button({cssClass: 'e-primary', disabled: true}, '#clear');
clearButton.element.onclick = function (e) {
    if (!signature.disabled && !signature.isReadOnly) {
        signature.clear();
    }
};

let checkBox1: CheckBox = new CheckBox({ label: 'Disabled', change: onChangeDisable}, '#disable');
function onChangeDisable(args: ChangeEventArgs): void {
        signature.disabled = args.checked;
    }

let checkBox2: CheckBox = new CheckBox({ label: 'ReadOnly', change: onChangeReadonly}, '#readonly');
function onChangeReadonly(args: ChangeEventArgs): void {
        signature.isReadOnly = args.checked;
    }

function changeEvent(args: SignatureChangeEventArgs) {
    if (!signature.disabled && !signature.isReadOnly) {
        if (signature.canUndo()) {
            undoButton.disabled = false;
        } else {
            undoButton.disabled = true;
        }
        if (signature.canRedo()) {
            redoButton.disabled = false;
        } else {
            redoButton.disabled = true;
        }
        if (!signature.isEmpty()) {
            clearButton.disabled = false;
        } else {
            clearButton.disabled = true;
        }
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Signature</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="TypeScript Signature Component" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-dropdowns/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id="option">
            <table>
                <tr>
                    <td>
                        <button id="undo" style="margin-right: 20px;">UNDO</button>
                    </td><td>
                        <button id="redo" style="margin-right: 20px;">REDO</button>
                    </td><td>
                        <button id="clear" style="margin-right: 200px;">CLEAR</button>
                    </td><td>
                        <div style="margin-bottom: 5px;"><input id="disable" type="checkbox" /></div>
                        <div><input id="readonly" type="checkbox" /></div>
                    </td>
                </tr>
            </table>
        </div>
        <div id ="signature-control">
            <canvas id="signature" style="height: 100%; width: 100%;"></canvas>
        </div>
    </div>
</body>
</html>
#container {
  visibility: hidden;
}

#loader {
  color: #008cff;
  font-family: 'Helvetica Neue','calibiri';
  font-size: 14px;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

#signature-control {
  height: 300px;
  width: 550px;
}

#signature {
  border: 1px solid lightgray;
}

#option {
  margin-bottom: 10px;
}