Search results

Integration with Toolbar in JavaScript (ES5) Signature control

30 Sep 2022 / 4 minutes to read

The Signature control integrates with the toolbar and the interaction performed using the change event of the toolbar. In that, canUndo, canRedo and isEmpty methods were used to enable/disable undo, redo, and clear buttons.

Source
Preview
index.js
index.html
styles.css
Copied to clipboard
ej.base.enableRipple = true;

var signature = new ej.inputs.Signature({
    change: function(args) {
        if (!signature.isEmpty()) {
            var saveBtn = ej.base.getComponent(document.getElementById("save-option"), 'split-btn');
            clearButton();
            saveBtn.disabled = false;
        }
        updateUndoRedo();
    }
}, '#signature');

var items = [
    {
        text: 'Png'
    },
    {
        text: 'Jpg'
    },
    {
        text: 'Svg'
    }
];

var toolbarObj = new ej.navigations.Toolbar({
    width: '100%',
    items: [
        { text: 'Undo', prefixIcon: 'e-icons e-undo', tooltipText: 'Undo (Ctrl + Z)' },
        {  text: 'Redo', prefixIcon: 'e-icons e-redo', tooltipText: 'Redo (Ctrl + Y)' },
        { type: 'Separator' },
        { tooltipText: 'Save (Ctrl + S)', type: 'Button', template: '<button id="save-option"></button>' },
        { type: 'Separator' },
        { tooltipText: 'Stroke Color', type: 'Input', template: '<input id="stroke-color" type="color"/>' },
        { type: 'Separator' },
        { tooltipText: 'Background Color', type: 'Input', template: '<input id="bg-color" type="color"/>' },
        { type: 'Separator' },
        { tooltipText: 'Stroke Width', type: 'Input', template: '<input id="stroke-width" type="text"/>' },
        { type: 'Separator' },
        { text: 'Clear', prefixIcon: 'e-sign-icons e-clear', tooltipText: 'Clear' },
        { tooltipText: 'Disabled', type: 'Input', template: new ej.buttons.CheckBox({ label: 'Disabled', checked: false, change: disabledChange}), align: 'Right' }
    ],
    created: function(args) {
    var ddl = new ej.dropdowns.DropDownList({
        dataSource:  [1, 2, 3, 4, 5],
        width: '120',
        value: 1,
        change: function(args) {
            signature.maxStrokeWidth = args.value;
        } 
    });
    ddl.appendTo('#stroke-width');
    new ej.splitbuttons.SplitButton({iconCss: 'e-sign-icons e-save', items: items, select: onSelect, disabled: true }, '#save-option');
    var strokeColor = new ej.inputs.ColorPicker({
        presetColors: {
            'custom': ['#000000', '#e91e63', '#9c27b0', '#673ab7', '#2196f3', '#03a9f4', '#00bcd4',
            '#009688', '#8bc34a', '#cddc39', '#ffeb3b', '#ffc107']
        },
        modeSwitcher: false,
        columns: 4,
        beforeTileRender: function(args) {
            args.element.classList.add('e-circle-palette');
            args.element.appendChild(ej.base.createElement('span', { className: 'e-circle-selection' }));
        },
        showButtons: false, mode: 'Palette', cssClass: 'e-stroke-color', change: strokeColorChanged});
        strokeColor.appendTo('#stroke-color');
        var bgColor = new ej.inputs.ColorPicker({
            modeSwitcher: false,
            columns: 4,
    		beforeTileRender: function(args) {
                args.element.classList.add('e-circle-palette');
                args.element.appendChild(ej.base.createElement('span', { className: 'e-circle-selection' }));
            },
            presetColors: {
                'custom': ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#2196f3', '#03a9f4', '#00bcd4',
                '#009688', '#8bc34a', '#cddc39', '#ffeb3b', '#ffffff']
            },
            showButtons: false, mode: 'Palette', cssClass: 'e-bg-color', change: bgColorChanged});
        bgColor.appendTo('#bg-color');
        ej.base.addClass([strokeColor.element.nextElementSibling.querySelector('.e-selected-color')], 'e-sign-icons');
        ej.base.addClass([bgColor.element.nextElementSibling.querySelector('.e-selected-color')], 'e-sign-icons');
        clearButton();
        var toolbarlItems = document.querySelectorAll('.e-toolbar .e-toolbar-items .e-toolbar-item .e-tbar-btn.e-tbtn-txt');
        for (var i = 0; i < toolbarlItems.length; i++) {
            if (toolbarlItems[i].children[0].classList.contains('e-undo')) {
                var undoButton = ej.base.getComponent(toolbarlItems[i] , 'btn');
                undoButton.disabled = true;
            }
            if (toolbarlItems[i].children[0].classList.contains('e-redo')) {
                var redoButton= ej.base.getComponent(toolbarlItems[i], 'btn');
                redoButton.disabled = true;
            }
        }
    },
    clicked: function(args) {
        var saveBtn = ej.base.getComponent(document.getElementById("save-option"), 'split-btn');
        if (signature.disabled && args.item.tooltipText != 'Disabled') {
            return;
        }
        switch (args.item.tooltipText) {
            case 'Undo (Ctrl + Z)':
                if (signature.canUndo()) {
                    signature.undo();
                    updateUndoRedo();
                }
                break;
            case 'Redo (Ctrl + Y)':
                if (signature.canRedo()) {
                    signature.redo();
                    updateUndoRedo();
                }
                break;
            case 'Clear':
                signature.clear();
                if (signature.isEmpty()) {
                    clearButton();
                    saveBtn.disabled = true;
                }
                break;
        }
    }
});
toolbarObj.appendTo('#toolbar');

