`
_御少
  • 浏览: 10288 次
社区版块
存档分类
最新评论

SVG实现拖动案例

    博客分类:
  • SVG
 
阅读更多
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 12.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 51448)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_svg "http://www.w3.org/2000/svg">
<!ENTITY ns_xlink "http://www.w3.org/1999/xlink">
]>
<svg onload='Init(evt)'   onmousedown='Grab(evt)'    onmousemove='Drag(evt)'
   onmouseup='Drop(evt)'   version="1.1" xmlns="&ns_svg;" xmlns:xlink="&ns_xlink;">
   <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 SVGDocument = null;
      var SVGRoot = null;
      var TrueCoords = null;
      var GrabPoint = null;
      var BackDrop = null;
      var DragTarget = null;

      function Init(evt)
      {
         SVGDocument = evt.target.ownerDocument;
         SVGRoot = SVGDocument.documentElement;

         // these svg points hold x and y values...
         //    very handy, but they do not display on the screen (just so you know)
         TrueCoords = SVGRoot.createSVGPoint();
         GrabPoint = SVGRoot.createSVGPoint();

         // this will serve as the canvas over which items are dragged.
         //    having the drag events occur on the mousemove over a backdrop
         //    (instead of the dragged element) prevents the dragged element
         //    from being inadvertantly dropped when the mouse is moved rapidly
         BackDrop = SVGDocument.getElementById('BackDrop');
      }

      function Grab(evt)
      {
         // find out which element we moused down on
         var targetElement = evt.target;

         // you cannot drag the background itself, so ignore any attempts to mouse down on it
         if ( BackDrop != targetElement )
         {
            //set the item moused down on as the element to be dragged
            DragTarget = targetElement;

            // move this element to the "top" of the display, so it is (almost)
            //    always over other elements (exception: in this case, elements that are
            //    "in the folder" (children of the folder group) with only maintain
            //    hierarchy within that group
            DragTarget.parentNode.appendChild( DragTarget );

            // turn off all pointer events to the dragged element, this does 2 things:
            //    1) allows us to drag text elements without selecting the text
            //    2) allows us to find out where the dragged element is dropped (see Drop)
            DragTarget.setAttributeNS(null, 'pointer-events', 'none');

            // we need to find the current position and translation of the grabbed element,
            //    so that we only apply the differential between the current location
            //    and the new location
            var transMatrix = DragTarget.getCTM();
            GrabPoint.x = TrueCoords.x - Number(transMatrix.e);
            GrabPoint.y = TrueCoords.y - Number(transMatrix.f);

         }
      };


      function Drag(evt)
      {
         // account for zooming and panning
         GetTrueCoords(evt);

         // if we don't currently have an element in tow, don't do anything
         if (DragTarget)
         {
            // account for the offset between the element's origin and the
            //    exact place we grabbed it... this way, the drag will look more natural
            var newX = TrueCoords.x - GrabPoint.x;
            var newY = TrueCoords.y - GrabPoint.y;

            // apply a new tranform translation to the dragged element, to display
            //    it in its new location
            DragTarget.setAttributeNS(null, 'transform', 'translate(' + newX + ',' + newY + ')');
         }
      };


      function Drop(evt)
      {
         // if we aren't currently dragging an element, don't do anything
         if ( DragTarget )
         {
            // since the element currently being dragged has its pointer-events turned off,
            //    we are afforded the opportunity to find out the element it's being dropped on
            var targetElement = evt.target;

            // turn the pointer-events back on, so we can grab this item later
            DragTarget.setAttributeNS(null, 'pointer-events', 'all');
            if ( 'Folder' == targetElement.parentNode.id )
            {
               // if the dragged element is dropped on an element that is a child
               //    of the folder group, it is inserted as a child of that group
               targetElement.parentNode.appendChild( DragTarget );
             
            }
            else
            {
               // for this example, you cannot drag an item out of the folder once it's in there;
               //    however, you could just as easily do so here
            }

            // set the global variable to null, so nothing will be dragged until we
            //    grab the next element
            DragTarget = null;
         }
      };


      function GetTrueCoords(evt)
      {
         // find the current zoom level and pan setting, and adjust the reported
         //    mouse position accordingly
         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' />

   <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>

</svg>
分享到:
评论

相关推荐

    HTML5基于SVG实现可拖拽和缩放的世界地图效果源码.zip

    在这个“HTML5基于SVG实现可拖拽和缩放的世界地图效果源码”中,我们主要会探讨以下几个关键知识点: 1. SVG的基本概念与应用:SVG是一种XML标记语言,用于描述二维图形和图像。它可以精确地控制图形的每一个细节,...

    svg图形拖拽变大变小旋转.7z

    在这个案例中,jQuery可能被用来处理用户输入事件,如鼠标点击和拖动,然后调用Snap.svg库的方法来改变SVG图形的状态。 4. **Snap.svg**: Snap.svg是一个强大的JavaScript库,专门用于SVG图形操作。它提供了丰富...

    svg 画流程图

    在本文中,我们将深入探讨如何使用SVG技术来绘制流程图,并实现拖拽、修改和保存的功能。 首先,SVG的基本结构是由一系列的形状元素组成的,如`&lt;rect&gt;`(矩形)、`&lt;circle&gt;`(圆形)、`&lt;line&gt;`(线)、`&lt;polygon&gt;`...

    HTML5结合SVG实现的滑块拖动曲线图表特效源码.zip

    在这个特定的案例中,"HTML5结合SVG实现的滑块拖动曲线图表特效源码.zip" 提供了一个利用HTML5与SVG(Scalable Vector Graphics)技术创建的动态图表示例。SVG是一种基于XML的矢量图像格式,它允许在网页上绘制清晰...

    html5+svg交互式3D商场地图代码

    在3D商场地图中,SVG可能被用来创建可缩放的楼层平面图,甚至可以包含3D效果,通过CSS3的变换和投影实现。SVG还支持事件监听,使得用户可以通过点击、拖动等方式与地图进行交互。 CSS(Cascading Style Sheets)在...

    简单实现SVG编辑器.rar

    这个“简单实现SVG编辑器.rar”项目是一个使用原生JavaScript编写的前端应用程序,它提供了对SVG图形的基本编辑功能。通过这个编辑器,用户能够创建和操作SVG中的四种基本图形:矩形(rect)、圆形(circle)、椭圆...

    svg的鼠标拖动气泡滑块动画特效.zip

    在本案例中,"svg的鼠标拖动气泡滑块动画特效"是一个利用SVG结合JavaScript实现的用户交互组件,用户可以通过鼠标拖动滑块来控制气泡的动画效果。 首先,SVG中的基本元素包括路径(path)、圆形(circle)、矩形...

    SVG交互式滑块拖动圆形进度条特效

    在这个特定的案例中,“SVG交互式滑块拖动圆形进度条特效”是一个利用SVG技术和JavaScript实现的网页元素,它可以展示一个动态的、圆形的进度条,并允许用户通过滑块进行交互式操作,改变进度条的值。 首先,让我们...

    HTML5 SVG仿PS拖动裁剪图片代码.zip

    这款"HTML5 SVG仿PS拖动裁剪图片代码"是一个创新的Web应用程序,它模拟了Photoshop中的图片裁剪功能,允许用户通过拖动SVG图形元素来裁剪图片,从而实现自定义的图片区域选择。 在HTML5中,SVG是一种用于创建清晰、...

    HTML5SVG滑块拖动曲线图表代码.zip

    SVG在本案例中被用来绘制曲线图,它使用XML语法,能够直接在HTML文档中嵌入。通过定义路径(`&lt;path&gt;`元素)、矩形(`&lt;rect&gt;`)、圆形(`&lt;circle&gt;`)等形状,以及设置填充色、描边颜色、线条样式等属性,可以构建出...

    html5 SVG 灯泡发光特效.rar

    在这个案例中,SVG的可编程性使得灯泡的亮灭效果可以灵活控制,而且由于SVG是矢量图形,无论屏幕大小如何,灯泡的显示效果都能保持清晰。 总的来说,这个项目展示了HTML5和SVG结合的威力,以及如何通过CSS3来创建...

    HTML5 SVG实现带时间轴风格的闹钟设置效果源码.zip

    SVG(Scalable Vector Graphics)是HTML5中一个重要的图形绘制工具,它可以创建出清晰、可缩放的矢量图形,无论放大多少倍都不会失真...对于想要学习HTML5高级特性和SVG图形编程的开发者来说,这是一个很好的实践案例。

    TweenMax.js+svg实现交互式小狗和碗式橄榄球动画效果源码.zip

    SVG 还支持事件监听器,使得我们可以与这些图形进行交互,比如点击或拖动,从而实现“交互式”的动画效果。 具体实现中,开发者可能使用了以下技术步骤: 1. 创建 SVG 元素:使用 `&lt;svg&gt;` 标签在 HTML 文件中创建...

    HTML5+svg实现的拉弓射箭动画效果源码.zip

    在这个案例中,`&lt;svg&gt;`元素被用来创建拉弓射箭的图形元素,因为它可以提供复杂的形状、路径和动画效果,非常适合制作这种细腻的动作动画。 SVG的优势在于其矢量性质,意味着图形由数学公式定义,而不是像素。因此,...

    TweenMax基于html5 svg实现鼠标滑动切水果游戏动画特效源码.zip

    将TweenMax与SVG结合,可以实现丰富的动画效果,尤其适用于网页游戏和交互式设计。 在这个“TweenMax基于html5 svg实现鼠标滑动切水果游戏动画特效源码”中,我们可以预见到以下几个核心知识点: 1. **SVG图形与...

    最全的SVG例子Demo分享

    9. **交互性**:SVG图形可以与JavaScript结合,实现用户交互,例如响应点击事件、拖动、缩放等。 通过学习和实践这个SVG Demo分享,你可以掌握SVG的基本语法和高级技巧,从而在网页设计或应用开发中充分利用SVG的...

    HTML5SVG滑块拖动曲线图表特效代码

    总的来说,"HTML5SVG滑块拖动曲线图表特效代码"结合了HTML5、SVG和JavaScript的技术,实现了动态且交互性强的曲线图表。它不仅展示了数据可视化的能力,也为开发者提供了一个学习和实践Web前端技术的实例。

    基于HTML5 SVG拖拽的JavaScript库插件源码.zip

    JavaScript库插件通常是为了简化特定任务的编程,比如在这个案例中,是实现SVG元素的拖放行为。这种拖放功能通常涉及到事件监听、坐标处理以及更新SVG元素的位置等复杂逻辑。JavaScript库插件通过封装这些逻辑,使得...

Global site tag (gtag.js) - Google Analytics