-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.js
133 lines (98 loc) · 2.51 KB
/
magic.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
126
127
128
129
130
131
132
var x = (function(){
//get the stuff
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var brush = document.getElementById("bsize");
var clear = document.getElementById("clear");
var erase = document.getElementById("erase");
var shade = document.getElementsByClassName("shade");
var n = shade.length;
var inc = document.getElementById("inc");
var dec = document.getElementById("dec");
var maxSize = 60;
var minSize = 5;
var size = 20;
var current = document.getElementById("bsize");
var save = document.getElementById("save");
//increase size
function increase(){
size += 5;
if(size>maxSize)
size = maxSize;
else if(size<minSize)
size = minSize;
current.innerHTML = size;
}
inc.addEventListener("click",increase);
//decrease size
function decrease(){
size -= 5;
if(size>maxSize)
size = maxSize;
else if(size<minSize)
size = minSize;
current.innerHTML = size;
}
dec.addEventListener("click",decrease);
//change color
function colorChange(e)
{
var color = e.target;
context.fillStyle = color.style.backgroundColor;
context.strokeStyle = color.style.backgroundColor;
var active = document.getElementsByClassName("active")[0];
console.log(color);
active.className = 'shade';
color.className += ' active';
}
for(var i =0;i<shade.length;i++)
{
shade[i].addEventListener("click",colorChange);
}
var press = false;
//draw line
function drawLine(e){
if(press){
context.lineTo(e.clientX,e.clientY);
context.lineWidth = 2*size;
context.stroke();
context.beginPath();
console.log(e.clientX);
context.arc(e.clientX,e.clientY,size,0,2*Math.PI);
context.fill();
context.beginPath();
console.log(e.clientX);
context.moveTo(e.clientX,e.clientY);
}
}
function up(e){
press = false;
context.beginPath();
}
function down(e){
press = true;
drawLine(e);
}
canvas.addEventListener('mouseup',up);
canvas.addEventListener('mousedown',down);
canvas.addEventListener('mousemove',drawLine);
//erase
function erasescr(){
context.fillStyle = '#fff';
context.strokeStyle = '#fff';
}
erase.addEventListener("click",erasescr);
//clear
function clearscr(){
context.clearRect(0,0,window.innerWidth,window.innerHeight);
}
clear.addEventListener("click",clearscr);
//save
function saveFile(e){
e.target.href = canvas.toDataURL();
e.target.download = "drawing.png";
}
save.addEventListener("click",saveFile);
})();