function onSelect(args) {
    signature.save(args.item.text, 'Signature');
}

function strokeColorChanged(args) {
    if (signature.disabled) {
        return;
    }
    var selElem = this.element.nextElementSibling.querySelector('.e-selected-color');
    selElem.style.borderBottomColor = args.currentValue.rgba;
    signature.strokeColor = args.currentValue.rgba;
}

function bgColorChanged(args) {
    if (signature.disabled) {
        return;
    }
    var selElem = this.element.nextElementSibling.querySelector('.e-selected-color');
    signature.backgroundColor = args.currentValue.rgba;
    selElem.style.borderBottomColor = args.currentValue.rgba;
}

function clearButton() {
    var tlItems = document.querySelectorAll('.e-toolbar .e-toolbar-items .e-toolbar-item .e-tbar-btn.e-tbtn-txt');
    for (var i = 0; i < tlItems.length; i++) {
        if (tlItems[i].children[0].classList.contains('e-clear')) {
            var clrBtn = ej.base.getComponent(tlItems[i], 'btn');
            if (signature.isEmpty()) {
                clrBtn.disabled = true;
            } else {
                clrBtn.disabled = false;
            }
        }
    }
}

function disabledChange(args) {
    signature.disabled = args.checked;
}

document.getElementById('save-option').onclick = function (e) {
    signature.save();
};

function updateUndoRedo() {
    var undoButton; var redoButton;
    var tlItems = document.querySelectorAll('.e-toolbar .e-toolbar-items .e-toolbar-item .e-tbar-btn.e-tbtn-txt');
    for (var i = 0; i < tlItems.length; i++) {
        if (tlItems[i].children[0].classList.contains('e-undo')) {
            undoButton = ej.base.getComponent(tlItems[i], 'btn'); 
        }
        if (tlItems[i].children[0].classList.contains('e-redo')) {
            redoButton = ej.base.getComponent(tlItems[i], 'btn');
        }
    }
    if (signature.canUndo()) {
        undoButton.disabled = false;
    } else {
        undoButton.disabled = true;
    }
    if (signature.canRedo()) {
        redoButton.disabled = false;
    } else {
        redoButton.disabled = true;
    }
}
Copied to clipboard
<!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="//cdn.syncfusion.com/ej2/20.3.47/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-dropdowns/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/20.3.47/ej2-popups/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/20.3.47/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
    
    <div id="container">
        <div id="signature-toolbar-control">
            <div id="toolbar" style="width: 100%;"></div>
            <div id="signature-control">
                <canvas id="signature" style="height: 100%; width: 100%;"></canvas>
            </div>
        </div>
    </div>

<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Copied to clipboard
#container {
    visibility: hidden;
}

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

