天天看點

js畫圖開發庫--mxgraph--[perimeter-邊界.html ]

 js畫圖開發庫--mxgraph--[perimeter-邊界.html ] 

<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
	<head>
	<meta http-equiv=Content-Type content="text/html;charset=utf-8">
	<title>周長</title>

	<!-- 如果本檔案的包與src不是在同一個目錄,就要将basepath設定到src目錄下 -->
	<script type="text/javascript">
		mxBasePath = '../src';
	</script>

	<!-- 引入支援庫檔案 -->
	<script type="text/javascript" src="../src/js/mxClient.js"></script>
	
	<!-- 示例代碼 -->
	<script type="text/javascript">
		//  程式在此方法中啟動  
		function main(container)
		{
			// 檢查浏覽器支援
			if (!mxClient.isBrowserSupported())
			{
				mxUtils.error('Browser is not supported!', 200, false);
			}
			else
			{
				// 去鋸齒效果 in SVG
				mxRectangleShape.prototype.crisp = true;
				
				// 重定向周邊的标簽界限邊緣和标簽之間的交點
				mxGraphViewGetPerimeterPoint = mxGraphView.prototype.getPerimeterPoint;
				mxGraphView.prototype.getPerimeterPoint = function(terminal, next, orthogonal, border)
				{
					var point = mxGraphViewGetPerimeterPoint.apply(this, arguments);
					
					if (point != null)
					{
						var perimeter = this.getPerimeterFunction(terminal);

						if (terminal.text != null && terminal.text.boundingBox != null)
						{
							// 添加标簽的邊界
							var b = terminal.text.boundingBox.clone();
							b.grow(3)

							if (mxUtils.rectangleIntersectsSegment(b, point, next))
							{
								point = perimeter(b, terminal, next, orthogonal);
							}
						}
					}
					
					return point;
				};
				
				// 在容器中建立圖形
				var graph = new mxGraph(container);
				graph.setVertexLabelsMovable(true);
				graph.setConnectable(true);

				// 容器大小自動變化
				//graph.setResizeContainer(true);
				
				// 浏覽器預設右鍵菜單
				new mxRubberband(graph);
				
				// 建立預設窗體
				var parent = graph.getDefaultParent();
								
				// 開啟更新事務
				graph.getModel().beginUpdate();
				try
				{
					var v1 = graph.insertVertex(parent, null, 'Label', 20, 20, 80, 30, 'verticalLabelPosition=bottom');
					var v2 = graph.insertVertex(parent, null, 'Label', 200, 20, 80, 30, 'verticalLabelPosition=bottom');
					var v3 = graph.insertVertex(parent, null, 'Label', 20, 150, 80, 30, 'verticalLabelPosition=bottom');
					var e1 = graph.insertEdge(parent, null, '', v1, v2);
					var e1 = graph.insertEdge(parent, null, '', v1, v3);
				}
				finally
				{
					// 結束更新事務
					graph.getModel().endUpdate();
				}
			}
		};
	</script>
</head>

<!-- 頁面載入時啟動程式 -->
<body οnlοad="main(document.getElementById('graphContainer'))">

	<!-- 建立帶網格桌面和曲線的一個容器  -->
	<div id="graphContainer"
		style="overflow:hidden;position:relative;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;">
	</div>
</body>
</html>