-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickboard.html
187 lines (153 loc) · 5.7 KB
/
quickboard.html
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html>
<head>
<title>Signature</title>
<style>
.main {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.canvas {
border: 2px solid black;
}
.top,
.bottom {
display: flex;
flex-direction: row;
justify-content: space-between;
margin: 20px 0 20px 0;
}
.block,
input,
select,
button {
width: 80%;
}
.column {
display: flex;
flex-direction: column;
}
.bottom>button {
margin: 10px;
}
.top>.block {
margin: 10px;
}
.block>p {
margin: 10px auto;
width: 50%;
}
</style>
<link rel="icon" href="favicon.png">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="main">
<div class="top">
<div class="block">
<p>Text color picker</p>
<input class="form-control" type="color" id="colorPicker">
</div>
<div class="block">
<p>Background</p>
<input class="form-control" type="color" id="canvasColor">
</div>
<!-- <div class="block">
<p>Thiccness slider</p>
<input type="range" class="form-control-range" id="lineWidthSlider" min="1" max="100" value="1">
</div> -->
<div class="block">
<p>Font size</p>
<select class="custom-select" type="select" id="fontSizePicker">
<option value="5">5px</option>
<option value="10">10px</option>
<option value="20">20px</option>
<option value="30">30px</option>
<option value="40">40px</option>
<option value="50">50px</option>
</select>
</div>
</div>
<canvas class="canvas" id="myCanvas" width="800" height="500"></canvas>
<div class="bottom">
<button type="button" class="btn btn-danger" id="clearButton">Clear</button>
<button type="button" class="btn btn-success" id="saveButton">Save & download</button>
<button type="button" class="btn btn-warning" id="retrieveButton">Retrieve saved signature</button>
</div>
</div>
<script>
let history = [];
const colorPicker = document.getElementById('colorPicker');
const canvasColor = document.getElementById('canvasColor');
const canvas = document.getElementById('myCanvas');
const undoButton = document.getElementById('undoButton');
const clearButton = document.getElementById('clearButton');
const saveButton = document.getElementById('saveButton');
const fontPicker = document.getElementById('fontPicker');
const textInput = document.getElementById('textInput');
const fontSizePicker = document.getElementById('fontSizePicker'); // add new element
const ctx = canvas.getContext('2d');
colorPicker.addEventListener('change', (event) => {
ctx.fillStyle = event.target.value;
ctx.strokeStyle = event.target.value;
});
canvasColor.addEventListener('change', (event) => {
ctx.fillStyle = event.target.value;
ctx.fillRect(0, 0, 800, 500);
});
canvas.addEventListener('mousedown', (event) => {
isDrawing = true;
lastX = event.offsetX;
lastY = event.offsetY;
});
canvas.addEventListener('mousemove', (event) => {
if (isDrawing) {
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(event.offsetX, event.offsetY);
ctx.stroke();
lastX = event.offsetX;
lastY = event.offsetY;
}
});
canvas.addEventListener('contextmenu', (event) => {
event.preventDefault();
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
});
fontSizePicker.addEventListener('change', (event) => {
ctx.lineWidth = event.target.value;
// ctx.font = `${fontPicker.value} ${event.target.value}px`;
});
clearButton.addEventListener('click', () => {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
})
// Add event listener for the save button
saveButton.addEventListener('click', () => {
localStorage.setItem('canvasContents', canvas.toDataURL());
// Create a new <a> element
let link = document.createElement('a');
// Set the download attribute and the href attribute of the <a> element
link.download = 'my-canvas.png';
link.href = canvas.toDataURL();
// Dispatch a click event on the <a> element
link.click();
});
// Add event listener for the retrieve button
retrieveButton.addEventListener('click', () => {
// Retrieve the saved canvas contents from local storage
let savedCanvas = localStorage.getItem('canvasContents');
if (savedCanvas) {
let img = new Image();
img.src = savedCanvas;
ctx.drawImage(img, 0, 0);
}
});
</script>
</body>
</html>