RGraph.common.zoom.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // version: 2014-06-26
  2. /**
  3. * o--------------------------------------------------------------------------------o
  4. * | This file is part of the RGraph package. RGraph is Free Software, licensed |
  5. * | under the MIT license - so it's free to use for all purposes. If you want to |
  6. * | donate to help keep the project going then you can do so here: |
  7. * | |
  8. * | http://www.rgraph.net/donate |
  9. * o--------------------------------------------------------------------------------o
  10. */
  11. RGraph = window.RGraph || {isRGraph: true};
  12. // Module pattern
  13. (function (win, doc, undefined)
  14. {
  15. var RG = RGraph,
  16. ua = navigator.userAgent,
  17. ma = Math;
  18. /**
  19. * A zoom in function
  20. *
  21. * @param e object The event object
  22. */
  23. RG.zoom =
  24. RG.Zoom = function (e)
  25. {
  26. e = RGraph.fixEventObject(e);
  27. /**
  28. * Triggered from a context menu
  29. */
  30. if (e && e.target && e.target.__canvas__) {
  31. var canvas = e.target.__canvas__;
  32. /**
  33. * This is here to facilitate zooming by just a single left click
  34. */
  35. } else if (e && e.target && e.target.__object__) {
  36. var canvas = e.target.__object__.canvas;
  37. e.stopPropagation(); // Hmmmm
  38. }
  39. // Fallback for MSIE9
  40. if (!canvas) {
  41. var registry_canvas = RGraph.Registry.get('chart.contextmenu').__canvas__;
  42. if (registry_canvas) {
  43. var canvas = registry_canvas;
  44. }
  45. }
  46. var obj = canvas.__object__;
  47. var context = obj.context;
  48. var dataurl = canvas.toDataURL();
  49. var tmp = canvas;
  50. var coords = RG.getCanvasXY(canvas);
  51. var factor = obj.Get('chart.zoom.factor') - 1;
  52. var x = coords[0];
  53. var y = coords[1];
  54. var img = document.createElement('img');
  55. img.className = 'RGraph_zoomed_canvas';
  56. img.style.border = '1px solid #ccc';
  57. img.style.width = canvas.width + 'px';
  58. img.style.height = canvas.height + 'px';
  59. img.style.position = 'absolute';
  60. img.style.left = x + 'px';
  61. img.style.top = y + 'px';
  62. img.style.backgroundColor = 'white';
  63. img.style.opacity = obj.Get('chart.zoom.fade.in') ? 0 : 1;
  64. img.style.zIndex = 99;
  65. img.src = dataurl;
  66. document.body.appendChild(img);
  67. //RGraph.Registry.Set('chart.zoomedimage', img);
  68. // Store the zoomed image in a global var - NOT the registry
  69. RGraph.zoom_image = img;
  70. RGraph.zoom_image.obj = obj;
  71. // Image onclick should not hide the image
  72. img.onclick = function (e)
  73. {
  74. e = RGraph.fixEventObject(e);
  75. e.stopPropagation();
  76. return false;
  77. }
  78. //setTimeout(function (){document.body.onclick = RGraph.HideZoomedCanvas;}, 1);
  79. var width = parseInt(canvas.width);
  80. var height = parseInt(canvas.height);
  81. var frames = obj.Get('chart.zoom.frames');
  82. var delay = obj.Get('chart.zoom.delay');
  83. // Increase the width over 10 frames - center
  84. if (obj.Get('chart.zoom.hdir') == 'center') {
  85. for (var i=1; i<=frames; ++i) {
  86. var newWidth = width * factor * (i/frames) + width;
  87. var rightHandEdge = x + canvas.width;
  88. var newLeft = (x + (canvas.width / 2)) - (newWidth / 2);
  89. setTimeout("RGraph.zoom_image.style.width = '" + String(newWidth) + "px'; RGraph.zoom_image.style.left = '" + newLeft + "px'", i * delay);
  90. }
  91. // Left
  92. } else if (obj.Get('chart.zoom.hdir') == 'left') {
  93. for (var i=1; i<=frames; ++i) {
  94. var newWidth = width * factor * (i/frames) + width;
  95. var rightHandEdge = x + canvas.width;
  96. var newLeft = rightHandEdge - newWidth;
  97. setTimeout("RGraph.zoom_image.style.width = '" + String(newWidth) + "px'; RGraph.zoom_image.style.left = '" + newLeft + "px'", i * delay);
  98. }
  99. // Right (default)
  100. } else {
  101. for (var i=1; i<=frames; ++i) {
  102. var newWidth = width * factor * (i/frames) + width;
  103. setTimeout("RGraph.zoom_image.style.width = '" + String(newWidth) + "px'", i * delay);
  104. }
  105. }
  106. // Increase the height over 10 frames - up
  107. if (obj.Get('chart.zoom.vdir') == 'up') {
  108. for (var i=1; i<=frames; ++i) {
  109. var newHeight = (height * factor * (i/frames)) + height;
  110. var bottomEdge = y + canvas.height;
  111. var newTop = bottomEdge - newHeight;
  112. setTimeout("RGraph.zoom_image.style.height = '" + String(newHeight) + "px'; RGraph.zoom_image.style.top = '" + newTop + "px'", i * delay);
  113. }
  114. // center
  115. } else if (obj.Get('chart.zoom.vdir') == 'center') {
  116. for (var i=1; i<=frames; ++i) {
  117. var newHeight = (height * factor * (i/frames)) + height;
  118. var bottomEdge = (y + (canvas.height / 2)) + (newHeight / 2);
  119. var newTop = bottomEdge - newHeight;
  120. setTimeout("RGraph.zoom_image.style.height = '" + String(newHeight) + "px'; RGraph.zoom_image.style.top = '" + newTop + "px'", i * delay);
  121. }
  122. // Down (default
  123. } else {
  124. for (var i=1; i<=frames; ++i) {
  125. setTimeout("RGraph.zoom_image.style.height = '" + String(height * factor * (i/frames) + height) + "px'", i * delay);
  126. }
  127. }
  128. // If enabled, increase the opactity over the requested number of frames
  129. if (obj.Get('chart.zoom.fade.in')) {
  130. for (var i=1; i<=frames; ++i) {
  131. setTimeout("RGraph.zoom_image.style.opacity = " + Number(i / frames), i * (delay / 2));
  132. }
  133. }
  134. // If stipulated, produce a shadow
  135. if (obj.Get('chart.zoom.shadow')) {
  136. for (var i=1; i<=frames; ++i) {
  137. setTimeout("RGraph.zoom_image.style.boxShadow = 'rgba(128,128,128," + Number(i / frames) / 2 + ") 0 0 25px'", i * delay);
  138. setTimeout("RGraph.zoom_image.style.MozBoxShadow = 'rgba(128,128,128," + Number(i / frames) / 2 + ") 0 0 25px'", i * delay);
  139. setTimeout("RGraph.zoom_image.style.WebkitBoxShadow = 'rgba(128,128,128," + Number(i / frames) / 2 + ") 0 0 25px'", i * delay);
  140. }
  141. }
  142. /**
  143. * Create the background. As of March 2013 this is always done
  144. */
  145. var div = document.createElement('DIV');
  146. div.style.backgroundColor = 'white';
  147. div.style.opacity = 0;
  148. div.style.position = 'fixed';
  149. div.style.top = 0;
  150. div.style.left = 0;
  151. div.style.width = (screen.width + 100) + 'px';
  152. div.style.height = (screen.height + 100) + 'px';
  153. div.style.zIndex = 98;
  154. // Hides the zoomed caboodle
  155. div.onclick =
  156. div.oncontextmenu = function (e)
  157. {
  158. return RG.hideZoomedCanvas(e);
  159. }
  160. div.origHeight = div.style.height;
  161. document.body.appendChild(div);
  162. RG.zoom_background = div;
  163. // If the window is resized, hide the zoom
  164. //window.onresize = RGraph.HideZoomedCanvas;
  165. for (var i=1; i<=frames; ++i) {
  166. setTimeout("RGraph.zoom_background.style.opacity = " + (Number(i / frames) * 0.8), i * (delay / 2));
  167. }
  168. /**
  169. * Fire the onzoom event
  170. */
  171. RG.fireCustomEvent(obj, 'onzoom');
  172. }
  173. // End module pattern
  174. })(window, document);