Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- var frames = document.getElementsByClassName('frame'),
- frameId = 0;
- getFrame = function(id) {
- id = id || 0;
- gid('heading').innerHTML = frames[id].getAttribute('data-heading');
- gid('back').style.display = id ? 'inline' : 'none';
- var i = frames.length;
- while (i--) {
- frames[i].style.display = i == id ? 'block' : 'none';
- }
- }
- nextFrame = function() {
- getFrame(++frameId);
- };
- prevFrame = function() {
- getFrame(--frameId);
- }
- })();
- function setUploadError(message) {
- gid('uploadError').innerHTML = message ? message : '';
- }
- function imagePreview(data) {
- var scale = 600,
- img = new Image;
- img.src = data;
- img.onload = function() {
- var proportion = this.width / this.height,
- cnv = gid('preview'),
- ctx = cnv.getContext('2d');
- console.log('resizing');
- console.log('before: width=' + this.width + ', height=' + this.height);
- if (this.width > scale) {
- this.height = (this.width = scale) / proportion;
- }
- else if (this.height > scale) {
- this.width = (this.height = scale) * proportion;
- }
- console.log('after: width=' + this.width + ', height=' + this.height);
- cnv.width = this.width;
- cnv.height = this.height;
- ctx.drawImage(this, 0, 0, this.width, this.height);
- nextFrame();
- }
- gid('uploadForm').reset();
- setUploadError();
- }
- function createMem(type, img) {
- var cnv = gid('mem'),
- ctx = cnv.getContext('2d'),
- // отступ от края
- x1 = 40,
- // толщина белой линии
- x2 = 3,
- // промежуток между линией и изображением
- x3 = 2,
- x4 = x1 + x2 + x3,
- x5 = x4 * 2,
- x6 = x2 * 2 + x3 * 2,
- x7 = x1 + x2,
- x8 = x3 * 2;
- cnv.width = img.width;
- cnv.height = img.height;
- img.width -= x5;
- img.height -= x5;
- ctx.fillStyle = '#000000';
- ctx.fillRect(0, 0, cnv.width, cnv.height);
- ctx.fillStyle = '#ffffff';
- ctx.fillRect(x1, x1, img.width + x6, img.height + x6);
- ctx.fillStyle = '#000000';
- ctx.fillRect(x7, x7, img.width + x8, img.height + x8);
- ctx.drawImage(img, x4, x4, img.width, img.height);
- nextFrame();
- }
- this.addEventListener('load', function() {
- gid('uploadForm').onsubmit = function() {
- var u = ( gid('uploadUrl').value = gid('uploadUrl').value.trim() );
- if (u) {
- var o = url.parse(u);
- if (!o) {
- setUploadError('Это не ссылка');
- }
- else if (o.scheme && o.scheme != 'http') {
- setUploadError('Протокол ' + o.scheme + ' не поддерживается');
- }
- else if (!o.host) {
- setUploadError('Не указан хост');
- }
- else if (!o.path || o.path == '/') {
- setUploadError('Не указан путь до файла');
- }
- else {
- ajax.post('get_image.php', {hostname: o.host, port: o.port || 80, uri: o.path + (o.query ? '?' + o.query : '')}, {
- done: function(response) {
- if (response.substr(0, 4) != 'data') {
- return setUploadError(response);
- }
- imagePreview(response);
- },
- fail: function() {
- this.reset()
- setUploadError('Сервис временно недоступен');
- }
- });
- }
- }
- return false;
- };
- gid('uploadImage').onchange = function(event) {
- var file = event.target.files[0];
- if ( !new RegExp('^image/(' + conf.allowedImageTypes.join('|') + ')$').test(file.type) ) {
- return setUploadError('Неправильный тип файла');
- }
- var reader = new FileReader;
- reader.onload = function(event) {
- imagePreview(event.target.result);
- };
- reader.readAsDataURL(file);
- };
- gid('back').onclick = prevFrame;
- gid('previewForm').onsubmit = function() {
- var img = new Image;
- img.src = gid('preview').toDataURL();
- img.onload = function() {
- createMem( gid('memType').options[ gid('memType').selectedIndex ].value, this );
- };
- return false;
- };
- gid('copyright').innerHTML = '© All rights reserved <a href="/" target="_blank">' + location.host.toUpperCase() + '</a>, 2013.';
- VK.init(getFrame);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement