-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.js
286 lines (253 loc) · 7.85 KB
/
src.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//Detail Values
var details = [
{
name:"stability",
value:2,
min:0,
max:10
}]
//Build Toolbar
var curTool = "pencil"
var tools = [
{
//0
name:"clear",
icon:"window-close",
info: "Clear Canvas",
action: function(){
ctx.clearRect(0,0,640,320);
}
},
{
//1
name:"paint-brush",
icon:"paint-brush",
info: "Thick Line",
action: function(){
curTool = this.id
ctx.lineWidth=5;
highlightTool(this)
resetTools(this)
}
},
{
//2
name:"pencil",
icon:"pencil-alt",
info: "Thin Line",
action: function(){
curTool = this.id
ctx.lineWidth=1;
highlightTool(this)
resetTools(this)
}
}//,
//{
//3
// name:"undo",
// icon:"undo",
// info: "Undo",
//action: function(){
// var tmp = canvasArray.pop()
//
// var img = new Image();
// img.onload = function() {
// ctx.drawImage(img, 0, 0);
// };
// img.src = tmp;
//}
//},
//{
//4
// name:"redo",
// icon:"redo",
// info: "redo",
//action: function(){}
//},
//{
//5
// name:"image",
// icon:"image",
// info: "Insert Image as Background",
//action: function(){}
// }
]
document.getElementById('tool').innerHTML = buildToolButtons(tools)
//toolbar tool variables
document.getElementById('clear').onclick = tools[0].action
document.getElementById('paint-brush').onclick = tools[1].action
document.getElementById('pencil').onclick = tools[2].action
//document.getElementById('undo').onclick = tools[3].action
function init(){
resetColors(document.getElementById(userColor))
highlightTool(document.getElementById(curTool))
buildDetails(details)
}
function buildToolButtons(t){
var tmp = ''
for (var i = 0; i < t.length; i++) {
if(tmp === ''){
tmp = '<button class="btn" btn title="' + t[i].info + '" id="' + t[i].name + '"><i class="fa fa-' + t[i].icon + '"></i></button>'
} else {
tmp = tmp
+ '<button class="btn" btn title="' + t[i].info + '" id="' + t[i].name + '"><i class="fa fa-' + t[i].icon + '"></i></button>'
}
}
return tmp
}
function buildDetails(d){
var tmp = ''
for (var i = 0; i < d.length; i++) {
if(tmp === ''){
tmp = d[i].name + ': <input type="number" id="' + d[i].name +'" value="' + d[i].value + '" min="' + d[i].min + '" max="' + d[i].max + '"/>'
} else {
tmp = tmp + d[i].name + ': <input type="number" id="' + d[i].name +'" value="' + d[i].value + '" min="' + d[i].min + '" max="' + d[i].max + '"/>'
}
}
document.getElementById('details').innerHTML = tmp
}
var userColor = 'white' //Default
var color = ['black', 'grey', 'white',
'blue', 'red', 'green',
'purple', 'orange','teal'] //Options
document.getElementById('color').innerHTML = colors(color)
function colors(c){
var tmp = ''
for (var i = 0; i < c.length; i++) {
if(tmp === ''){
tmp = '<button onclick="resetColors(' + c[i] + ')" style="background-color:' + c[i] + '" title="' + c[i] + '" class="colors btn" id="' + c[i] + '"></button>'
} else {
tmp = tmp
+ '<button onclick="resetColors(' + c[i] + ')" style="background-color:' + c[i] + '" title="' + c[i] + '" class="colors btn" id="' + c[i] + '"></button>'
}
}
return tmp
}
function pointer(cur){
document.getElementById('draw').style.cursor = 'url(' + cur + '),auto'
}
//End Buttons Toolbar
//Build Canvas
var click, squareStart = false
var cnt = 0
var stability = function(){return document.getElementById("stability").value}
var c = document.getElementById("draw")
var ctx = c.getContext("2d")
ctx.strokeStyle= 'white'
ctx.fillRect(0,0,640,320);
function getWidth() {
return Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
)*0.80;
}
function getHeight() {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
)*0.50;
}
document.getElementById('draw').width = 640
document.getElementById('draw').height = 320
//Mouse Events
////click -> draw line
document.body.onmousedown = function(){
ctx.beginPath()
click = true
}
////release click -> stop drawing
document.body.onmouseup = function(){
click = false
}
document.getElementById('draw').onmouseup = function(){
printCanvasToArray('draw')
}
//called from canvas -> onmousemove
//acts as a refresher based on user activity
function refresh(e) {
if(click && cnt > stability()){
draw(event)
cnt = 0
} else {
cnt++
}
}
//Draw -- General
function draw(e){
ctx.lineTo(coorX(event),coorY(event))
ctx.stroke()
}
var X, Y
var offsetX = document.getElementById('draw').offsetLeft
+ document.getElementById('canvas').offsetLeft
var offsetY = document.getElementById('canvas').offsetTop
- window.pageYOffset
function edges(){
var XLeft = coorX(event) < offsetX
var XRight = coorX(event) > (getWidth()-offsetX)
var YTop = coorY(event) < offsetY
var YBottom = coorY(event) > (getHeight()-offsetY)
return XLeft || XRight || YTop || YBottom
}
//Helper Functions`
function coorX(e){
offsetX = document.getElementById('draw').offsetLeft
+ document.getElementById('canvas').offsetLeft
return e.clientX - offsetX
}
function coorY(e){
offsetY = document.getElementById('canvas').offsetTop
- window.pageYOffset
return e.clientY - offsetY
}
function replaceName(name){
return name.split('-').join('')
}
function resetTools(t){
for (var i = 0; i < tools.length; i++) {
if(tools[i].name !== t.id){
document.getElementById(tools[i].name).style.backgroundColor = '#888888'
document.getElementById(tools[i].name).style.borderColor = '#b3b3b3'
document.getElementById(tools[i].name).style.color = 'black'
} else {
document.getElementById('draw').style.cursor = 'url('+tools[i].icon+'.cur),auto'
}
}
}
function highlightTool(t){
document.getElementById(t.id).style.color = userColor
document.getElementById(t.id).style.backgroundColor = "lightGrey"
}
function resetColors(c){
for (var i = 0; i < tools.length; i++) {
if(tools[i].name === curTool){
document.getElementById(tools[i].name).style.color = c.id
resetTools(document.getElementById(curTool))
userColor = c.id
ctx.strokeStyle = c.id
}
}
}
//Download image functions
var canvasArray = []
function printCanvasToArray(canvasId) {
canvasArray.push(document.getElementById(canvasId).toDataURL())
console.log(canvasArray.length)
}
function downloadCanvas(link, canvasId, filename) {
link.href = document.getElementById(canvasId).toDataURL();
link.download = filename;
}
document.getElementById('download').addEventListener('click', function() {
var fileName = (document.getElementById('fileName').value+'').split('.').join('-').split(' ').join('_')
if (fileName == null || fileName == "") {
fileName = "drawing_app_" + (Math.floor(Math.random() * 1000000) + 1);
}
downloadCanvas(this, 'draw', fileName);
}, false);