-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
141 lines (125 loc) · 4.2 KB
/
javascript.js
File metadata and controls
141 lines (125 loc) · 4.2 KB
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
const brush = document.getElementById('brush');
const eraser = document.getElementById('erase');
const reset = document.getElementById('reset');
const save = document.getElementById('saveLink');
const brushColor = document.getElementById('color');
const brushSize = document.getElementById('size');
const radioBkNone = document.getElementById('Backnone');
const radioBkColor = document.getElementById('hasBack');
const backColor = document.getElementById('bkcolor');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth - 27;
canvas.height = window.innerHeight - 120;
var mouse = false;
var positionX, positionY;
canvas.addEventListener("mousedown", brushDown, false);
canvas.addEventListener("mousemove", brushMove, false);
canvas.addEventListener("mouseup", brushUp, false);
canvas.addEventListener("touchstart", brushDown, false);
canvas.addEventListener("touchmove", brushMove, false);
canvas.addEventListener("touchend", brushUp, false);
save.addEventListener('click',saveClick);
reset.addEventListener('click',resetClick);
brush.addEventListener('click',brushClick);
eraser.addEventListener('click',eraserClick);
radioBkColor.addEventListener('click', hasBackgroundColor);
radioBkNone.addEventListener('click', hasBackgroundColor);
backColor.addEventListener('change',hasBackgroundColor)
brushClick();
function brushClick() {
brush.style.border = "2px solid #ee6c4d";
eraser.style.border = "none";
mode = "pen";
}
function eraserClick() {
eraser.style.border = "2px solid #ee6c4d";
brush.style.border = "none";
mode = "eraser"
}
function brushDown(e) {
mouse = true;
var coordinates = getCoordinates(canvas,e);
canvas.style.cursor = "pointer";
positionX = coordinates.x;
positionY = coordinates.y;
ctx.strokeStyle = brushColor.value;
ctx.lineWidth = brushSize.value;
ctx.beginPath();
ctx.moveTo(positionX,positionY);
brushDraw(canvas,positionX,positionY)
}
function brushMove(e) {
var coordinates = getCoordinates(canvas,e);
positionX = coordinates.x;
positionY = coordinates.y;
brushDraw(canvas, positionX, positionY);
}
function getCoordinates(canvas , e) {
var rect = canvas.getBoundingClientRect();
if(e.clientX){
return {
x: e.clientX - rect.left,
y: e.clientY - rect.top
}
}
else{
return {
x: e.touches[0].clientX - rect.left,
y: e.touches[0].clientY - rect.top,
}
}
}
function brushDraw(canvas, positionX, positionY) {
if(mouse){
if(mode=="pen"){
ctx.globalCompositeOperation="source-over";
ctx.strokeStyle = brushColor.value;
}
else{
if(document.querySelector('input[name="hasBackground"]:checked').value === 'true'){
ctx.globalCompositeOperation="source-over";
ctx.strokeStyle = backColor.value;
}
else{
ctx.globalCompositeOperation="destination-out";
}
}
ctx.lineWidth = brushSize.value;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.lineTo(positionX,positionY);
ctx.stroke();
canvas.style.cursor = "pointer";
}
}
function brushUp() {
mouse = false;
canvas.style.cursor = "default";
}
function hasBackgroundColor() {
var hasBackColor = document.querySelector('input[name="hasBackground"]:checked');
if(hasBackColor.value == 'true'){
backColor.disabled = false;
ctx.globalCompositeOperation="source-over";
ctx.fillStyle = backColor.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
else {
backColor.disabled = true;
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}
function saveClick() {
save.href = canvas.toDataURL();
save.download = "paintScreen.png"
}
function resetClick() {
if(document.querySelector('input[name="hasBackground"]:checked').value === 'true'){
ctx.fillStyle = backColor.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
else{
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}