RGraph.common.zoom.js 8.3 KB

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