-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML5Draw.js
More file actions
297 lines (244 loc) · 8.05 KB
/
HTML5Draw.js
File metadata and controls
297 lines (244 loc) · 8.05 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
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
287
288
289
290
291
292
293
294
295
296
297
/************** Default Variables ****************
* Author: Sean Po
* Only change these flags. Unless you really want to change the other stuff.
*/
var DEFAULT_ACTION_COLOR = "rgba(0,0,0,1)"; // black
var DEFAULT_ACTION_SIZE = 3; // 3px
var DEFAULT_ACTION_OPACITY = 1; // fully opaque
/************** Helper classes***************/
// This DrawAction object keeps a history of the actions that have been made;
DrawAction = function () {
var _this = this;
this.createHistory = function(){
// This is to detect a paintbucket click.
return {"paintBucket":[],
// This is to detect where the user has clicked.
"click": {
"x":[],
"y":[]
},
// Boolean to check if the user is dragging.
"drag":[],
// A number that determines the size of each stroke in pixels.
"size":[],
// color will be rgba
"color":[]
};
};
this.length = 0;
// This function gets the specific action item in the given index
this.get = function (key, index) {
if (["x","y"].indexOf(key) !== -1){
return _this.history["click"][key][index];
} else {
return _this.history[key][index];
}
}
this.history = this.createHistory();
this.setHistory = function (history){
_this.history = history;
}
this.clearHistory = function () {
this.history = createHistory();
};
this.toJSON = function () {
return JSON.stringify(_this.history);
}
this.color = DEFAULT_ACTION_COLOR;
this.prevColor = DEFAULT_ACTION_COLOR;
this.size = DEFAULT_ACTION_SIZE;
this.drag = false;
this.paintBucket = false;
this.setColor = function ( color ) {
_this.prevColor = _this.color;
_this.color = color;
};
this.setSize = function ( size ) {
_this.size = size;
};
this.setDrag = function () {
_this.drag = true;
};
this.unsetDrag = function () {
_this.drag = false;
};
this.togglePaintBucket = function () {
_this.paintBucket = _this.paintBucket;
}
this.isEmpty = function () {
// If this' x array is of length 0, then you know history is empty;
return _this.history["click"]["x"].length == 0;
}
this.length = function () {
return _this.history["click"]["x"].length;
}
this.update = function(click){
var _history = _this.history;
_history['click']['x'].push(click['x']);
_history['click']['y'].push(click['y']);
// This if statement is to get rid of the issue where when someone undo, the first dot still remains.
_history['paintBucket'].push(_this.paintBucket);
if (_this.drag && _this.length != 0) {
_history['drag'][_this.length] = true;
}
_history['drag'].push(_this.drag);
_history['size'].push(_this.size);
_history['color'].push(_this.color);
}
// This function recieves a set similar to that of history and updates the current History with it
this.updateSet = function ( set ){
var _history = _this.history;
var pushX = set['click']['x'];
var pushY = set['click']['y'];
for (index in pushX){
_history['click']['x'].push(pushX[index]);
_history['click']['y'].push(pushY[index]);
}
_history['drag'].concat(set['drag']);
_history['size'].concat(set['size']);
_history['color'].concat(set['color']);
};
}
/*********************************************/
HTML5Draw = function (dom) {
var _this = this;
this.domID = dom;
this.$canvas = $('#' + dom );
this.canvas = document.getElementById(dom);
// Set up context, and set styles:
this.context = canvas.getContext("2d");
this.context.lineJoin = "miter";
this.context.canvas.height = this.$canvas.height();
this.context.canvas.width = this.$canvas.width();
this.setLineJoin = function (type){
_this.context.lineJoin = type;
};
// This is the consolidated Action List that is recieved from server
this.actionList = [];
// This is the information caught in the Stanza
this.stanza = new DrawAction();
this.drawStanza = function (stanza){
var length = stanza.length();
for(var i = 0; i < length; i++){
// If the step is to apply the paintbucket, then the location, and drawing is uneccesary
var x = stanza.get("x", i);
var y = stanza.get("y", i);
var color = stanza.get("color", i);
if (stanza.get("paintBucket", i)){
_paintBucket(x, y, color);
} else {
_this.context.beginPath();
if(stanza.get("drag", i) && i != 0){
_this.context.moveTo(stanza.get("x", i - 1), stanza.get("y", i - 1));
} else {
_this.context.moveTo(x - 1, y);
}
_this.context.lineTo(x, y);
_this.context.closePath();
_this.context.strokeStyle = color;
_this.context.lineWidth = stanza.get("size", i);
_this.context.stroke();
}
}
};
this.redraw = function () {
for ( index in _this.actionList ) {
var stanza = _this.actionList[index];
_this.drawStanza(stanza);
}
};
this.draw = function () {
_this.drawStanza(_this.stanza);
}
this.clearCanvas = function () {
_this.actionList = [];
var height = _this.context.canvas.height;
var width = _this.context.canvas.width ;
_this.context.clearRect(0, 0, width, height);
_this.redraw();
};
/********************* Canvas Tools ***************************************/
this.toPNG = function (){
return Canvas2Image.saveAsPNG(_this.canvas, true)
};
this.toBMP = function (){
return Canvas2Image.saveAsBMP(_this.canvas, true)
};
this.toJPEG = function (){
return Canvas2Image.saveAsJPEG(_this.canvas, true)
};
/********************* Utility Functions ***********************************/
// converts an event object to
this._createAction = function (e){
var $this = _this.$canvas;
return { x : e.pageX - $this.offset().left,
y : e.pageY - $this.offset().top};
};
/********************* Actions *********************************************/
this.$canvas.mousedown( function (e) {
_this.stanza.update( _this._createAction(e) );
_this.stanza.setDrag();
_this.draw()
_this.$canvas.trigger("updateCanvas");
});
$(document).mouseup( function () {
_this.stanza.unsetDrag();
_this.actionList.push(_this.stanza);
_this.redraw();
_this.$canvas.trigger("stanzaComplete", [_this.stanza]);
// A mouseup signifies a new stanza
_this.stanza = new DrawAction();
});
this.$canvas.mousemove( function (e) {
if ( _this.stanza.drag && !_this.stanza.paintBucket ){
_this.stanza.update( _this._createAction(e) );
_this.draw();
_this.$canvas.trigger("updateCanvas");
}
});
// This is pretty fucked up code.
/*
function paint_bucket(startX, startY, curClickColor){
var pixelStack = [[startX, startY]];
curClickColor = rgbaStringToObject(curClickColor);
var colorLayer = context.getImageData(0,0,canvas_width,canvas_height);
var startColor = context.getImageData(startX,startY,1,1).data;
var startR = startColor[0];
var startG = startColor[1];
var startB = startColor[2];
var startA = startColor[3];
var newPos, x, y, pixelPos, reachLeft, reachRight;
while(pixelStack.length)
{
newPos = pixelStack.pop();
x = newPos[0];
y = newPos[1];
pixelPos = (y*canvas_width + x) * 4;
while(y-- >= 0 && matchStartColor(pixelPos, colorLayer,startR,startG,startB,startA))
{
pixelPos -= canvas_width * 4;
}
pixelPos += canvas_width * 4;
++y;
reachLeft = false;
reachRight = false;
while(y++ < canvas_height-1 && matchStartColor(pixelPos,colorLayer,startR,startG,startB,startA))
{
colorPixel(pixelPos, colorLayer, curClickColor);
if(x > 0)
{
if(matchStartColor(pixelPos - 4,colorLayer,startR,startG,startB,startA))
{
if(!reachLeft){
pixelStack.push([x - 1, y]);
reachLeft = true;
}
}
else if(reachLeft)
{
reachLeft = false;
}
}
if(x < canvas_width-1)
*/
}