-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (196 loc) · 5.42 KB
/
script.js
File metadata and controls
225 lines (196 loc) · 5.42 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext("2d");
// wait for the content of the window element
// to load, then performs the operations.
// This is considered best practice.
window.addEventListener('load', ()=>{
resize(); // Resizes the canvas once the window loads
brush();
color(initColorObj);
getWidth(initLnWidthObj);
document.addEventListener('mousedown', startPainting);
document.addEventListener('mouseup', stopPainting);
document.addEventListener('mousemove', sketch);
//window.addEventListener('resize', resize);
});
let strStyle = "black";
let lnWidth = 1;
let isEraser = false;
function color(obj) {
if (!isEraser) {
ctx.strokeStyle = obj.id;
strStyle = obj.id;
changeSelectedElement(obj.id, "colorID");
}
}
function getWidth(obj) {
if (!isEraser) {
ctx.lineWidth = obj.id;
lnWidth = obj.id;
ctx.strokeStyle = strStyle;
changeSelectedElement(obj.id, "lineID");
}
}
function erase() {
var m = confirm("Want to clear canvas?");
if (m) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}
function download() {
var dt = canvas.toDataURL('image/jpg');
//dt = dt.replace(/^data:image\/[^;]*/, 'data:application/octet-stream');
//dt = dt.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=Canvas.png');
this.href = dt;
};
downloadLnk.addEventListener('click', download, false);
function eraser() {
ctx.strokeStyle = "white";
ctx.lineWidth = 15;
isEraser = true;
shape = false;
changeSelectedElement("eraser", "drawID");
canvas.style.cursor = "url('./images/eraser.cur'), auto";
}
//Resizes the canvas to the available size of the window.
function resize(){
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}
// Stores the initial position of the cursor
let coord = {x:0 , y:0};
// This is the flag that we are going to use to
// trigger drawing
let paint = false;
let shape = false;
var shapeType = "rectangle";
var x, y;
let drawID = "brush", lineID = "1", colorID = "black", old;
var buttonID = "drawID";
var initColorObj = { id: "black" };
var initLnWidthObj = { id:"1" };
function changeSelectedElement(newID, buttonID) {
switch(buttonID) {
case 'drawID':
old = drawID;
drawID = newID;
break;
case 'lineID':
old = lineID;
lineID = newID;
break;
case 'colorID':
old = colorID;
colorID = newID;
break;
}
document.getElementById(old).style.border = "1px solid black";
document.getElementById(newID).style.border = "double black 5px";
}
function isShape(obj) {
shapeType = obj.id;
shape = true;
leaveEraser();
changeSelectedElement(obj.id, "drawID");
canvas.style.cursor = "default";
}
function brush() {
shape = false;
leaveEraser();
changeSelectedElement("brush", "drawID");
canvas.style.cursor = "url('./images/brush-cursor.cur'), auto";
}
function leaveEraser() {
if (isEraser) {
isEraser = false;
ctx.strokeStyle = strStyle;
ctx.lineWidth = lnWidth;
canvas.style.cursor = "default";
}
}
// Updates the coordianates of the cursor when
// an event e is triggered to the coordinates where
// the said event is triggered.
// getBoundingClientRect() is used to negate bad effects from scrolling
function getPosition(event){
coord.x = event.clientX - canvas.getBoundingClientRect().left;
coord.y = event.clientY - canvas.getBoundingClientRect().top;
}
// The following functions toggle the flag to start
// and stop drawing
function startPainting(event){
getPosition(event);
if (shape) {
canvas.style.cursor = "crosshair";
}
else {
paint = true;
ctx.beginPath();
ctx.fillStyle = ctx.strokeStyle;
//ctx.fillRect(coord.x, coord.y, ctx.lineWidth, ctx.lineWidth);
ctx.arc(coord.x, coord.y, ctx.lineWidth / 2, 0, 2 * Math.PI);
ctx.fill();
}
}
function stopPainting(){
if (shape) {
x = coord.x;
y = coord.y;
getPosition(event);
ctx.beginPath();
switch (shapeType) {
case "rectangle":
ctx.strokeRect(x, y, coord.x - x, coord.y - y);
break;
case "circle":
ctx.arc(x + (coord.x - x) / 2, y + (coord.y - y) / 2, (coord.x - x) / 2, 0, Math.PI * 2);
ctx.stroke();
break;
case "star":
drawStar(x + (coord.x - x) / 2, y + (coord.y - y) / 2, 5, (coord.x - x) / 2, (coord.x - x) / 4);
break;
}
canvas.style.cursor = "default";
}
else {
paint = false;
}
}
function drawStar(cx,cy,spikes,outerRadius,innerRadius){
var rot=Math.PI/2*3;
var x=cx;
var y=cy;
var step=Math.PI/spikes;
ctx.beginPath();
ctx.moveTo(cx,cy-outerRadius)
for(i=0;i<spikes;i++){
x=cx+Math.cos(rot)*outerRadius;
y=cy+Math.sin(rot)*outerRadius;
ctx.lineTo(x,y)
rot+=step
x=cx+Math.cos(rot)*innerRadius;
y=cy+Math.sin(rot)*innerRadius;
ctx.lineTo(x,y)
rot+=step
}
ctx.lineTo(cx,cy-outerRadius);
ctx.closePath();
ctx.stroke();
}
function sketch(event){
if (shape || !paint) return;
ctx.beginPath();
ctx.lineCap = 'round';
// The cursor to start drawing
// moves to this coordinate
ctx.moveTo(coord.x, coord.y);
// The position of the cursor
// gets updated as we move the
// mouse around.
getPosition(event);
// A line is traced from start
// coordinate to this coordinate
ctx.lineTo(coord.x , coord.y);
// Draws the line.
ctx.stroke();
}