jcrop.go.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /** Init for Jcrop library and page setup
  2. *
  3. * @package StatusNet
  4. * @author Sarven Capadisli <csarven@status.net>
  5. * @author Mikael Nordfeldth <mmn@hethane.se>
  6. * @copyright 2009 StatusNet, Inc.
  7. * @copyright 2013 Free Software Foundation, Inc.
  8. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  9. * @link http://status.net/
  10. */
  11. $(function(){
  12. var x = ($('#avatar_crop_x').val()) ? $('#avatar_crop_x').val() : 0;
  13. var y = ($('#avatar_crop_y').val()) ? $('#avatar_crop_y').val() : 0;
  14. var w = ($('#avatar_crop_w').val()) ? $('#avatar_crop_w').val() : $("#avatar_original img").attr("width");
  15. var h = ($('#avatar_crop_h').val()) ? $('#avatar_crop_h').val() : $("#avatar_original img").attr("height");
  16. $("#avatar_original img").Jcrop({
  17. onChange: showPreview,
  18. setSelect: [ x, y, w, h ],
  19. onSelect: updateCoords,
  20. aspectRatio: 1,
  21. boxWidth: 480,
  22. boxHeight: 480,
  23. bgColor: '#000',
  24. bgOpacity: .4
  25. });
  26. });
  27. function showPreview(coords) {
  28. var rx = 96 / coords.w;
  29. var ry = 96 / coords.h;
  30. var img_width = $("#avatar_original img").attr("width");
  31. var img_height = $("#avatar_original img").attr("height");
  32. $('#avatar_preview img').css({
  33. width: Math.round(rx *img_width) + 'px',
  34. height: Math.round(ry * img_height) + 'px',
  35. marginLeft: '-' + Math.round(rx * coords.x) + 'px',
  36. marginTop: '-' + Math.round(ry * coords.y) + 'px'
  37. });
  38. };
  39. function updateCoords(c) {
  40. $('#avatar_crop_x').val(c.x);
  41. $('#avatar_crop_y').val(c.y);
  42. $('#avatar_crop_w').val(c.w);
  43. $('#avatar_crop_h').val(c.h);
  44. };