-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
125 lines (102 loc) · 2.91 KB
/
script.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
123
124
125
//Canvas variables
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
//Store current and previous mouse position
var mouse = {
x: 0,
y: 0
};
var last_mouse = {
x: 0,
y: 0
};
//Image element
var outputcanvas = document.getElementById('canvasout');
//Store current value of pen color and width
var pencol = document.getElementById('pen-color');
var penwid = document.getElementById('pen-width');
//Variable to check mouse hover
var drawup = false;
//Set pen values
var drawcolor = 'black';
var drawwidth = penwid.value;
//Check for change in pen color and width
pencol.addEventListener('change', fcolor);
penwid.addEventListener('change', fwidth);
//Execute functions when respective buttons are clicked
document.getElementById('xsave').addEventListener("click", fsave);
document.getElementById('xclear').addEventListener("click", fclear);
document.getElementById('xdownload').addEventListener("click", fdown);
//Check for movement of mouse over the canvas
canvas.addEventListener('mousemove', function (e) {
last_mouse.x = mouse.x;
last_mouse.y = mouse.y;
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
draw('move');
}, false);
//Check for click to draw
canvas.addEventListener('mousedown', function (e) {
draw('down');
}, false);
//Check for release of click to stop drawing
canvas.addEventListener('mouseup', function (e) {
draw('up');
}, false);
//Check for mouse outside the canvas
canvas.addEventListener('mouseout', function (e) {
draw('up');
}, false);
//Draw on Canvas if clicked
function draw(a) {
if (a == 'down') {
drawup = true;
}
if (a == 'up') {
drawup = false;
}
if (drawup) {
//Start
ctx.beginPath();
///From previous point to current point
ctx.moveTo(last_mouse.x, last_mouse.y);
ctx.lineTo(mouse.x, mouse.y);
//In selected color and width
ctx.strokeStyle = drawcolor;
ctx.lineWidth = drawwidth;
//Draw it
ctx.stroke();
//Stop
ctx.closePath();
}
}
//Set color
function fcolor() {
drawcolor = pencol.value;
}
//Set width
function fwidth() {
drawwidth = penwid.value;
}
//Save
function fsave() {
var dataURL = canvas.toDataURL(); //Take canvas data and change it to base64 image hex-code
outputcanvas.src = dataURL; //Set canvas image element's source to the image code from canvas
outputcanvas.style.display = 'inline'; //Display the image by converting the code back to an image
}
//Clear after confirmation
function fclear() {
var flag = confirm("Clear Everything?");
if (flag) {
ctx.clearRect(0, 0, w, h);
outputcanvas.style.display = 'none';
}
}
//Download
function fdown() {
var button = document.getElementById('xdownload');
var dataURL = canvas.toDataURL('image/png');
button.href = dataURL;
}