/
jquery-processing.js
169 lines (129 loc) · 3.44 KB
/
jquery-processing.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
/*!
* jQuery Processing - A tiny Processing.js utility build on jQuery.
*
* @dependency jQuery v1.7+
* @dependency Processing.js v1.4+
* @author Uniba Inc.
* @license MIT License.
*/
/**
* Check processing existance.
*/
if (!window.Processing) {
throw new Error("Processing is not found.");
}
/**
* Disable processing auto-load mechanism.
*/
Processing.disableInit();
/**
* Plugin.
*/
(function($, Processing) {
/**
* No operation.
*/
var noop = function() {};
/**
* Plugin implementation.
*
* @param {Object} options
* @return {Array}
*/
var plugin = function(options) {
var options = options || {};
/**
* Process each canvas.
*/
return $(this).each(function() {
var self = this
, deferred = $.Deferred()
, $this = $(this)
, path = getSketchFilePath(this);
/**
* Store deferred object to data attr.
*
* $('canvas').data('p5-deferred')
* .fail(function(err) { console.log('fail callback'); })
* .done(function(sketch, insatnce) { console.log('done callback'); });
*/
$this.data('p5-deferred', deferred);
ajax(path, function(err, source) {
var sketch, instance;
/**
* Exit and reject deferred on error.
*/
if (err) {
return deferred.reject(err);
}
/**
* Compile and generate Sketch instance.
*/
sketch = new Processing.compile(source);
/**
* Set callbacks.
*/
sketch.onSetup = $.proxy(options.onSetup || noop, self);
sketch.onLoop = $.proxy(options.onLoop || noop, self);
sketch.onExit = $.proxy(options.onExit || noop, self);
sketch.onLoad = $.proxy(options.onLoad || noop, self);
sketch.onPause = $.proxy(options.onPause || noop, self);
sketch.onFrameStart = $.proxy(options.onFrameStart || noop, self);
sketch.onFrameEnd = $.proxy(options.onFrameEnd || noop, self);
/**
* Start rendering.
*/
instance = new Processing(self, sketch);
/**
* Resolve deferred.
*/
deferred.resolve(sketch, instance);
/**
* Store sketch and instance to data attr.
*/
$this.data('p5-sketch', sketch).data('p5-instance', instance);
});
});
};
/**
* Set plugin version.
*/
plugin.version = '0.1.0';
/**
* Register as jQuery plugin.
*/
$.fn.processing = plugin;
/**
* Detect sketch file path from given canvas element.
*
* @param {HTMLCanvas} canvas
* @param
*/
function getSketchFilePath(canvas) {
return canvas.getAttribute('data-processing-sources')
|| canvas.getAttribute('data-src')
|| canvas.getAttribute('datasrc');
}
/**
* Load file using xhr.
*
* @param {String} url
* @param {Function} callback
*/
function ajax(url, callback) {
var err
, xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (4 === xhr.readyState) {
if (xhr.status !== 200 && xhr.status !== 0) {
err = new Error('Invalid xhr status: ' + xhr.status);
}
callback(err, xhr.responseText);
}
};
xhr.open('GET', url, true);
xhr.overrideMimeType && xhr.overrideMimeType('text/plain');
xhr.setRequestHeader('If-Modified-Since', 'Fri, 01 Jan 1960 00:00:00 GMT');
xhr.send(null);
}
})(jQuery, Processing);