index.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- 必须强制指定页面编码为 UTF-8 -->
  5. <meta charset="utf-8">
  6. <!-- Demo 的简要说明,必须要填写 -->
  7. <meta name="description" content="百度脑图的原型 Demo">
  8. <!-- Demo 的作者,建议填写 -->
  9. <meta name="author" content="kity@baidu.com">
  10. <!-- Demo 的标题,必须填写 -->
  11. <title>Graffle</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. </script>
  20. <!-- dev end -->
  21. <!-- 生产使用中可以直接引用发布压缩的版本 -->
  22. <!-- production start -->
  23. <!-- 目录型的 Demo 注意修正源码引用路径 -->
  24. <!-- <script src="../dist/kity.min.js"></script> -->
  25. <!-- production end -->
  26. <script>
  27. define('demo', function (require) {
  28. var Draggable = require('../demo/public/draggable');
  29. var Paper = require('core/class').extendClass( require('graphic/paper'), Draggable.prototype );
  30. var Rect = require('graphic/rect');
  31. var BezierConnection = require('../demo/graffle/bezierconnection');
  32. var PatternBrush = require('graphic/pattern');
  33. var Group = require('graphic/group');
  34. var Ellipse = require('graphic/ellipse');
  35. var Color = require('graphic/color');
  36. var Palette = require('graphic/palette');
  37. var utils = require('core/utils');
  38. var createClass = require('core/class').createClass;
  39. var MindRect = createClass( {
  40. base: Rect,
  41. mixins: [Draggable],
  42. getCenter: function() {
  43. return {
  44. x: this.getX(),
  45. y: this.getY()
  46. };
  47. },
  48. setCenterX: function(x) {
  49. return this.setPositionX( x - this.getWidth() / 2 );
  50. },
  51. setCenterY: function(y) {
  52. return this.setPositionY( y - this.getHeight() / 2 );
  53. },
  54. getCenterX: function() {
  55. return this.getPositionX() + this.getWidth() / 2;
  56. },
  57. getCenterY: function() {
  58. return this.getPositionY() + this.getHeight() / 2;
  59. },
  60. dragStart: function() {
  61. this.dragStartPos = this.getPosition();
  62. },
  63. dragMove: function( e ) {
  64. var pos = this.dragStartPos, delta = e.movement;
  65. this.setPosition(pos.x + delta.x, pos.y + delta.y);
  66. }
  67. } );
  68. var MindContainer = createClass({
  69. base: Group,
  70. constructor: function( x, y, width, height ) {
  71. this.callBase();
  72. this.x = x || 0;
  73. this.y = y || 0;
  74. this.width = width || 1000;
  75. this.height = height || 1000;
  76. this.palette = new Palette().pipe(function() {
  77. var fill = Color.createHSL(0, 75, 60);
  78. this.add('level0-fill', fill);
  79. this.add('level0-stroke', fill.dec(Color.L, 30));
  80. this.add('level1-fill', fill.set(Color.H, 200));
  81. this.add('level1-stroke', fill.dec(Color.L, 30));
  82. this.add('fill', Color.createHSL(200, 50, 90));
  83. this.add('stroke', Color.createHSL(200, 0, 30));
  84. this.add('connection', Color.createHSL(60, 75, 75));
  85. });
  86. this.gaps = 50;
  87. this.mindTree = this.generateMindObject();
  88. },
  89. generateMindObject: function( prevLevelObject ) {
  90. var levelObject, connection;
  91. var level = prevLevelObject ? prevLevelObject.level + 1 : 0;
  92. levelObject = this.generateLevelObject( level );
  93. levelObject.drag();
  94. levelObject.level = level;
  95. levelObject.children = [];
  96. this.addShape( levelObject );
  97. if( prevLevelObject ) {
  98. this.addChildTo( levelObject, prevLevelObject );
  99. levelObject.parent = prevLevelObject;
  100. connection = new BezierConnection(levelObject, prevLevelObject);
  101. connection.stroke(this.palette.get('connection'));
  102. this.addShape( connection );
  103. }
  104. var me = this;
  105. levelObject.on('mousedown', function() {
  106. me.select(levelObject);
  107. });
  108. return levelObject;
  109. },
  110. addChildTo: function( levelObject, prevLevelObject ) {
  111. var yValues = [];
  112. var delta = levelObject.getHeight() + this.gaps;
  113. var me = this;
  114. levelObject.setCenterX( prevLevelObject.getCenterX() + levelObject.getWidth() + 200 );
  115. if(prevLevelObject.children.length == 0) {
  116. levelObject.setCenterY( prevLevelObject.getCenterY() );
  117. prevLevelObject.children.push(levelObject);
  118. return;
  119. }
  120. utils.each(prevLevelObject.children, function(child) {
  121. yValues.push(child.getCenterY());
  122. });
  123. var levelValue = Math.max.apply(Math, yValues) + delta;
  124. levelObject.setCenterY( levelValue );
  125. yValues.push(levelValue);
  126. prevLevelObject.children.push(levelObject);
  127. utils.each(prevLevelObject.children, function(child, index) {
  128. me.updateTreePosition( child, delta / 2 );
  129. });
  130. },
  131. updateTreePosition: function ( levelObject, dy ) {
  132. levelObject.setCenterY(levelObject.getCenterY() - dy);
  133. var me = this;
  134. utils.each( levelObject.children, function (child) {
  135. me.updateTreePosition( child, dy );
  136. } );
  137. },
  138. generateLevelObject: function(level) {
  139. switch(level) {
  140. case 0:
  141. return new MindRect(200, 200, 0, -100, 10)
  142. .fill(this.palette.get('level0-fill'));
  143. case 1:
  144. return new MindRect(200, 100, 0, 0, 5)
  145. .fill(this.palette.get('level1-fill'));
  146. default:
  147. return new MindRect(200, 80, 0, 0, 3)
  148. .fill(this.palette.get('fill'));
  149. }
  150. },
  151. select: function (levelObject) {
  152. if(this.selected) {
  153. this.selected.stroke('none');
  154. }
  155. this.selected = levelObject.stroke('yellow', 5);
  156. },
  157. nextObject: function () {
  158. if(this.selected && this.selected.parent) {
  159. var nextObject = this.generateMindObject(this.selected.parent);
  160. this.select(nextObject);
  161. return nextObject;
  162. }
  163. },
  164. nextLevel: function () {
  165. if(this.selected) {
  166. var nextLevel = this.generateMindObject(this.selected);
  167. this.select(nextLevel);
  168. return nextLevel;
  169. }
  170. }
  171. });
  172. var paper = new Paper(document.body);
  173. paper.setViewBox(-1000, -600, 2000, 1200);
  174. paper.drag();
  175. var mc = new MindContainer(-1000, -600, 2000, 1200);
  176. paper.addShape(mc);
  177. document.body.addEventListener('keydown', function(e) {
  178. switch(e.keyCode) {
  179. case 13:
  180. e.preventDefault();
  181. return mc.nextObject();
  182. case 9:
  183. e.preventDefault();
  184. return mc.nextLevel();
  185. }
  186. });
  187. document.body.addEventListener('mousewheel', function(e) {
  188. var viewport = paper.getViewPort();
  189. if(e.wheelDelta > 0) {
  190. viewport.zoom = viewport.zoom * 0.95;
  191. } else {
  192. viewport.zoom = viewport.zoom / 0.95;
  193. }
  194. paper.setViewPort(viewport);
  195. });
  196. });
  197. </script>
  198. <style>
  199. body, div, html {
  200. margin: 0;
  201. padding: 0;
  202. overflow: hidden;
  203. background: #333;
  204. height: 100%;
  205. }
  206. </style>
  207. </head>
  208. <body></body>
  209. <script>
  210. seajs.use('demo');
  211. </script>
  212. </html>