js画图

来源:互联网 发布:2017年中国经济知乎 编辑:程序博客网 时间:2024/05/17 08:37

无意间发现的不错的网站http://raphaeljs.com/,做流程设计器感觉还不错。下面是一个简单例子,画两个矩形,进行连接,拖动:

Html代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>线条</title>  
  6. <script type="text/javascript" language="javascript" src="raphael.js"></script>  
  7. <script type="text/javascript" language="javascript" src="jquery-1.4.2.js"></script>  
  8. <script type="text/javascript" language="javascript">  
  9.     //两对象连线方法  
  10.     Raphael.fn.connection = function (obj1, obj2, line, bg) {  
  11.         if (obj1.line && obj1.from && obj1.to) {  
  12.             line = obj1;  
  13.             obj1 = line.from;  
  14.             obj2 = line.to;  
  15.         }  
  16.         var bb1 = obj1.getBBox(),  
  17.             bb2 = obj2.getBBox(),  
  18.             p = [{x: bb1.x + bb1.width / 2, y: bb1.y - 1},  
  19.             {x: bb1.x + bb1.width / 2, y: bb1.y + bb1.height + 1},  
  20.             {x: bb1.x - 1, y: bb1.y + bb1.height / 2},  
  21.             {x: bb1.x + bb1.width + 1, y: bb1.y + bb1.height / 2},  
  22.             {x: bb2.x + bb2.width / 2, y: bb2.y - 1},  
  23.             {x: bb2.x + bb2.width / 2, y: bb2.y + bb2.height + 1},  
  24.             {x: bb2.x - 1, y: bb2.y + bb2.height / 2},  
  25.             {x: bb2.x + bb2.width + 1, y: bb2.y + bb2.height / 2}],  
  26.             d = {}, dis = [];  
  27.         for (var i = 0; i < 4; i++) {  
  28.             for (var j = 4; j < 8; j++) {  
  29.                 var dx = Math.abs(p[i].x - p[j].x),  
  30.                     dy = Math.abs(p[i].y - p[j].y);  
  31.                 if ((i == j - 4) || (((i != 3 && j != 6) || p[i].x < p[j].x) && ((i != 2 && j != 7) || p[i].x > p[j].x) && ((i != 0 && j != 5) || p[i].y > p[j].y) && ((i != 1 && j != 4) || p[i].y < p[j].y))) {  
  32.                     dis.push(dx + dy);  
  33.                     d[dis[dis.length - 1]] = [i, j];  
  34.                 }  
  35.             }  
  36.         }  
  37.         if (dis.length == 0) {  
  38.             var res = [0, 4];  
  39.         } else {  
  40.             res = d[Math.min.apply(Math, dis)];  
  41.         }  
  42.         var x1 = p[res[0]].x,  
  43.             y1 = p[res[0]].y,  
  44.             x4 = p[res[1]].x,  
  45.             y4 = p[res[1]].y;  
  46.         dx = Math.max(Math.abs(x1 - x4) / 2, 10);  
  47.         dy = Math.max(Math.abs(y1 - y4) / 2, 10);  
  48.         var x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3),  
  49.             y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3),  
  50.             x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3),  
  51.             y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3);  
  52.         var path = ["M", x1.toFixed(3), y1.toFixed(3), "C", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(",");  
  53.         if (line && line.line) {  
  54.             line.bg && line.bg.attr({path: path});  
  55.             line.line.attr({path: path});  
  56.         } else {  
  57.             var color = typeof line == "string" ? line : "#000";  
  58.             return {  
  59.                 bg: bg && bg.split && this.path(path).attr({stroke: bg.split("|")[0], fill: "none", "stroke-width": bg.split("|")[1] || 3}),  
  60.                 line: this.path(path).attr({stroke: color, fill: "none"}),  
  61.                 from: obj1,  
  62.                 to: obj2  
  63.             };  
  64.         }  
  65.     };  
  66.   
  67.     //画布  
  68.     var paper;  
  69.     var r_1;//矩形1  
  70.     var r_2;//矩形2  
  71.     var connection;//连线  
  72.       
  73.     var start = function () {  
  74.         thisthis.ox = this.type == "rect" ? this.attr("x") : this.attr("cx");  
  75.         thisthis.oy = this.type == "rect" ? this.attr("y") : this.attr("cy");  
  76.     },  
  77.     move = function (dx, dy) {  
  78.         var att = this.type == "rect" ? {x: this.ox + dx, y: this.oy + dy} : {cx: this.ox + dx, cy: this.oy + dy};  
  79.         this.attr(att);  
  80.         paper.connection(connection);  
  81.         paper.safari();  
  82.     },  
  83.     up = function () {  
  84.           
  85.     };  
  86.   
  87.     //初始化方法   
  88.     window.onload = function(){  
  89.         paper = Raphael("canvasDiv", 659, 309);  
  90.         r_1 = paper.rect(10, 10, 50, 50);  
  91.         r_1.attr({fill: "#99CC33",stroke: "none"});  
  92.         r_2 = paper.rect(140, 10, 50, 50);  
  93.         r_2.attr({fill: "#666666",stroke: "none"});  
  94.         //通过线连接两矩形  
  95.         connection = paper.connection(r_1, r_2, "#000");  
  96.         //实现拖动事件  
  97.         r_1.drag(move, start, up);  
  98.         r_2.drag(move, start, up);  
  99.     }  
  100. </script>  
  101. <style type="text/css">  
  102. <!--  
  103. #canvasDiv {  
  104.     position:absolute;  
  105.     left:114px;  
  106.     top:102px;  
  107.     width:659px;  
  108.     height:309px;  
  109.     z-index:1;  
  110.     background-color: #CCCCCC;  
  111. }  
  112.   
  113. -->  
  114. </style>  
  115. </head>  
  116.   
  117. <body>  
  118. <div id="canvasDiv"></div>  
  119. </body>  
  120. </html> 


0 1