-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.js
122 lines (95 loc) · 3.54 KB
/
drawing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const LINE_COLOUR = '#FFFFFF';
const LINE_WIDTH = 10;
var currentX = 0;
var currentY = 0;
var previousX = 0;
var previousY = 0;
var canvas;
var context;
function prepareCanvas() {
canvas = document.getElementById('my-canvas');
context = canvas.getContext('2d');
// Set canvas width and height based on CSS properties
canvas.width = parseInt(getComputedStyle(canvas).width, 10);
canvas.height = parseInt(getComputedStyle(canvas).height, 10);
context.strokeStyle = LINE_COLOUR;
context.lineWidth = LINE_WIDTH;
context.lineJoin = 'round';
var isPainting = false;
function startDrawing(event) {
isPainting = true;
currentX = event.clientX - canvas.getBoundingClientRect().left;
currentY = event.clientY - canvas.getBoundingClientRect().top;
}
function continueDrawing(event) {
if (isPainting) {
previousX = currentX;
currentX = event.clientX - canvas.getBoundingClientRect().left;
previousY = currentY;
currentY = event.clientY - canvas.getBoundingClientRect().top;
draw();
}
}
function stopDrawing() {
isPainting = false;
}
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', continueDrawing);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseleave', stopDrawing);
// Touch Events
canvas.addEventListener('touchstart', function (event) {
startDrawing(event.touches[0]);
});
canvas.addEventListener('touchmove', function (event) {
continueDrawing(event.touches[0]);
});
canvas.addEventListener('touchend', stopDrawing);
canvas.addEventListener('touchcancel', stopDrawing);
}
function draw() {
context.beginPath();
context.moveTo(previousX, previousY);
context.lineTo(currentX, currentY);
context.closePath();
context.stroke();
}
function clearCanvas() {
currentX = 0;
currentY = 0;
previousX = 0;
previousY = 0;
// Clear only the canvas without affecting the CSS background
context.clearRect(0, 0, canvas.width, canvas.height);
}
prepareCanvas()
// // Call prepareCanvas() to initialize the canvas
// function captureElementAndDrawOnCanvas(elementId, canvasId) {
// // Get the HTML element to capture
// const elementToCapture = document.getElementById(elementId);
// // Create an img element to store the captured image
// const imgElement = document.createElement('img');
// // Capture the element's content as a base64-encoded image
// const base64Image = getBase64Image(elementToCapture);
// // Set the src attribute of the img element to the captured image
// imgElement.src = base64Image;
// // Get the canvas element
// const canvas = document.getElementById(canvasId);
// const context = canvas.getContext('2d');
// // Draw the captured image onto the canvas
// imgElement.onload = function () {
// context.drawImage(imgElement, 0, 0);
// };
// }
// function getBase64Image(element) {
// // Create a new canvas element to temporarily draw the content
// const canvas = document.createElement('canvas');
// const context = canvas.getContext('2d');
// // Set the canvas dimensions to match the element
// canvas.width = element.clientWidth;
// canvas.height = element.clientHeight;
// // Draw the element's content onto the canvas
// context.drawImage(element, 0, 0);
// // Return the base64-encoded image data
// return canvas.toDataURL('image/png');
// }