clock.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- 必须强制指定页面编码为 UTF-8 -->
  5. <meta charset="utf-8">
  6. <!-- Demo 的简要说明,必须要填写 -->
  7. <meta name="description" content="使用 Kity 绘制的仿真时钟">
  8. <!-- Demo 的作者,建议填写 -->
  9. <meta name="author" content="kity@baidu.com">
  10. <!-- Demo 的标题,必须填写 -->
  11. <title>Kity Clock</title>
  12. <!-- Demo 开发过程中使用 CMD 引入 Kity 源码,方便调试 -->
  13. <!-- dev start -->
  14. <!-- 目录型的 Demo 注意修正源码引用路径 -->
  15. <script src="../dev-lib/sea.js"></script>
  16. <script>
  17. // 设置好 kity 源代码目录
  18. seajs.config( { base: '../src'} );
  19. // 定义 Demo 模块
  20. define('demo', function(require) { require('kity'); });
  21. </script>
  22. <script>
  23. // 启动 Demo 模块
  24. seajs.use('demo');
  25. </script>
  26. <!-- dev end -->
  27. <!-- 生产使用中可以直接引用发布压缩的版本 -->
  28. <!-- production start -->
  29. <!-- 目录型的 Demo 注意修正源码引用路径 -->
  30. <!-- <script src="../dist/kity.min.js"></script> -->
  31. <!-- production end -->
  32. <style>
  33. body, div, html {
  34. margin: 0;
  35. padding: 0;
  36. overflow: hidden;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="clock" style="width: 100%; height: 100%; position: absolute;"></div>
  42. </body>
  43. <script>
  44. // 绘图画布
  45. var paper = new kity.Paper('clock').pipe(function() {
  46. this.setWidth('100%').setHeight('100%');
  47. this.setViewBox(-600, -600, 1200, 1200);
  48. });
  49. // 调色板
  50. var palette = new kity.Palette().pipe(function() {
  51. var gray = new kity.Color('gray');
  52. this.add('outside', gray.dec('l', 20));
  53. this.add('inside', gray.inc('l', 20));
  54. this.add('big-ruler', gray.dec('l', 40));
  55. this.add('small-ruler', gray.dec('l', 20));
  56. this.add('h-pointer', gray.dec('l', 50));
  57. this.add('m-pointer', gray.dec('l', 20));
  58. this.add('s-pointer', new kity.Color('hsl(0, 75, 50)'));
  59. this.add('frame-light', new kity.Color('white').dec('l', 5));
  60. this.add('frame-dark', gray.dec('l', 10));
  61. });
  62. // 框架
  63. paper.addShape(new kity.Circle(500).pipe(function(){
  64. this.fill(new kity.LinearGradientBrush().pipe(function() {
  65. this.addStop(0, palette.get('frame-dark'));
  66. this.addStop(0.4, palette.get('frame-light'));
  67. this.addStop(0.6, palette.get('frame-light'));
  68. this.addStop(1, palette.get('frame-dark'));
  69. paper.addResource(this);
  70. }));
  71. this.stroke( palette.get('outside') );
  72. this.rotate(30);
  73. }));
  74. // 背景
  75. paper.addShape(new kity.Circle(465).pipe(function(){
  76. this.fill(new kity.LinearGradientBrush().pipe(function() {
  77. this.addStop(0, palette.get('frame-light'));
  78. this.addStop(0.4, palette.get('frame-dark'));
  79. this.addStop(0.6, palette.get('frame-dark'));
  80. this.addStop(1, palette.get('frame-light'));
  81. paper.addResource(this);
  82. }));
  83. this.rotate(30);
  84. }));
  85. // 表盘
  86. paper.addShape(new kity.Circle(450).pipe(function() {
  87. this.stroke(palette.get('inside'), 8);
  88. this.fill(new kity.RadialGradientBrush().pipe(function(brush){
  89. paper.addResource(brush);
  90. brush.setFocal(0.8, 0.2);
  91. brush.setRadius(0.6);
  92. brush.addStop(0, new kity.Color('white'));
  93. brush.addStop(1, new kity.Color('hsl(0, 0, 80)'));
  94. }));
  95. }));
  96. // 刻度
  97. paper.addShape(new kity.Group().pipe(function() {
  98. var minutes = 60, stepDeg = 360 / minutes;
  99. var deg = 90, distance = 390, step = 0;
  100. while(step < minutes) {
  101. var rect;
  102. if(!(step % 5)) {
  103. rect = new kity.Rect(50, 10, 390, -5).fill(palette.get('big-ruler'));
  104. } else {
  105. rect = new kity.Rect(25, 6, 410, -3).fill(palette.get('small-ruler'));
  106. }
  107. this.addShape(rect.rotate(deg));
  108. deg += stepDeg;
  109. step ++;
  110. }
  111. }));
  112. // hello kity
  113. paper.addShape(new kity.Text().pipe(function() {
  114. this.addSpan(new kity.TextSpan('Hello ').fill(new kity.Color('blue').set('s', 40)));
  115. this.addSpan(new kity.TextSpan(' Kity').fill(new kity.Color('red').set('s', 40)));
  116. this.setPath(new kity.Path().pipe(function() {
  117. var radius = 280;
  118. var start = kity.Point.fromPolar(radius, -120), end = kity.Point.fromPolar(radius, -60);
  119. var d = this.getDrawer();
  120. d.moveTo(start.x, start.y).carcTo(radius, 0, 1, end.x, end.y);
  121. paper.addResource(this);
  122. }));
  123. this.setSize(50);
  124. this.setStartOffset(50);
  125. }));
  126. // 指针类
  127. var Pointer = kity.createClass('Pointer', {
  128. base: kity.Path,
  129. constructor: function( headWidth, tailWidth, length, offset, color ) {
  130. this.callBase();
  131. this.draw( headWidth, tailWidth, length, offset );
  132. this.fill( color );
  133. },
  134. draw: function( headWidth, tailWidth, length, offset ) {
  135. var d = this.getDrawer(),
  136. x1 = -offset, y1 = -headWidth / 2,
  137. x2 = length - offset, y2 = -tailWidth / 2;
  138. d.moveTo( x1, y1 );
  139. d.lineTo( x2, y2 );
  140. d.lineTo( x2, -y2 );
  141. d.lineTo( x1, -y1 );
  142. d.close();
  143. }
  144. })
  145. var hPointer, mPointer, sPointer, pointers;
  146. hPointer = new Pointer( 30, 15, 300, 50, palette.get('h-pointer') );
  147. mPointer = new Pointer( 20, 10, 400, 80, palette.get('m-pointer') );
  148. sPointer = new Pointer( 10, 5, 450, 90, palette.get('s-pointer') );
  149. pointers = new kity.Group().pipe(function(){
  150. this.rotate(-90);
  151. this.addShape(hPointer);
  152. this.addShape(mPointer);
  153. this.addShape(sPointer);
  154. });
  155. var msPointer = new Pointer( 10, 2, 80, 0, new kity.Color('gray'));
  156. var msMeter = new kity.Group().pipe(function() {
  157. this.addShape(new kity.Circle(90).pipe(function() {
  158. this.stroke(palette.get('inside'), 5);
  159. }));
  160. this.addShape(msPointer);
  161. this.addShape(new kity.Circle(8).fill(new kity.Color('gray')));
  162. this.rotate(-90);
  163. this.translate(0, 250);
  164. });
  165. var mmPointer = new Pointer( 4, 1, 40, 0, new kity.Color('gray'));
  166. var mmMeter = new kity.Group().pipe(function() {
  167. this.addShape(new kity.Circle(50).pipe(function() {
  168. this.stroke(palette.get('inside'), 3);
  169. }));
  170. this.addShape(mmPointer);
  171. this.addShape(new kity.Circle(6).fill(new kity.Color('gray')));
  172. this.rotate(90);
  173. var pos = kity.Point.fromPolar(250, 50);
  174. this.translate(pos.x, pos.y);
  175. });
  176. paper.addShape(msMeter);
  177. paper.addShape(mmMeter);
  178. paper.addShape(pointers);
  179. function updatePointers(frame) {
  180. var time = new Date(),
  181. h = time.getHours(),
  182. m = time.getMinutes(),
  183. s = time.getSeconds(),
  184. ms = time % 1000,
  185. mm = time % 500;
  186. m += s / 60;
  187. h += m / 60;
  188. sPointer.setRotate(s * 6);
  189. mPointer.setRotate(m * 6);
  190. hPointer.setRotate(h * 30);
  191. msPointer.setRotate(ms * 360 / 1000);
  192. mmPointer.setRotate(mm * 360 / 500);
  193. frame.next();
  194. }
  195. kity.requestFrame(updatePointers);
  196. </script>
  197. </html>