@font-face {
  font-family: 'font-icons';
  src:
  url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1M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250IGdlbmVyYXRlZCB1c2luZyBTeW5jZnVzaW9uIE1ldHJvIFN0dWRpb3d3dy5zeW5jZnVzaW9uLmNvbQAgAGYAbwBuAHQALQBpAGMAbwBuAHMAUgBlAGcAdQBsAGEAcgBmAG8AbgB0AC0AaQBjAG8AbgBzAGYAbwBuAHQALQBpAGMAbwBuAHMAVgBlAHIAcwBpAG8AbgAgADEALgAwAGYAbwBuAHQALQBpAGMAbwBuAHMARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAdQBzAGkAbgBnACAAUwB5AG4AYwBmAHUAcwBpAG8AbgAgAE0AZQB0AHIAbwAgAFMAdAB1AGQAaQBvAHcAdwB3AC4AcwB5AG4AYwBmAHUAcwBpAG8AbgAuAGMAbwBtAAAAAAIAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgECAQMBBAEFAQYBBwEIAQkBCgELAAdzYXZlXzAyB3NhdmUtMDEHZWRpdF8wMwdlZGl0XzAxBWNsZWFyDHBhaW50LWJ1Y2tldA9wYWludC1idWNrZXQtd2YGaW1hZ2VzC3BpY3R1cmVzLXdmAAAA) format('truetype');
  font-weight: normal;
  font-style: normal;
}

.e-sign-icons {
  font-family: 'font-icons' !important;
  font-size: 55px;
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#toolbar-control {
  border: 1px solid lightgray;
}

#toolbar {
  border: none;
  border-bottom: 1px solid lightgray;
  box-sizing: border-box;
}

#toolbar-control .e-btn:disabled {
  opacity: 0.5 !important;
  pointer-events: none;
}

#signature-control {
  height: 300px;
  width: 100%;
  margin: 0;
}

#signature {
  border: 1px solid lightgray;
}

.e-colorpicker-wrapper.e-bg-color #bg-color+.e-split-btn-wrapper .e-split-btn .e-selected-color {
  background: none;
  border-bottom-style: solid;
  border-bottom-width: 3px;
  width: 14px;
  margin: 0px 2px;
  border-bottom-color: #ffffff;
}
.e-colorpicker-wrapper.e-stroke-color #stroke-color+.e-split-btn-wrapper .e-split-btn .e-selected-color {
  background: none;
  border-bottom-style: solid;
  border-bottom-width: 3px;
  width: 14px;
  margin: 0px 2px;
  border-bottom-color: #000000;
}

.e-colorpicker-wrapper.e-bg-color #bg-color+.e-split-btn-wrapper .e-split-btn .e-selected-color .e-split-preview,
.e-colorpicker-wrapper.e-stroke-color #stroke-color+.e-split-btn-wrapper .e-split-btn .e-selected-color .e-split-preview {
  display: none;
}

.e-colorpicker-wrapper.e-bg-color #bg-color+.e-split-btn-wrapper .e-split-btn .e-selected-color::before {
  content: '\e707';
}

.e-colorpicker-wrapper.e-stroke-color #stroke-color+.e-split-btn-wrapper .e-split-btn .e-selected-color::before {
  content: '\e704';
}

.e-clear::before {
  content: '\e706';
}

.e-save::before {
  content: '\e701';
}
.e-container .e-palette .e-circle-palette {
  border: 0;
  height: 32px;
  width: 32px;
  border-radius: 20px;
  margin: 4px;
}
.e-container .e-palette .e-circle-palette:hover {
  box-shadow: none;
  transform: scale(1.2);
  transition: transform .2s ease-out;
}
.e-circle-palette .e-circle-selection {
  height: 32px;
  width: 32px;
  border-radius: 20px;
  display: inline-block;
  transform: scale(0);
  transition: transform 1.2s ease-in;
}
.e-circle-palette.e-selected .e-circle-selection {
  transform: scale(0.8);
  background-color: #fff;
  transition: transform .2s ease-out;
}
#circle-palette+.e-container,
#scroll-palette+.e-container {
  background-color: transparent;
  border-color: transparent;
  box-shadow: none;
}