-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery_canvbg.js
43 lines (36 loc) · 1.1 KB
/
jquery_canvbg.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
(function() {
var canvbg, canvbgs, populate_style_elem, style_elem;
canvbgs = {};
style_elem = null;
populate_style_elem = function() {
/*
ensures that this element is in the head:
<style type='text/css' id='canvbg'>...</style>
and then fills it with the different "background-image" styles.
*/
var contents, data, selector;
contents = [];
for (selector in canvbgs) {
data = canvbgs[selector];
contents.push("" + selector + " { background-image:url(" + data + "); }");
}
style_elem || (style_elem = $('<style>', {
type: 'text/css',
'id': 'canvbg'
}).appendTo('head'));
return style_elem.html(contents.join(' '));
};
canvbg = function(selector, opts, canvasCallback) {
var canvas;
canvas = document.createElement('canvas');
canvas.width = opts.width;
canvas.height = opts.height;
if (canvas.getContext) {
canvasCallback.call(canvas, canvas.getContext('2d'));
canvbgs[selector] = canvas.toDataURL();
populate_style_elem();
return true;
}
};
jQuery.canvbg = canvbg;
}).call(this);