forked from desandro/close-pixelate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvasFromImage.js
executable file
·80 lines (67 loc) · 2.72 KB
/
canvasFromImage.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
var canvasFromImage = (function ( global, document, undefined )
{
var hasSameOrigin = (function ( global, document ) {
var page = document.location,
protocol = page.protocol,
domain = document.domain,
port = page.port ? ':' + page.port : '',
sop_string = protocol + '//' + domain + port,
sop_regex = new RegExp('^' + sop_string),
http_regex = /^http(?:s*)/,
data_regex = /^data:/,
closure = function ( url )
{
var is_local = (!http_regex.test(url)) || data_regex.test(url),
is_same_origin = sop_regex.test(url);
return is_local || is_same_origin;
};
return closure;
})( global, document );
function getRemoteImageData( img_url, callback )
{
var page_url = document.location.href,
secure_root = "https://img-to-json.appspot.com/",
insecure_root = "http://img-to-json.maxnov.com/",
secure_regex = /^https:/,
is_secure = secure_regex.test(img_url) || secure_regex.test(page_url),
service_root = is_secure ? secure_root : insecure_root,
cb_stack_name = "cp_remote_image_callbacks",
cb_stack = cb_stack_name in global ? global[cb_stack_name] : global[cb_stack_name] = [],
cb_name = cb_stack_name +'['+ cb_stack.length +']',
service_url = service_root + "?url=" + escape(img_url) + "&callback=" + cb_name,
script = document.createElement('script');
cb_stack.push( callback );
script.src = service_url;
document.body.appendChild(script);
};
function loadImage( img, callback )
{
var onImageLoaded = function( e ){ callback( e.target ); },
onDataLoaded = function ( obj )
{
img.addEventListener( 'load', onImageLoaded, false );
img.src = obj.data;
};
if ( !hasSameOrigin( img.src ) ) {
getRemoteImageData( img.src, onDataLoaded );
} else {
img.complete
? callback( img )
: img.addEventListener( 'load', onImageLoaded, false );
}
}
function canvasFromImage( img, onCanvasReady, onImageLoaded )
{
onImageLoaded = onImageLoaded || function ( img )
{
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage( img, 0, 0 );
onCanvasReady( canvas, img );
};
loadImage( img, onImageLoaded );
}
return canvasFromImage;
})(window, document);