Guest User

Untitled

a guest
May 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript" src="jquery.js"></script>
  5. <script type="text/javascript" src="jquery.rightClick.js"></script>
  6. <script type="text/javascript">
  7. $(function() {
  8. var zoom_ratio = 1;
  9.  
  10. $('#content').offset({top: $('#view_window').width() / 2, left: $('#view_window').height() / 2});
  11.  
  12. $('#view_window').click(function(event) {
  13. //panning process
  14. var window_size = {width: $('#view_window').width(), height: $('#view_window').height()};
  15. var click_pos = {x: event.pageX, y:event.pageY};
  16. var now_pan = $('#content').offset();
  17.  
  18. var next_pan = get_next_pan(click_pos, now_pan, zoom_ratio, +1, {x: window_size.width / 2, y:window_size.height / 2});
  19. $('#content').offset(next_pan);
  20.  
  21. //zooming process
  22. zoom_ratio += 1;
  23. zoomin($('#content'), zoom_ratio);
  24. });
  25.  
  26. $('#view_window').rightClick(function(event) {
  27. //panning process
  28. var window_size = {width: $('#view_window').width(), height: $('#view_window').height()};
  29. var click_pos = {x: event.pageX, y:event.pageY};
  30. var now_pan = $('#content').offset();
  31.  
  32. var next_pan = get_next_pan(click_pos, now_pan, zoom_ratio, -1, {x: window_size.width / 2, y:window_size.height / 2});
  33. $('#content').offset(next_pan);
  34.  
  35. //zooming process
  36. zoomout($('#content'), zoom_ratio);
  37.  
  38. zoom_ratio -= 1;
  39. });
  40.  
  41. function zoomin(target, zoom_ratio) {
  42. zooming(target, zoom_ratio, function(a, b) {return a * b; });
  43. }
  44.  
  45. function zoomout(target, zoom_ratio) {
  46. console.log('zoomout');
  47. zooming(target, zoom_ratio, function(a, b) {return a / b; });
  48. }
  49.  
  50. function zooming(target, zoom_ratio, op) {
  51. target.children().each(function() {
  52. $(this).width(op($(this).width(), zoom_ratio)).
  53. height(op($(this).height(), zoom_ratio)).
  54. css('top', op($(this).css('top'), zoom_ratio)).
  55. css('left', op($(this).css('left'), zoom_ratio));
  56. });
  57. }
  58.  
  59. function get_next_pan(click_pos, now_pan, zoom_ratio, zoom_step, origin) {
  60. //position on viewport layer => position on relative(zoomable) universe
  61. var universe_click_pos = {x: click_pos.x - now_pan.left, y: click_pos.y - now_pan.top};
  62. console.log('universe click pos:' + universe_click_pos.x + ',' + universe_click_pos.y);
  63.  
  64. //position on relative universe => position on absolute universe
  65. var x_pos = {x: universe_click_pos.x / zoom_ratio, y: universe_click_pos.y / zoom_ratio};
  66. console.log('x pos:' + x_pos.x + ',' + x_pos.y);
  67.  
  68. //position on absolute universe => next position on relative universe
  69. var u_c_dash = {x: x_pos.x * (zoom_ratio + zoom_step), y: x_pos.y * (zoom_ratio + zoom_step)};
  70. console.log('u_c_dash pos:' + u_c_dash.x + ',' + u_c_dash.y);
  71.  
  72. //next position on relative universe => next pan for contents(universe layer)
  73. var next_pan = {left: origin.x - u_c_dash.x, top: origin.y - u_c_dash.y };
  74. console.log('next_pan:' + next_pan.left + ',' + next_pan.top );
  75.  
  76. return next_pan;
  77. }
  78. });
  79. </script>
  80. <style>
  81. * {
  82. margin: 0;
  83. padding: 0;
  84. }
  85. #view_window {
  86. /* for crop viewport */
  87. position: absolute;
  88. overflow: hidden;
  89. width: 300px;
  90. height: 300px;
  91.  
  92. border: 1px solid gray;
  93. }
  94.  
  95. #content {
  96. position: absolute;
  97. }
  98.  
  99. .box {
  100. position: absolute;
  101. width: 100px;
  102. height: 100px;
  103. background-color: red;
  104. }
  105.  
  106. .point {
  107. width: 1px;
  108. height: 1px;
  109. position: absolute;
  110. background-color: blue;
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div id="view_window">
  116. <div id="content">
  117. <div id="origin" class="point" style="top: 0px; left: 0px;"></div>
  118. <div id="box" class="box" style="top: 50px; left: 50px;"></div>
  119. </div>
  120. </div>
  121. </body>
  122. </html>
Add Comment
Please, Sign In to add comment