-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawr.js
264 lines (182 loc) · 6.63 KB
/
drawr.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
var COLOR;
var SIZE;
var drawr = (function (COLOR, SIZE) {
var _drawr = {};
_drawr.start = function (canvas_params/*ex: {id: "myCanvas", w: 250, h: 250}*/ ) {
var canvas_id = document.getElementById(canvas_params.id);
var context = canvas_id.getContext('2d');
/*
scaling bug fix
of course IE is
freaking out over this
// need to make default
// settings
*/
// *** set default height width ***
var canvasWidth = canvas_params.w || window.innerWidth;
var canvasHeight = canvas_params.h || window.innerHeight;
canvas_id.width = canvasWidth;
canvas_id.height = canvasHeight;
//create some utilities for draw function to use
var localPen = {};
var drawing = false;
var canvasPos = {
x: canvas_id.offsetLeft,
y: canvas_id.offsetTop
};
//touch events
canvas_id.addEventListener('touchstart', function (e) {
e.preventDefault();
if (COLOR !== undefined && SIZE !== undefined) {
console.log(COLOR + " " + SIZE);
drawing = true;
console.log(drawing);
localPen.x = e.targetTouches[0].pageX - canvasPos.x;
localPen.y = e.targetTouches[0].pageY - canvasPos.y;
} else {
console.log('no drawing properties set');
return;
}
});
canvas_id.addEventListener('touchmove', function (e) {
e.preventDefault();
var drawTo = {
x: e.targetTouches[0].pageX - canvasPos.x,
y: e.targetTouches[0].pageY - canvasPos.y
}
if (drawing) {
drawr.ctx(localPen.x, localPen.y, drawTo.x, drawTo.y, COLOR, SIZE, context);
}
localPen.x = drawTo.x;
localPen.y = drawTo.y;
});
canvas_id.addEventListener('touchend', function (e) {
e.preventDefault();
drawing = false;
});
//mouse events
canvas_id.addEventListener('mousedown', function (e) {
if (COLOR !== undefined && SIZE !== undefined) {
console.log(COLOR + " " + SIZE);
drawing = true;
console.log(drawing);
localPen.x = e.pageX - canvasPos.x;
localPen.y = e.pageY - canvasPos.y;
} else {
console.log('no drawing properties set');
return;
}
});
canvas_id.addEventListener('mousemove', function (e) {
var mouseTo = {
x: e.pageX - canvasPos.x,
y: e.pageY - canvasPos.y
}
if (drawing) {
drawr.ctx(localPen.x, localPen.y, mouseTo.x, mouseTo.y, COLOR, SIZE, context);
}
localPen.x = mouseTo.x;
localPen.y = mouseTo.y;
});
canvas_id.addEventListener('mouseup', function (e) {
drawing = false;
});
canvas_id.addEventListener('mouseleave', function (e) {
drawing = false;
});
};
//assumes that the canvases are nested
// **** Pass in object literal ****
// **** ex: {id: "canvas-wrapper", newId: "layer-x", classname: "layers"}
_drawr.newLayer = function (c){
var canvasContainer = document.getElementById(c.id);
var newCanvasLayer = document.createElement('CANVAS');
newCanvasLayer.className = c.classname || "";
newCanvasLayer.id = c.newId;
newCanvasLayer.zIndex = c.zIndex;
canvasContainer.appendChild(newCanvasLayer);
_drawr.start({id: newCanvasLayer.id, w: c.w, h: c.h});
};
_drawr.colorPicker = function(color_canvas){ //pass in object literal ex: {c_id: "myCanvas", x: 25, y: 50}
var colorCanvas = document.getElementById(color_canvas.id);
var ccCTX = colorCanvas.getContext('2d');
var p = ccCTX.getImageData(color_canvas.x, color_canvas.y, 1, 1).data;
/* Convert each r, g, b value to Hex value */
function componentToHex(c){
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
/* Take r, g, b components and combine to 1 hex value */
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
var selectedColor = rgbToHex(p[0], p[1], p[2]);
return selectedColor;
};
/*
DRAWING TOOLS:
1)Path
2)Shapes
i) rectangles - 1/28/15
ii) circles
*/
_drawr.rect = function(inputs){//{canvas: 'id', x: startx, y: starty, xx: endx, yy: endy}
var canvas = document.getElementById(inputs.canvas);
var context = canvas.getContext('2d');
var begPoint = {
x: inputs.x,
y: inputs.y
};
var endPoint = {
x: inputs.xx,
y: inputs.yy
};
var strokePoint = {
x: begPoint.x,
y: begPoint.y
};
var rectAttr = {
w: endPoint.x - begPoint.x,
h: endPoint.y - begPoint.y
};
if(endPoint.x < begPoint.x){
strokePoint.x = endPoint.x;
rectAttr.w = begPoint.x - endPoint.x;
} else if(endPoint.y < begPoint.y){
strokePoint.y = endPoint.y;
rectAttr.h = begPoint.y - endPoint.y;
}
context.beginPath();
context.lineWidth = inputs.lineWidth || 1;
context.strokeStyle = inputs.lineColor || "";
context.rect(strokePoint.x, strokePoint.y, rectAttr.w, rectAttr.h);
context.stroke();
if(inputs.fillColor){
context.fillStyle = inputs.fillColor;
context.fill();
}
};
//utils
_drawr.draw = function (c, s) {
COLOR = c;
SIZE = s;
//console.log(COLOR + " " + SIZE);
};
_drawr.ctx = function (a, b, x, y, dLineColor, dLineWidth, ctx) { //lineWidth & lineColor are optional; defaults are 1px & 'black'
var context = ctx;
context.lineJoin = 'round';
context.beginPath();
context.moveTo(a, b);
context.lineTo(x, y);
context.closePath();
context.strokeStyle = dLineColor;
context.lineWidth = dLineWidth;
context.stroke();
};
_drawr.clear = function(c) {
var canvas = document.getElementById(c.id);
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
};
return _drawr;
}(COLOR, SIZE));