js画图开发库--mxgraph--[fixedicon-固定图标.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">
// 覆盖的元素重绘方法放置图标
mxLabel.prototype.redraw = function()
{
var isSvg = (this.dialect == mxConstants.DIALECT_SVG);
var isVml = mxUtils.isVml(this.node);
// 更新的最外层的边框
if (isSvg)
{
this.updateSvgShape(this.innerNode);
if (this.shadowNode != null)
{
this.updateSvgShape(this.shadowNode);
}
this.updateSvgGlassPane();
}
else if (isVml)
{
this.updateVmlShape(this.node);
this.updateVmlShape(this.rectNode);
this.label.style.width = this.node.style.width;
this.label.style.height = this.node.style.height;
this.updateVmlGlassPane();
}
else
{
this.updateHtmlShape(this.node);
}
// 更新图像的宽度和图像高度
var imageWidth = 0;
var imageHeight = 0;
if (this.imageNode != null)
{
imageWidth = (this.style[mxConstants.STYLE_IMAGE_WIDTH] ||
this.imageSize) * this.scale;
imageHeight = (this.style[mxConstants.STYLE_IMAGE_HEIGHT] ||
this.imageSize) * this.scale;
// 放置图标
var x = (this.bounds.width - imageWidth) / 2;
var y = this.bounds.height - imageHeight;
if (isSvg)
{
this.imageNode.setAttribute('x', (this.bounds.x + x) + 'px');
this.imageNode.setAttribute('y', (this.bounds.y + y) + 'px');
this.imageNode.setAttribute('width', imageWidth + 'px');
this.imageNode.setAttribute('height', imageHeight + 'px');
}
else
{
this.imageNode.style.position = 'relative';
this.imageNode.style.left = x + 'px';
this.imageNode.style.top = y + 'px';
this.imageNode.style.width = imageWidth + 'px';
this.imageNode.style.height = imageHeight + 'px';
this.imageNode.setAttribute('stroked', 'false');
}
}
};
// 程序在此启动
function main(container)
{
// 检测浏览器兼容性
if (!mxClient.isBrowserSupported())
{
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// 去锯齿效果
mxLabel.prototype.crisp = true;
// 使阴影变亮
mxConstants.SHADOWCOLOR = '#C0C0C0';
// 在容器中创建图形
var graph = new mxGraph(container);
// 可设置元素为自适应大小
//graph.setResizeContainer(true);
// 启用默认下拉列表
new mxRubberband(graph);
// 创建一个默认窗体
var parent = graph.getDefaultParent();
// 更新事务开启
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Fixed icon', 20, 20, 80, 50,
'shape=label;image=images/plus.png;imageWidth=16;imageHeight=16;spacingBottom=10;' +
'fillColor=#adc5ff;gradientColor=#7d85df;glass=1;rounded=1;shadow=1;');
}
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>