-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaintbox.js
395 lines (344 loc) · 15.1 KB
/
paintbox.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* jquery-paintbox - jQuery Plugin to build out a grid-based coloring system
* similar to MS Paint.
* URL: http://pstrinkle.github.com/jquery-paintbox
* Author: Patrick Trinkle <https://github.com/pstrinkle>
* Version: 0.1.1
* Copyright 2016 Patrick Trinkle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function ($) {
function PaintBox(config) {
this.init(config);
}
PaintBox.prototype = {
constructor: PaintBox,
init: function(config) {
$.extend(this, config);
},
el: null,
elId: '',
interactive: true,
penColor: 'black',
penDown: false,
offColor: 'white',
cols: 50,
rows: 50,
cell: 10,
colors : ['black', 'red', 'green', 'blue', 'white', 'yellow', 'pink'],
}
/**
* Set up a paintbox.
*
* @param configOrCommand - Config object or command name
* Example: { ... };
* you may set any public property (see above);
*
* @param commandArgument - Some commands (like 'increment') may require an
* argument
*/
$.fn.paintbox = function(configOrCommand, commandArgument) {
var dataName = 'paintbox';
var paintFill = function(instance, i, j, off, color) {
/* given some point, recursively crawl outward filling until a
* border is hit.
*/
var eid = instance.elId;
var id = eid + '_' + i + ',' + j;
var cell = document.getElementById(id);
var current = $(cell).css('background-color');
/* XXX: support instance.off */
if (current != off) {
/* I'm not sure I should break out on this condition. */
return;
} else {
$(cell).css('background-color', color);
}
/* go up. */
if (i > 0) {
paintFill(instance, i-1, j, off, color);
}
/* go down. */
if (i < instance.rows) {
paintFill(instance, i+1, j, off, color);
}
/* go left. */
if (j > 0) {
paintFill(instance, i, j-1, off, color);
}
/* go right. */
if (j < instance.cols) {
paintFill(instance, i, j+1, off, color);
}
};
var buildIt = function(instance) {
var el = instance.el;
var baseId = instance.elId;
var clicked = function(event) {
/* click's this is the individual element */
$(this).css('background-color', instance.penColor);
};
var colorSelect = function(event) {
instance.penColor = $(this).data('color');
};
el.append($('<div>', {id: baseId + 'pen_box', style: 'clear:both;'}));
el.append($('<div>', {id: baseId + 'master_box', style: 'clear:both;'}));
var mId = '#' + baseId + 'master_box';
var $dest = $(mId);
var $pens = $('#' + baseId + 'pen_box');
var i = 0, j = 0;
var itm = 'box';
/* fixup grid container. */
var master_width = instance.cols * instance.cell;
var master_height = instance.rows * instance.cell;
$dest.css('width', master_width + 'px');
$dest.css('height', master_height + 'px');
var dim = instance.cell + 'px';
/* build grid */
for (i = 0; i < instance.rows; i++) {
for (j = 0; j < instance.cols; j++) {
var $n = $('<div>');
$n.css('width', dim);
$n.css('height', dim);
$n.css('background-color', instance.offColor);
$n.css('float', 'left');
$n.css('display', 'inline');
$n.attr('id', baseId + '_' + i + ',' + j);
if (instance.interactive) {
$n.on('click', clicked);
}
$n.addClass(itm);
$dest.append($n);
}
}
if (instance.interactive) {
/* this won't work with multiple paintbox's on the same page. */
$(mId + ' .box').hover(function(event) {
/* if pen is selected, and mousedown has been activated,
* then we draw, otherwise we temporarily hover.
*/
$(this).css('border', '1px solid ' + instance.penColor);
if (instance.penDown) {
$(this).css('background-color', instance.penColor);
}
}, function(event) {
/* if pen is selected, and mousedown has been activated,
* don't undo things.
*/
$(this).css('border', 'none');
});
/* Create the pen boxes. */
if (instance.colors.length == 0) {
throw Error("No color palette specified!");
}
/* Set default starting color to one of the provided (or
* default)
*/
instance.penColor = instance.colors[0];
/* create pen boxes. */
$.each(instance.colors, function(index, element) {
var $color = $('<div>', {id: 'color_' + index, style: "float:left"});
$color.data('color', element);
$pens.append($color);
});
/* just do this next. */
$.each($pens.children(), function(index, element) {
$(element).css('width', '10px');
$(element).css('height', '10px');
$(element).css('float', 'left');
$(element).css('display', 'inline');
/* Seems silly but originally we had spacer cells that didn't
* have IDs.
*/
var id = $(element).attr('id');
if (id) {
var color = $(element).data('color');
$(element).css('background-color', color);
$(element).on('click', colorSelect);
if (id === 'black') {
$(element).css('border', '1px solid white');
} else {
$(element).css('border', '1px solid black');
}
}
});
$dest.on('mousedown', function(event) {
instance.penDown = true;
});
$dest.on('mouseup', function(event) {
instance.penDown = false;
});
$dest.on('mouseleave', function(event) {
instance.penDown = false;
});
} /* end interactive only */
}; /* end buildIt() */
if (typeof configOrCommand == 'string') {
if (configOrCommand === 'cell') {
/* you want to update this here in case they call it a lot. */
return this.each(function() {
var instance = $(this).data(dataName);
var eid = instance.elId;
var i = commandArgument.i;
var j = commandArgument.j;
if (i < 0 || i >= instance.rows) {
throw Error("Invalid row: " + i);
}
if (j < 0 || j >= instance.cols) {
throw Error("Invalid column: " + j);
}
var id = eid + '_' + i + ',' + j;
var cell = document.getElementById(id);
$(cell).css('background-color', commandArgument.color);
});
} else if (configOrCommand === 'line') {
/* you want to update this here in case they call it a lot. */
return this.each(function() {
var instance = $(this).data(dataName);
var eid = instance.elId;
var i = commandArgument.i;
var j = commandArgument.j;
var dir = commandArgument.direction;
var len = commandArgument.length;
/* XXX: could handle line with rect. ;) */
if (i < 0 || i >= instance.rows) {
throw Error("Invalid row: " + i);
}
if (j < 0 || j >= instance.cols) {
throw Error("Invalid column: " + j);
}
if (dir === 'left' && j - (len-1) < 0) {
throw Error("Invalid length: " + len);
} else if (dir === 'right' && j + (len-1) >= instance.cols) {
throw Error("Invalid length: " + len);
} else if (dir === 'up' && i - (len-1) < 0) {
throw Error("Invalid length: " + len);
} else if (dir === 'down' && i + (len-1) >= instance.rows) {
throw Error("Invalid length: " + len);
}
if (dir === 'left' || dir === 'right') {
var start = 0;
var b = 0;
if (dir === 'left') {
start = j - len+1;
} else {
start = j;
}
j = start;
for (b = 0; b < len; b++) {
var this_j = j + b;
var id = eid + '_' + i + ',' + this_j;
var cell = document.getElementById(id);
$(cell).css('background-color', commandArgument.color);
}
} else if (dir === 'up' || dir === 'down') {
var start = 0;
var b = 0;
if (dir === 'up') {
start = i - len+1;
} else {
start = i;
}
i = start;
for (b = 0; b < len; b++) {
var this_i = i + b;
var id = eid + '_' + this_i + ',' + j;
var cell = document.getElementById(id);
$(cell).css('background-color', commandArgument.color);
}
} else {
throw Error("Invalid direction: " + dir);
}
var id = eid + '_' + i + ',' + j;
var cell = document.getElementById(id);
$(cell).css('background-color', commandArgument.color);
});
} else if (configOrCommand === 'rect') {
/* you want to update this here in case they call it a lot. */
return this.each(function() {
var instance = $(this).data(dataName);
var eid = instance.elId;
var i1 = commandArgument.i;
var j1 = commandArgument.j;
var i2 = commandArgument.i2;
var j2 = commandArgument.j2;
if (i1 < 0 || i1 >= instance.rows) {
throw Error("Invalid row: " + i1);
}
if (j1 < 0 || j1 >= instance.cols) {
throw Error("Invalid column: " + j1);
}
if (i2 < 0 || i2 >= instance.rows) {
throw Error("Invalid row: " + i2);
}
if (j2 < 0 || j2 >= instance.cols) {
throw Error("Invalid column: " + j2);
}
/* find upper left point. */
var x0 = (i1 > i2) ? i2 : i1;
var y0 = (j1 > j2) ? j2 : j1;
/* find lower right point. */
var x1 = (i1 > i2) ? i1 : i2;
var y1 = (j1 > j2) ? j1 : j2;
for (i = x0; i <= x1; i++) {
for (j = y0; j <= y1; j++) {
var id = eid + '_' + i + ',' + j;
var cell = document.getElementById(id);
$(cell).css('background-color', commandArgument.color);
}
}
});
} else if (configOrCommand === 'fill') {
/* you want to update this here in case they call it a lot. */
return this.each(function() {
var instance = $(this).data(dataName);
var eid = instance.elId;
var i = commandArgument.i;
var j = commandArgument.j;
/* Really I think it'll fill any color with another, so
* you could click on a blue thing and turn it and all
* other connected blue area.
*/
if (i < 0 || i >= instance.rows) {
throw Error("Invalid row: " + i);
}
if (j < 0 || j >= instance.cols) {
throw Error("Invalid column: " + j);
}
var id = eid + '_' + i + ',' + j;
var cell = document.getElementById(id);
var current = $(cell).css('background-color');
paintFill(instance, i, j, current, commandArgument.color);
});
}
}
return this.each(function() {
var el = $(this), instance = el.data(dataName),
config = $.isPlainObject(configOrCommand) ? configOrCommand : {};
if (instance) {
instance.init(config);
buildIt(instance);
} else {
var initialConfig = $.extend({}, el.data());
config = $.extend(initialConfig, config);
config.el = el;
config.elId = el.attr('id');
// throw exception if no ID is set for this.
instance = new PaintBox(config);
el.data(dataName, instance);
buildIt(instance);
}
});
}
}(jQuery));