User interaction in Vue Signature component

11 Jun 202415 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 component. 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.

<template>
  <div class='wrap'>
      <div id="option">
          <table>
              <tr>
                  <td>
                      <ejs-button id="undo" cssClass="e-primary" :disabled="undoDisable"
                          v-on:click="onUndo">UNDO</ejs-button>
                  </td>
                  <td>
                      <ejs-button id="redo" cssClass="e-primary" :disabled="redoDisable"
                          v-on:click="onRedo">REDO</ejs-button>
                  </td>
                  <td>
                      <ejs-button id="clear" cssClass="e-primary" :disabled="clearDisable"
                          v-on:click="onClear">CLEAR</ejs-button>
                  </td>
                  <td>
                      <div style="margin-bottom: 5px; margin-left: 200px;"><ejs-checkbox label='Disabled'
                              :change="disableChange"></ejs-checkbox></div>
                      <div style="margin-left: 200px;"><ejs-checkbox label='ReadOnly'
                              :change="readOnlyChange"></ejs-checkbox></div>
                  </td>
              </tr>
          </table>
      </div>
      <div id="signature-control">
          <ejs-signature id="signature" :change="onChange"></ejs-signature>
      </div>
  </div>
</template>
  
<script setup>

import { SignatureComponent as EjsSignature } from "@syncfusion/ej2-vue-inputs";
import { ButtonComponent as EjsButton } from '@syncfusion/ej2-vue-buttons';
import { getComponent } from '@syncfusion/ej2-base';
import { CheckBoxComponent as EjsCheckbox } from "@syncfusion/ej2-vue-buttons";
import { enableRipple } from '@syncfusion/ej2-base';
import { ref } from 'vue';

// Define reactive variables
let undoDisable = ref(true);
let redoDisable = ref(true);
let clearDisable = ref(true);
enableRipple(true);


const onUndo = () => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  if (!signature.disabled && !signature.isReadOnly) {
      signature.undo();
  }
};
const onRedo = () => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  if (!signature.disabled && !signature.isReadOnly) {
      signature.redo();
  }
};
const onClear = () => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  if (!signature.disabled && !signature.isReadOnly) {
      signature.clear();
  }
};
const disableChange = (args) => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  signature.disabled = args.checked;
};
const readOnlyChange = (args) => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  signature.isReadOnly = args.checked;
};
const onChange = () => {
  var signature = getComponent(document.getElementById('signature'), 'signature');
  if (!signature.disabled && !signature.isReadOnly) {
      if (signature.canUndo()) {
          undoDisable.value = false;
      } else {
          undoDisable.value = true;
      }
      if (signature.canRedo()) {
          redoDisable.value = false;
      } else {
          redoDisable.value = true;
      }
      if (!signature.isEmpty()) {
          clearDisable.value = false;
      } else {
          clearDisable.value = true;
      }
  }
};

</script>
  
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";

#signature {
  border: 1px solid lightgray;
  height: 100%;
  width: 100%;
}

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

#option {
  margin-bottom: 10px;
}

.wrap {
  height: 300px;
  width: 100%;
}
</style>
<template>
  <div class='wrap'>
    <div id="option">
      <table>
        <tr>
          <td>
            <ejs-button id="undo" cssClass="e-primary" :disabled="undoDisable"
              v-on:click="onUndo">UNDO</ejs-button>
          </td>
          <td>
            <ejs-button id="redo" cssClass="e-primary" :disabled="redoDisable"
              v-on:click="onRedo">REDO</ejs-button>
          </td>
          <td>
            <ejs-button id="clear" cssClass="e-primary" :disabled="clearDisable"
              v-on:click="onClear">CLEAR</ejs-button>
          </td>
          <td>
            <div style="margin-bottom: 5px; margin-left: 200px;"><ejs-checkbox label='Disabled'
                :change="disableChange"></ejs-checkbox></div>
            <div style="margin-left: 200px;"><ejs-checkbox label='ReadOnly' :change="readOnlyChange"></ejs-checkbox></div>
          </td>
        </tr>
      </table>
    </div>
    <div id="signature-control">
      <ejs-signature id="signature" :change="onChange"></ejs-signature>
    </div>
  </div>
</template>

<script>

import { SignatureComponent } from "@syncfusion/ej2-vue-inputs";
import { ButtonComponent } from '@syncfusion/ej2-vue-buttons';
import { getComponent } from '@syncfusion/ej2-base';
import { CheckBoxComponent } from "@syncfusion/ej2-vue-buttons";
import { enableRipple } from '@syncfusion/ej2-base';

enableRipple(true);

export default {
  name: "App",
  components: {
    "ejs-button": ButtonComponent,
    "ejs-checkbox": CheckBoxComponent,
    "ejs-signature": SignatureComponent
  },
  data: function () {
    return {
      undoDisable: true, redoDisable: true, clearDisable: true,
    };
  },
  methods: {
    onUndo: function () {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      if (!signature.disabled && !signature.isReadOnly) {
        signature.undo();
      }
    },
    onRedo: function () {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      if (!signature.disabled && !signature.isReadOnly) {
        signature.redo();
      }
    },
    onClear: function () {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      if (!signature.disabled && !signature.isReadOnly) {
        signature.clear();
      }
    },
    disableChange: function (args) {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      signature.disabled = args.checked;
    },
    readOnlyChange: function (args) {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      signature.isReadOnly = args.checked;
    },
    onChange: function () {
      var signature = getComponent(document.getElementById('signature'), 'signature');
      if (!signature.disabled && !signature.isReadOnly) {
        if (signature.canUndo()) {
          this.undoDisable = false;
        } else {
          this.undoDisable = true;
        }
        if (signature.canRedo()) {
          this.redoDisable = false;
        } else {
          this.redoDisable = true;
        }
        if (!signature.isEmpty()) {
          this.clearDisable = false;
        } else {
          this.clearDisable = true;
        }
      }
    }
  }
}
</script>

<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";

#signature {
  border: 1px solid lightgray;
  height: 100%;
  width: 100%;
}

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

#option {
  margin-bottom: 10px;
}

.wrap {
  height: 300px;
  width: 100%;
}</style>