-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasfoto_generator.js
224 lines (188 loc) · 8.28 KB
/
pasfoto_generator.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
/*
Free Pirateparty photo generator v1.2
Made by Joshua (TheYOSH) Rubingh for Piratenpartij Nederland - 10 Sept 2016
This script uses the latest HTML5 techniques for offline file reading and manipulation.
No data is uploaded to the server. All graphical calculation is done in the local browser.
*/
// Setup
var pasfoto_source = new Image(),
pasfoto_overlay = new Image(),
pp_canvas = null,
pp_max_width = 400,
pp_max_height = 500,
defaultpadding = 5,
rotation = 0,
basepath = '/',
overlay_images = new Array('purple_bar2.png', 'piratenpartij_nederland_logo_zwart2.png', 'piratenpartij_nederland_logo_zwart.png', 'piratenpartij_nederland_logo_wit.png', 'purple_saveyourinternet.png', 'transparantie-rond-tw.png', 'transparantie-onder-tw.png', 'stem-piraat-tw.png' );
basepath = '/wp-content/themes/ppnl/pasfoto_generator/';
//basepath = '';
// Load the canvas loader listen for new images
pasfoto_source.onload = function() {
rotation = 0;
generate_pasfoto();
};
jQuery(document).ready(function(){
jQuery('head').append('<link href="' + basepath + 'pasfoto_generator.css" rel="stylesheet" id="pasfoto_generator_css" />');
load_app();
// Listener for new image
document.getElementById('pasfoto').addEventListener('change', handleFileSelect, false);
// Listener for image rotations
document.getElementById("rotate").addEventListener('click', rotate_image, false);
// Listener for download action
document.getElementById("dl").addEventListener('click', download_pasfoto, false);
// Load initial free image
pasfoto_source.src = basepath + 'pasfoto.jpg';
});
function rotate_image() {
// Rotate image wit 90 degrees per step.
rotation = rotation + 90;
rotation = rotation % 360;
// Generate the new photo
generate_pasfoto();
}
// Borrowed from: http://stackoverflow.com/questions/13073647/crop-canvas-export-html5-canvas-with-certain-width-and-height#13074780
function generate_pasfoto() {
padding = defaultpadding;
pp_canvas = document.getElementsByTagName("canvas")[0];
pp_canvas.width = pp_max_width;
pp_canvas.height = pp_max_height;
// Initialize the image such as rotation and scaling..
initialize_pasfoto();
// Here we add the image overlay
var pp_context = pp_canvas.getContext("2d");
// Load overlay image
if (jQuery('div.overlay.checked').length == 1) {
pasfoto_overlay.src = jQuery('div.overlay.checked img').attr('src');
if (pasfoto_overlay.src.indexOf('purple_bar') >= 0) {
padding = 0;
}
var xpos = padding, ypos = padding;
// Check for selected position
if (jQuery('table.position td.checked').length == 1) {
switch (jQuery('table.position td.checked').attr('id')) {
case 'lt':
// Default
break;
case 'rt':
xpos = pp_canvas.width - (pasfoto_overlay.width + padding);
break;
case 'lb':
ypos = pp_canvas.height - (pasfoto_overlay.height + padding);
break;
case 'rb':
xpos = pp_canvas.width - (pasfoto_overlay.width + padding);
ypos = pp_canvas.height - (pasfoto_overlay.height + padding);
break;
}
}
// Draw the overlay on the main image on selected position
pp_context.drawImage(pasfoto_overlay, xpos, ypos, pasfoto_overlay.width, pasfoto_overlay.height);
}
}
// Borrowed from: https://stackoverflow.com/questions/17411991/html5-canvas-rotate-image
function initialize_pasfoto() {
// Rotation
var tempCanvas = document.createElement("canvas");
var pp_context = tempCanvas.getContext("2d");
if(rotation == 90 || rotation == 270) {
tempCanvas.width = pasfoto_source.height;
tempCanvas.height = pasfoto_source.width;
} else {
tempCanvas.width = pasfoto_source.width;
tempCanvas.height = pasfoto_source.height;
}
pp_context.clearRect(0,0,tempCanvas.width,tempCanvas.height);
if(rotation == 90 || rotation == 270) {
pp_context.translate(pasfoto_source.height/2,pasfoto_source.width/2);
} else {
pp_context.translate(pasfoto_source.width/2,pasfoto_source.height/2);
}
pp_context.rotate(rotation*Math.PI/180);
pp_context.drawImage(pasfoto_source,-pasfoto_source.width/2,-pasfoto_source.height/2);
// Scaling up / down....
var scale_width = 0, scale_height = 0, scale = 0;
scale_width = pp_max_width / tempCanvas.width;
scale_height = pp_max_height / tempCanvas.height;
scale = (scale_width < scale_height ? scale_width : scale_height);
pp_canvas.width = scale * tempCanvas.width;
pp_canvas.height = scale * tempCanvas.height;
pp_context = pp_canvas.getContext("2d");
pp_context.drawImage(tempCanvas,0,0,pp_canvas.width,pp_canvas.height);
}
// Borrowed from: http://stackoverflow.com/questions/12796513/html5-canvas-to-png-file/12796748
function download_pasfoto() {
var dt = pp_canvas.toDataURL('image/png');
/* Change MIME type to trick the browser to downlaod the file instead of displaying it */
//dt = dt.replace(/^data:image\/[^;]*/, 'data:application/octet-stream');
/* In addition to <a>'s "download" attribute, you can define HTTP-style headers */
//dt = dt.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=Canvas.png');
this.href = dt;
}
// Borrowed from: http://www.html5rocks.com/en/tutorials/file/dndfiles/
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Load image
pasfoto_source.src = e.target.result;
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
// Only support 1 image!
jQuery('td.result a').hide();
break;
}
}
function load_app() {
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
var div1 = jQuery('<div>').addClass('generator_menu');
div1.append(jQuery('<h2>').text('1. Selecteer pasfoto'));
div1.append(jQuery('<input>').attr({type:'file', name:'pasfoto', id: 'pasfoto'}));
div1.append(jQuery('<br>'));
div1.append(jQuery('<input>').attr({type:'button', name:'rotate', id: 'rotate', value:'Draai'}));
jQuery('div#pasfoto_generator').html('');
jQuery('div#pasfoto_generator').append(div1);
var div2 = jQuery('<div>').addClass('generator_preview');
div2.append(jQuery('<h2>').text('Preview'));
div2.append(jQuery('<canvas>').attr({width:pp_max_width, height:pp_max_height}));
jQuery('div#pasfoto_generator').append(div2);
var div3 = jQuery('<div>').addClass('generator_menu');
div3.append(jQuery('<h2>').text('2. Selecteer logo'));
jQuery.each(overlay_images,function(index,value){
div3.append(jQuery('<div>').addClass('overlay' + (index === 0 ? ' checked' : '')).append(jQuery('<img>').attr({src: basepath + 'overlays/' + value})));
});
div3.append(jQuery('<h2>').text('3. Selecteer positie'));
div3.append('<table class="position"><tr><td id="lt"></td><td id="rt"></td></tr><tr><td id="lb"></td><td class="checked" id="rb"></td></tr></table>');
div3.append(jQuery('<h2>').text('4. Sla op!'));
var downloadLink = jQuery('<a>').addClass('download_button').attr({id:'dl', href:'#', target:'_blank'}).text('Piratenpartij pasfoto');
if ( ! /Android012345RareHack/i.test(navigator.userAgent) ) {
downloadLink.attr({download:'Piratenpartij_pasfoto.png'});
}
div3.append(downloadLink);
// div3.append(jQuery('<a>').addClass('download_button').attr({id:'dl', href:'#', download:'Piratenpartij_pasfoto.png', target:'_blank'}).text('Piratenpartij pasfoto'));
jQuery('div#pasfoto_generator').append(div3);
jQuery('div.overlay').on('click',function(){
jQuery('div.overlay').removeClass('checked');
jQuery(this).addClass('checked');
generate_pasfoto();
});
jQuery('table.position td').on('click',function(){
jQuery('table.position td').removeClass('checked');
jQuery(this).addClass('checked');
generate_pasfoto();
});
}