Skip to content

Commit e0b10c3

Browse files
Create Client Controller.js
Handles the logic for drawing on the canvas, capturing mouse/touch input, clearing the signature, and converting it into a Base64-encoded image for further processing.
1 parent 41c9a74 commit e0b10c3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
api.controller = function($scope) {
2+
var c = this;
3+
var canvas, ctx;
4+
var drawing = false;
5+
var lastPos = { x: 0, y: 0 };
6+
7+
// Initialize after DOM is ready
8+
c.$onInit = function() {
9+
setTimeout(function() {
10+
canvas = document.getElementById('signature-pad');
11+
if (!canvas) return;
12+
13+
// Get 2D drawing context
14+
ctx = canvas.getContext('2d');
15+
ctx.lineWidth = 2;
16+
ctx.strokeStyle = '#000';
17+
18+
// Mouse event listeners
19+
canvas.addEventListener('mousedown', startDraw);
20+
canvas.addEventListener('mousemove', draw);
21+
canvas.addEventListener('mouseup', endDraw);
22+
23+
// Touch event listeners (for mobile/tablet)
24+
canvas.addEventListener('touchstart', startDraw);
25+
canvas.addEventListener('touchmove', draw);
26+
canvas.addEventListener('touchend', endDraw);
27+
}, 200);
28+
};
29+
30+
// Get drawing position based on mouse or touch input
31+
function getPosition(event) {
32+
var rect = canvas.getBoundingClientRect();
33+
if (event.touches && event.touches[0]) {
34+
return {
35+
x: event.touches[0].clientX - rect.left,
36+
y: event.touches[0].clientY - rect.top
37+
};
38+
}
39+
return {
40+
x: event.clientX - rect.left,
41+
y: event.clientY - rect.top
42+
};
43+
}
44+
45+
// Start drawing when mouse/touch pressed
46+
function startDraw(e) {
47+
drawing = true;
48+
lastPos = getPosition(e);
49+
}
50+
51+
// Draw line on canvas while dragging
52+
function draw(e) {
53+
if (!drawing) return;
54+
e.preventDefault(); // Prevent page scrolling on touch
55+
var pos = getPosition(e);
56+
ctx.beginPath();
57+
ctx.moveTo(lastPos.x, lastPos.y);
58+
ctx.lineTo(pos.x, pos.y);
59+
ctx.stroke();
60+
lastPos = pos;
61+
}
62+
63+
// Stop drawing when mouse/touch released
64+
function endDraw() {
65+
drawing = false;
66+
}
67+
68+
// Clear the canvas
69+
c.clearSignature = function() {
70+
if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
71+
drawing = false;
72+
};
73+
74+
// Convert signature to base64 image and attach
75+
c.attachSignature = function() {
76+
if (!ctx) return alert("Canvas not initialized.");
77+
var data = canvas.toDataURL('image/png'); // Get base64 encoded image
78+
alert("Signature captured successfully. It will be attached after submission.\n\n" + data);
79+
};
80+
};

0 commit comments

Comments
 (0)