/
playground.js
332 lines (263 loc) · 8.47 KB
/
playground.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
// --------------------------------------------------
//
// playground.js, v0.2
// supporting script for PaintbrushJS playground demo
//
// Developed by Dave Shea, http://www.mezzoblue.com/
//
// This project lives on GitHub:
// http://github.com/mezzoblue/PaintbrushJS
//
// Except where otherwise noted, PaintbrushJS is licensed under the MIT License:
// http://www.opensource.org/licenses/mit-license.php
//
// --------------------------------------------------
// filter master control object
var filterControl = {
"filters" : [
{
"name" : "blur",
"label" : "Gaussian Blur",
"filterClass" : "filter-blur",
"controlId" : "controls-blur"
},
{
"name" : "greyscale",
"label" : "Greyscale",
"filterClass" : "filter-greyscale",
"controlId" : "controls-greyscale"
},
{
"name" : "mosaic",
"label" : "Mosaic",
"filterClass" : "filter-mosaic",
"controlId" : "controls-mosaic"
},
{
"name" : "noise",
"label" : "Noise",
"filterClass" : "filter-noise",
"controlId" : "controls-noise"
},
{
"name" : "posterize",
"label" : "Posterize",
"filterClass" : "filter-posterize",
"controlId" : "controls-posterize"
},
{
"name" : "sepia",
"label" : "Sepia Tone",
"filterClass" : "filter-sepia",
"controlId" : "controls-sepia"
},
{
"name" : "tint",
"label" : "Colour Tint",
"filterClass" : "filter-tint",
"controlId" : "controls-tint"
}
]
}
// filter selection events
addLoadEvent(function() {
// get the main image
var img = getFilterTarget();
// get the filter chooser
var filterSelector = getFilterSelector();
// populate the select list with defined filters
for (var i = 0; i < filterControl.filters.length; i++) {
// create the new option element
newOption = document.createElement("option");
newOption.text = filterControl.filters[i].label;
newOption.value = filterControl.filters[i].name;
// add it to the list
filterSelector.appendChild(newOption);
}
// add an event handler to swap control panels
filterSelector.onchange = function() {
removeClasses(img);
flushDataAttributes(img);
destroyStash(img, true);
// adjust visible control panel
displayControls();
updateFilters(img);
}
// initialize the control panel
displayControls();
updateFilters(img);
});
// thumbnail / photo swapping
addLoadEvent(function() {
// get the main image
var img = getFilterTarget();
// get every element with the specified thumb class
var thumbParents = getElementsByClassName("thumb");
// walk through each thumbnail and attach an event handler
for (var i = 0; i < thumbParents.length; i++) {
var thumbs = thumbParents[i].getElementsByTagName("img");
for (var j = 0; j < thumbs.length; j++) {
// swap the main image with the thumbnail source on click
thumbs[j].onclick = function() {
// clear classes on parent li elements
var parentLi = this.parentNode;
var parentUl = parentLi.parentNode;
var lis = parentUl.getElementsByTagName("li");
for (var k = 0; k < lis.length; k++) {
removeClass(lis[k], "current");
}
// add a "current" class to highlight current thumbnail
addClass(parentLi, "current");
// clean up old image
removeClasses(img);
destroyStash(img);
img.src = this.getAttribute("src");
// re-apply filters
displayControls();
updateFilters(img);
}
}
}
});
// add events to update the image upon various interactions
addLoadEvent(function() {
// get our two main interaction objects, the form and the image
var thisForm = getInteractionForm();
var interaction = getInteractionTarget();
var img = getFilterTarget();
// initial filter application
updateFilters(img);
// when the form is submitted...
thisForm.onsubmit = function() {
updateFilters(img);
return false;
};
// also add a hook for range sliders
inputs = interaction.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type.toLowerCase() == "range") {
// kills performance if we're calling it onchange.
// but inaccessible / irrelevant to mobile if we do it onmouseup.
// damned if we do... I'm favouring speed here, would love input.
inputs[i].onmouseup = function() {
updateFilters(img);
}
}
// fallback for radio buttons, colour input
// (and browsers that don't do range yet, hint hint Firefox)
if (inputs[i].type.toLowerCase() == ("text" || "radio")) {
inputs[i].onchange = function() {
updateFilters(img);
}
}
}
});
// refresh filters after various actions have been performed
function updateFilters(img) {
// first, does the reference object have an original image in the DOM?
var classList = (img.className.toLowerCase()).split(' ');
for (var i = 0; i < classList.length; i++) {
// quick reference
var currentClass = classList[i];
// okay, we're good, there's an original
if (currentClass.substr(0, 7) == "pb-ref-") {
// clear reference object's data-pb-* attributes
flushDataAttributes(img);
// get the filter chooser
var filterSelector = getFilterSelector();
// go fetch the original and update the reference object source
var original = document.getElementById("pb-original-" + currentClass.substr(7, currentClass.length - 7));
placeReferenceImage(img, original.src, img);
img.style.visibility = "hidden";
// find the currently-selected filter
var options = filterSelector.getElementsByTagName("option");
var obj = getCurrentControl(options);
if (obj) {
// get all inputs in the form
currentForm = document.getElementById(obj.controlId);
inputs = currentForm.getElementsByTagName("input");
// loop through the inputs, grabbing the names and values and
// setting them as attributes on the reference object
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].type == ("range" || "text")) addAttribute(img, inputs[j].name, inputs[j].value);
if ((inputs[j].type == "radio")) {
if (inputs[j].checked) {
addAttribute(img, inputs[j].name, inputs[j].value);
}
}
}
}
}
}
// finally, apply the filters
processFilters();
img.style.visibility = "visible";
}
// update to current filter controls when switching filters
function displayControls() {
// get the main image
var img = getFilterTarget();
// get the filter chooser
var filterSelector = getFilterSelector();
// set up the control panels
hideControls();
showCurrentControl(filterSelector);
flushDataAttributes(img);
removeClasses(img);
// find the currently-selected filter
var options = filterSelector.getElementsByTagName("option");
var obj = getCurrentControl(options);
if (obj) {
// if we've got a match, display this current filter's control panel
addClass(img, obj.filterClass);
}
// kinda weird that I need to apply this inside and outside the filter. ideally it'd only happen here.
// Haven't quite tracked down why the 2x application is necessary yet.
updateFilters(img);
}
// hide all control panels
function hideControls() {
var controls = getElementsByClassName("controls");
for (var i = 0; i < controls.length; i++) {
controls[i].style.display = "none";
}
}
// show currently-selected control panel
function showCurrentControl(filterSelector) {
// find the currently-selected filter
var options = filterSelector.getElementsByTagName("option");
var obj = getCurrentControl(options);
if (obj) {
// if we've got a match, display this current filter's control panel
var current = document.getElementById(obj.controlId);
current.style.display = "block";
}
}
// helper functions to grab the needed DOM elements
function getFilterSelector() {
return document.getElementById("filter-selector");
}
function getFilterTarget() {
return document.getElementById("filter-target");
}
function getInteractionForm() {
return document.getElementById("interaction-form");
}
function getInteractionTarget() {
return document.getElementById("interaction");
}
// helper function to match the currently-selected option in the select list to a filter control
function getCurrentControl(options) {
// first, loop through the list of select options
for (var i = 0; i < options.length; i++) {
// when we find the currently-selected one, loop through the filter control object
if (options[i].selected) {
for (var j = 0; j < filterControl.filters.length; j++) {
// if we can match the selected option to a filter, return the filter
if (filterControl.filters[j].name == options[i].value) {
return(filterControl.filters[j]);
}
}
}
}
}