Creating a standalone digital signature component in AngularJS involves several steps. The process includes setting up the AngularJS directive, integrating a signature capturing library, and ensuring the signature can be saved as a base64 string in the desired size and aspect ratios. Below is a simplified guide to achieve this:
Step 1: Set Up Your AngularJS Application
Ensure you have the AngularJS framework set up in your project. Include the necessary libraries like AngularJS and jQuery (if required).
Step 2: Choose a Signature Capture Library
For signature capturing functionality, you can use a library like Signature Pad which is a simple JavaScript library for drawing smooth signatures.
Step 3: Create the AngularJS Directive
Create an AngularJS directive to encapsulate the functionality of the digital signature component.
signaturePadDirective.js
angular.module('myApp').directive('signaturePad', function() {
return {
restrict: 'E',
scope: {
onSave: '&'
},
template: `
<div class="signature-pad-modal">
<canvas class="signature-pad-canvas"></canvas>
<button ng-click="clear()">Clear</button>
<button ng-click="save()">Save</button>
</div>
`,
link: function(scope, element) {
const canvas = element.find('canvas')[0];
const signaturePad = new SignaturePad(canvas);
scope.clear = function() {
signaturePad.clear();
};
scope.save = function() {
if (!signaturePad.isEmpty()) {
const base64Data = signaturePad.toDataURL();
scope.onSave({data: base64Data});
}
};
// Resize the canvas to desired size or aspect ratio
function resizeCanvas() {
const ratio = Math.max(window.devicePixelRatio || 1, 1);
canvas.width = canvas.offsetWidth * ratio;
canvas.height = canvas.offsetHeight * ratio;
canvas.getContext('2d').scale(ratio, ratio);
signaturePad.clear();
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
}
};
});
Step 4: Define the CSS for the Component
Design your component with necessary CSS adjustments:
styles.css
.signature-pad-modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border: 1px solid #ccc;
padding: 20px;
z-index: 1000;
}
.signature-pad-canvas {
border: 1px solid #ccc;
width: 100%;
height: 200px; /* Adjust to the required height */
}
button {
margin-top: 10px;
}
Step 5: Integrate the Directive in Your Application
Use the directive within your AngularJS application and define a controller to handle the base64 data.
app.js
angular.module('myApp', [])
.controller('MainController', function($scope) {
$scope.saveSignature = function(data) {
// Handle the base64 encoded data
console.log(data); // Send it to the server or save locally
};
});
index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body ng-controller="MainController">
<signature-pad on-save="saveSignature(data)"></signature-pad>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="signature_pad.min.js"></script>
<script src="signaturePadDirective.js"></script>
<script src="app.js"></script>
</body>
</html>
Additional Considerations
-
Responsive Design: Adjust the resizeCanvas function and CSS styling to ensure responsiveness on various devices.
-
Aspect Ratio: For specific aspect ratios, you can modify the canvas dimensions accordingly and handle resizing dynamically.
-
Saving Data: Use an appropriate method to handle the base64 data returned from the directive (e.g., sending it to a server endpoint or storing it locally).
By following these steps, you can effectively create a reusable digital signature component using AngularJS, which can be added anywhere within your application.