在SVG View下整图平移
来源:互联网 发布:数据库外键是什么 编辑:程序博客网 时间:2024/06/06 09:27
今天在网上查询在SVGView下对SVG图形进行平移操作的JS代码时发现了有不少对SVG图形中某些图元进行移动的文档,而没有整图平移的代码。
参考这些代码,自己写了整图平移的代码。
代码如下(记得将下面代码保存成UTF-8编码格式的svg文件):
<?xml version='1.0' standalone='no'?>
<svg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'
onload='Init(evt)' onmousedown='Grab(evt)' onmousemove='Drag(evt)'
onmouseup='Drop(evt)'>
<title>Drag And Drop</title>
<desc>
A nice little demo of drag-and-drop functionality in SVG,
written by Doug Schepers on February 16, 2004. Use or misuse
this code however you wish.
</desc>
<script type="text/javascript"><![CDATA[
var svgdoc = null; // SVG文档对象
var svgroot = null; // SVG文档的根节点
var check = false; // 图像拖动标志
var TrueCoords = null; //
var GrabPoint = null;
var BackDrop = null; // 背景对象
var WholeMap = null; // 整图对象
/**
* @功能: 初始化函数
**/
function Init(evt) {
svgdoc = evt.target.ownerDocument;
svgroot = svgdoc.documentElement;
TrueCoords = svgroot.createSVGPoint();
GrabPoint = svgroot.createSVGPoint();
BackDrop = svgdoc.getElementById('BackDrop');
WholeMap = svgdoc.getElementById('whole');
}
/**
* @功能:处理鼠标落下的事件
* @param evt:事件句柄
**/
function Grab(evt) {
if (evt.button != 0) {
// 非左键不响应
return;
}
var transMatrix = WholeMap.getCTM();
GrabPoint.x = TrueCoords.x - Number(transMatrix.e);
GrabPoint.y = TrueCoords.y - Number(transMatrix.f);
check = true;
// alert(GrabPoint.x+":"+GrabPoint.y);
}
/**
* @功能:处理鼠标移动事件
* @param evt:事件句柄
**/
function Drag(evt) {
GetTrueCoords(evt);
if (check == true) {
var newX = TrueCoords.x - GrabPoint.x;
var newY = TrueCoords.y - GrabPoint.y;
WholeMap.setAttributeNS(null, 'transform', 'translate(' + newX + ','
+ newY + ')');
}
}
/**
* @功能:处理鼠标松开事件
* @param evt:事件句柄
**/
function Drop(evt) {
check = false;
}
/**
* @功能:获取鼠标移动位置
* @param evt:事件句柄
**/
function GetTrueCoords(evt) {
var newScale = svgroot.currentScale;
var translation = svgroot.currentTranslate;
TrueCoords.x = (evt.clientX - translation.x) / newScale;
TrueCoords.y = (evt.clientY - translation.y) / newScale;
};
]]></script>
<rect id='BackDrop' x='-10%' y='-10%' width='110%' height='110%'
fill='none' pointer-events='all' />
<g id="whole">
<circle id='BlueCircle' cx='25' cy='25' r='20' style='fill:blue; ' />
<circle id='RedCircle' cx='125' cy='25' r='20' style='fill:red; ' />
<circle id='OrangeCircle' cx='225' cy='25' r='20'
style='fill:orange; ' />
<text id='DraggableText' x='20' y='200'
style='fill:red; font-size:18px; font-weight:bold;'>
Draggable Text
</text>
<rect id='GreenRectangle' x='50' y='70' width='100' height='100'
style='fill:green; ' />
<g id='Folder'>
<rect id='FolderRectangle' x='300' y='100' width='200'
height='150' style='fill:tan; stroke:brown; stroke-width:3;' />
</g>
</g>
</svg>
- 在SVG View下整图平移
- 如何使用 SVG 进行缩放和平移
- SVG裁剪和平移的顺序
- d3.js 实现svg 缩放 平移 旋转
- svg平移、放大、缩小及js操作svg
- svg平移、放大、缩小及js操作svg
- android 中View的平移
- Android如何平移一个View
- hammer.js操作svg使达到平移,缩放
- view仿射变换 缩放 旋转 平移
- View平移动画的正确写法
- SVG在无线网络中的应用
- 在SVG中使用JavaScript
- 在SVG图里加按钮
- 在HTML中使用SVG
- 在HTML中使用SVG
- 在网页中加载SVG
- 在 HTML5 中使用 SVG
- 我们到底是不是产品经理:给互联网、软件业者
- 系统优化(集成修复防护)软件(Avanquest Fix-It Utilities? 9 Professional)V9.24官方英文注册版(光盘镜像)
- SWT(JFace)体验之快捷键设定
- Span与Div的区别
- 初步搭建yui-ext(Ajax)的开发环境(后台->前台数据交换)
- 在SVG View下整图平移
- 又见地震云(图)
- ArrayList的使用方法
- WebSphere和RemoteObject
- [面试技巧]16个经典面试问题回答思路
- 冯青华的专栏(内核调试)
- 托管与非托管
- C#实现图片的任意四边形变形
- c51 编译时致命的324个错误提醒,与大家分享