-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcss-shader.js
245 lines (210 loc) · 5.56 KB
/
css-shader.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
/*jshint expr:true */
/*global window:false, console:false, define:false, module:false */
/**
* @license CSSShader - A JavaScript wrapper for Custom CSS GLSL Shaders
* Copyright (c) 2012 - 2013 Jens Arps
* http://jensarps.de/
*
* Licensed under the MIT (X11) license
*/
(function (name, definition, global) {
if (typeof define === 'function') {
define(definition);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = definition();
} else {
global[name] = definition();
}
})('CSSShader', function () {
/**
* The CSSShader constructor
*
* @constructor
* @name CSSShader
*/
var CSSShader = function(){
this.boundAnimate = this.animate.bind(this);
};
CSSShader.prototype = /** @lends CSSShader */ {
/**
* Whether animation is paused
*
* @type Boolean
*/
paused: false,
/**
* The vertex shader to be applied
*
* @type String
*/
vertexShader: '',
/**
* The fragment shader to be applied
*
* @type String
*/
fragmentShader: '',
/**
* The render target for the shaders
*
* @type HTMLElement
*/
renderTarget: null,
/**
* The optional user args for the shaders
*
* @type Object
*/
shaderArgs: null,
/**
* The blend mode to use
*
* @see http://www.w3.org/TR/compositing/#blend-mode
* @type String
*/
blendMode: 'normal',
/**
* The alpha compositing mode to use
*
* @see http://www.w3.org/TR/compositing/#alpha-compositing
* @type String
*/
alphaCompositing: 'source-atop',
/**
* The vertex mesh to use. Defined as a string containing the number of
* rows, a space and the number of columns.
*
* @see http://www.w3.org/TR/filter-effects/#vertexMesh-attribute
* @type String
*/
vertexMesh: '40 40',
/**
* Takes a node by reference or it's id, and takes it's innerHTML as
* vertex shader.
*
* @param {HTMLElement|String} nodeOrId A node reference or a node id
*/
setVertexShaderByNode: function(nodeOrId){
this._setShader('vertex', this._getNode(nodeOrId).innerHTML);
},
/**
* Sets the vertex shader
*
* @param {String} shaderString
*/
setVertexShader: function(shaderString){
this._setShader('vertex', shaderString);
},
/**
* Takes a node by reference or it's id, and takes it's innerHTML as
* fragment shader.
*
* @param {HTMLElement|String} nodeOrId A node reference or a node id
*/
setFragmentShaderByNode: function(nodeOrId){
this._setShader('fragment', this._getNode(nodeOrId).innerHTML);
},
/**
* Sets the fragment shader.
*
* @param {String} shaderString
*/
setFragmentShader: function(shaderString){
this._setShader('fragment', shaderString);
},
/**
* Sets a shader.
*
* @param {String} type The shader type to set, 'vertex' or 'fragment'
* @param {String} shaderString The shader code
* @private
*/
_setShader: function(type, shaderString){
this[type + 'Shader'] = 'url(data:text/plain;charset=utf-8;base64,' + btoa(shaderString) + ')';
},
/**
* Sets the shader args. Note: will overwrite all current shader args.
*
* @param {Object} args A key-value map of shader args
*/
setShaderArgs: function(args){
this.shaderArgs = args;
},
/**
* Sets a single shader arg
*
* @param {*} id The id (key) of the shader arg
* @param {*} value The value of the shader arg
*/
setShaderArg: function(id, value){
this.shaderArgs[id] = value;
},
/**
* Takes a node by reference or id and uses it to apply the shaders to.
*
* @param {HTMLElement|String} nodeOrId A node reference or node id
*/
setRenderTarget: function(nodeOrId){
this.renderTarget = this._getNode(nodeOrId);
},
/**
* Renders the shaders on the render target.
*
* @param {Number} [time] The time arg to pass to the shaders
*/
render: function(time){
var shaderString = this.vertexShader +
' mix(' +
this.fragmentShader + ' ' +
this.blendMode + ' ' +
this.alphaCompositing +
')';
var cssString = 'custom( ' +
shaderString + ', ' +
this.vertexMesh;
this.shaderArgs || (this.shaderArgs = {});
this.shaderArgs.time = 0.1 + (time || 0);
for(var key in this.shaderArgs){
cssString += ', ' + key + ' ' + this.shaderArgs[key];
}
cssString += ')';
this.renderTarget.style.webkitFilter = cssString;
},
/**
* Uses RAF to animate the shaders on the render target.
*
* @param {Number} [time] The time arg to pass to the shaders
*/
animate: function(time){
if(this.paused){
return;
}
requestAnimationFrame(this.boundAnimate);
this.render(time);
},
/**
* Pauses an ongoing animation.
*/
pause: function(){
this.paused = true;
},
/**
* Resumes a paused animation.
*/
resume: function(){
this.paused = false;
this.animate();
},
/**
* Takes a node reference or id and returns the node.
*
* @param {HTMLElement|String} nodeOrId A node reference or node id
* @returns {HTMLElement} The node
* @private
*/
_getNode: function(nodeOrId){
return typeof nodeOrId == 'string' ? document.getElementById(nodeOrId) : nodeOrId;
}
};
return CSSShader;
}, this);