87e3bd7c59588d5e619a88cb054030d4fe02a64b.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <html>
  2. <head>
  3. <title>dojox.gfx: interactive analog clock</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <style type="text/css">
  6. @import "../../../dojo/resources/dojo.css";
  7. @import "../../../dijit/tests/css/dijitTests.css";
  8. </style>
  9. <!--
  10. The next line should include Microsoft's Silverlight.js, if you plan to use the silverlight backend
  11. <script type="text/javascript" src="Silverlight.js"></script>
  12. -->
  13. <script type="text/javascript" src="../../../dojo/dojo.js"></script>
  14. <script type="text/javascript">
  15. dojo.require("dojox.gfx");
  16. dojo.require("dojo.date.locale");
  17. var current_time = new Date();
  18. var hour_hand = null;
  19. var minute_hand = null;
  20. var second_hand = null;
  21. var hour_shadow = null;
  22. var minute_shadow = null;
  23. var second_shadow = null;
  24. var center = {x: 385 / 2, y: 385 / 2};
  25. var hour_shadow_shift = {dx: 2, dy: 2};
  26. var minute_shadow_shift = {dx: 3, dy: 3};
  27. var second_shadow_shift = {dx: 4, dy: 4};
  28. var selected_hand = null;
  29. var container = null;
  30. var container_position = null;
  31. var text_time = null;
  32. var diff_time = new Date();
  33. placeHand = function(shape, angle, shift){
  34. var move = {dx: center.x + (shift ? shift.dx : 0), dy: center.y + (shift ? shift.dy : 0)};
  35. return shape.setTransform([move, dojox.gfx.matrix.rotateg(angle)]);
  36. };
  37. placeHourHand = function(h, m, s){
  38. var angle = 30 * (h % 12 + m / 60 + s / 3600);
  39. placeHand(hour_hand, angle);
  40. placeHand(hour_shadow, angle, hour_shadow_shift);
  41. };
  42. placeMinuteHand = function(m, s){
  43. var angle = 6 * (m + s / 60);
  44. placeHand(minute_hand, angle);
  45. placeHand(minute_shadow, angle, minute_shadow_shift);
  46. };
  47. placeSecondHand = function(s){
  48. var angle = 6 * s;
  49. placeHand(second_hand, angle);
  50. placeHand(second_shadow, angle, second_shadow_shift);
  51. };
  52. reflectTime = function(time, hold_second_hand, hold_minute_hand, hold_hour_hand){
  53. if(!time) time = current_time;
  54. var h = time.getHours();
  55. var m = time.getMinutes();
  56. var s = time.getSeconds();
  57. if(!hold_hour_hand) placeHourHand(h, m, s);
  58. if(!hold_minute_hand) placeMinuteHand(m, s);
  59. if(!hold_second_hand) placeSecondHand(s);
  60. text_time.innerHTML = dojo.date.locale.format(
  61. time, {selector: "time", timePattern: "h:mm:ss a"});
  62. };
  63. resetTime = function(){
  64. current_time = new Date();
  65. reflectTime();
  66. };
  67. tick = function(){
  68. current_time.setSeconds(current_time.getSeconds() + 1);
  69. reflectTime();
  70. };
  71. advanceTime = function(){
  72. if(!selected_hand) {
  73. tick();
  74. }
  75. };
  76. normalizeAngle = function(angle){
  77. if(angle > Math.PI) {
  78. angle -= 2 * Math.PI;
  79. } else if(angle < -Math.PI) {
  80. angle += 2 * Math.PI;
  81. }
  82. return angle;
  83. };
  84. calculateAngle = function(x, y, handAngle){
  85. try {
  86. return normalizeAngle(Math.atan2(y - center.y, x - center.x) - handAngle);
  87. } catch(e) {
  88. // supress
  89. }
  90. return 0;
  91. };
  92. getSecondAngle = function(time){
  93. if(!time) time = current_time;
  94. return (6 * time.getSeconds() - 90) / 180 * Math.PI;
  95. };
  96. getMinuteAngle = function(time){
  97. if(!time) time = current_time;
  98. return (6 * (time.getMinutes() + time.getSeconds() / 60) - 90) / 180 * Math.PI;
  99. };
  100. getHourAngle = function(time){
  101. if(!time) time = current_time;
  102. return (30 * (time.getHours() + (time.getMinutes() + time.getSeconds() / 60) / 60) - 90) / 180 * Math.PI;
  103. };
  104. onMouseDown = function(evt){
  105. selected_hand = evt.target;
  106. diff_time.setTime(current_time.getTime());
  107. dojo.stopEvent(evt);
  108. };
  109. onMouseMove = function(evt){
  110. if(!selected_hand) return;
  111. if(evt.target == second_hand.getEventSource() ||
  112. evt.target == minute_hand.getEventSource() ||
  113. evt.target == hour_hand.getEventSource()){
  114. dojo.stopEvent(evt);
  115. return;
  116. }
  117. if(dojox.gfx.equalSources(selected_hand, second_hand.getEventSource())){
  118. var angle = calculateAngle(
  119. evt.clientX - container_position.x,
  120. evt.clientY - container_position.y,
  121. normalizeAngle(getSecondAngle())
  122. );
  123. var diff = Math.round(angle / Math.PI * 180 / 6); // in whole seconds
  124. current_time.setSeconds(current_time.getSeconds() + Math.round(diff));
  125. reflectTime();
  126. }else if(dojox.gfx.equalSources(selected_hand, minute_hand.getEventSource())){
  127. var angle = calculateAngle(
  128. evt.clientX - container_position.x,
  129. evt.clientY - container_position.y,
  130. normalizeAngle(getMinuteAngle(diff_time))
  131. );
  132. var diff = Math.round(angle / Math.PI * 180 / 6 * 60); // in whole seconds
  133. diff_time.setTime(diff_time.getTime() + 1000 * diff);
  134. reflectTime(diff_time, true);
  135. }else if(dojox.gfx.equalSources(selected_hand, hour_hand.getEventSource())){
  136. var angle = calculateAngle(
  137. evt.clientX - container_position.x,
  138. evt.clientY - container_position.y,
  139. normalizeAngle(getHourAngle(diff_time))
  140. );
  141. var diff = Math.round(angle / Math.PI * 180 / 30 * 60 * 60); // in whole seconds
  142. diff_time.setTime(diff_time.getTime() + 1000 * diff);
  143. reflectTime(diff_time, true, true);
  144. }else{
  145. return;
  146. }
  147. dojo.stopEvent(evt);
  148. };
  149. onMouseUp = function(evt){
  150. if(selected_hand && !dojox.gfx.equalSources(selected_hand, second_hand.getEventSource())){
  151. current_time.setTime(diff_time.getTime());
  152. reflectTime();
  153. }
  154. selected_hand = null;
  155. dojo.stopEvent(evt);
  156. };
  157. makeShapes = function(){
  158. // prerequisites
  159. container = dojo.byId("gfx_holder");
  160. container_position = dojo.coords(container, true);
  161. text_time = dojo.byId("time");
  162. var surface = dojox.gfx.createSurface(container, 385, 385);
  163. surface.createImage({width: 385, height: 385, src: "images/clock_face.jpg"});
  164. // hand shapes
  165. var hour_hand_points = [{x: -7, y: 15}, {x: 7, y: 15}, {x: 0, y: -60}, {x: -7, y: 15}];
  166. var minute_hand_points = [{x: -5, y: 15}, {x: 5, y: 15}, {x: 0, y: -100}, {x: -5, y: 15}];
  167. var second_hand_points = [{x: -2, y: 15}, {x: 2, y: 15}, {x: 2, y: -105}, {x: 6, y: -105}, {x: 0, y: -116}, {x: -6, y: -105}, {x: -2, y: -105}, {x: -2, y: 15}];
  168. // create shapes
  169. hour_shadow = surface.createPolyline(hour_hand_points)
  170. .setFill([0, 0, 0, 0.1])
  171. ;
  172. hour_hand = surface.createPolyline(hour_hand_points)
  173. .setStroke({color: "black", width: 2})
  174. .setFill("#889")
  175. ;
  176. minute_shadow = surface.createPolyline(minute_hand_points)
  177. .setFill([0, 0, 0, 0.1])
  178. ;
  179. minute_hand = surface.createPolyline(minute_hand_points)
  180. .setStroke({color: "black", width: 2})
  181. .setFill("#ccd")
  182. ;
  183. second_shadow = surface.createPolyline(second_hand_points)
  184. .setFill([0, 0, 0, 0.1])
  185. ;
  186. second_hand = surface.createPolyline(second_hand_points)
  187. .setStroke({color: "#800", width: 1})
  188. .setFill("#d00")
  189. ;
  190. // next 3 lines kill Silverlight because its nodes do not support CSS
  191. //dojox.gfx._addClass(hour_hand .getEventSource(), "movable");
  192. //dojox.gfx._addClass(minute_hand.getEventSource(), "movable");
  193. //dojox.gfx._addClass(second_hand.getEventSource(), "movable");
  194. surface.createCircle({r: 1}).setFill("black").setTransform({dx: 192.5, dy: 192.5});
  195. // attach events
  196. hour_hand .connect("onmousedown", onMouseDown);
  197. minute_hand.connect("onmousedown", onMouseDown);
  198. second_hand.connect("onmousedown", onMouseDown);
  199. dojo.connect(container, "onmousemove", onMouseMove);
  200. dojo.connect(container, "onmouseup", onMouseUp);
  201. dojo.connect(dojo.byId("reset"), "onclick", resetTime);
  202. // start the clock
  203. resetTime();
  204. window.setInterval(advanceTime, 1000);
  205. };
  206. dojo.addOnLoad(makeShapes);
  207. </script>
  208. <style type="text/css">
  209. .movable { cursor: hand; }
  210. </style>
  211. </head>
  212. <body>
  213. <h1>dojox.gfx: interactive analog clock</h1>
  214. <p>Grab hands and set your own time.</p>
  215. <p>Warning: Canvas renderer doesn't implement event handling.</p>
  216. <div id="gfx_holder" style="width: 385px; height: 385px;"></div>
  217. <p>Current time: <span id="time"></span>.</p>
  218. <p><button id="reset">Reset</button></p>
  219. </body>
  220. </html>