2015-03-03 4 views
-1

У меня есть эти функцииДоступ к функции осуществляется из другой функции?

function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 

     reader.onload = function (e) { 

      var img = new Image; 
      img.src = reader.result; 

      var imgwidth = img.width; 
      var imgheight = img.height; 
      console.log(imgwidth); 
      console.log(imgheight); 

      $('#imgcrop').attr('src', e.target.result); 
      $('#preview').attr('src', e.target.result); 
      $('.crop').Jcrop({ 

       onSelect: updateCoords, 
       boxWidth: 300, 
       boxHeight: 300, 
       onChange: showPreview, 
       aspectRatio: 1, 
       bgOpacity: .4, 
       setSelect: [ 100, 100, 50, 50 ] 
      }); 
     }; 

     reader.readAsDataURL(input.files[0]); 
    } 
} 

$("#imgInp").change(function() { 
    console.log(this); 
    readURL(this); 
}); 

function updateCoords(c) { 
    console.log(c); 
    $('#x').val(c.x); 
    $('#y').val(c.y); 
    $('#w').val(c.w); 
    $('#h').val(c.h); 
} 

function showPreview(coords) { 
    var rx = 100/coords.w; 
    var ry = 100/coords.h; 

    $('#preview').css({ 
     width: Math.round(rx * imgwidth) + 'px', 
     height: Math.round(ry * imgheight) + 'px', 
     marginLeft: '-' + Math.round(rx * coords.x) + 'px', 
     marginTop: '-' + Math.round(ry * coords.y) + 'px' 
    }); 
} 

И мне нужно, чтобы получить доступ к imgwidth и imgheight из функции чтения файла для использования в функции showpreview, но у меня возникают проблемы со сферой этих функций. Как я могу получить доступ к этим значениям или хотя бы передать их в качестве параметров для использования в другой функции?

Спасибо!

+0

объявить глобальную переменную, вне функций. –

+0

Можно ли это сделать? Хорошо ли иметь глобальные переменные? Означает ли этот случай использование глобальных переменных? Извините, я новичок в JS, и мне сказали не загрязнять глобальный охват, чтобы я боялся его использовать. – Barbarah

+0

Я думаю, это зависит от вашей ситуации, но я тоже не эксперт. Я думаю, что ответ Бармара должен работать. –

ответ

0

Передайте их в качестве аргументов showPreview:

function showPreview(coords, imgheight, imgwidth) { 
    var rx = 100/coords.w; 
    var ry = 100/coords.h; 

    $('#preview').css({ 
     width: Math.round(rx * imgwidth) + 'px', 
     height: Math.round(ry * imgheight) + 'px', 
     marginLeft: '-' + Math.round(rx * coords.x) + 'px', 
     marginTop: '-' + Math.round(ry * coords.y) + 'px' 
    }); 
} 

function readURL(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 

     reader.onload = function (e) { 

      var img = new Image; 
      img.src = reader.result; 

      var imgwidth = img.width; 
      var imgheight = img.height; 
      console.log(imgwidth); 
      console.log(imgheight); 

      $('#imgcrop').attr('src', e.target.result); 
      $('#preview').attr('src', e.target.result); 
      $('.crop').Jcrop({ 

       onSelect: updateCoords, 
       boxWidth: 300, 
       boxHeight: 300, 
       onChange: function(coords) { 
        showPreview(coords, imgheight, imgwidth); 
       }, 
       aspectRatio: 1, 
       bgOpacity: .4, 
       setSelect: [ 100, 100, 50, 50 ] 
      }); 
     }; 

     reader.readAsDataURL(input.files[0]); 
    } 
} 
+0

Спасибо, это одна из первых вещей, которые я пробовал, но она не работает. – Barbarah

+0

Выполняет ли запись правильной высоты и ширины в функции 'readURL'? – Barmar

+0

Нет, показывает нуль для обоих. – Barbarah

Смежные вопросы