简体   繁体   English

使用Javascript和JQuery对svg元素进行动画处理

[英]Animate svg elements with Javascript and JQuery

I'm trying to animate the following html elements to implement a functionality similar to a volume wheel. 我正在尝试为以下html元素设置动画,以实现类似于音量滚轮的功能。

 <svg id="circle_svg" width="200" height="175"> <circle cx="100" cy="85" r="75" stroke="black" stroke-width="2" fill="lightgray"/> <line id="line_alpha" x1="100" y1="85" x2="100" y2="160" style="stroke:rgb(0,0,255);stroke-width:2"/> <circle id="dot_alpha" cx="100" cy="160" r="10" stroke="black" stroke-width="2" fill="red"/> </svg> 

The basic idea is that clicking on the red dot and moving the mouse around should result in the following behavior: 基本思想是,单击红点并四处移动鼠标将导致以下行为:

  1. The red dot moves along the circle (even if mouse doesn't stay exactly on it). 红点沿圆圈移动(即使鼠标没有完全停留在圆圈上)。
  2. The end point of the line on the circle follows the red dot. 圆上线的终点跟随红点。
  3. A number shown somewhere else in the page gets incremented or decremented with the amount of angular displacement. 页面其他位置显示的数字随角度位移量的增加或减少。

I found a demo online that allows to drag an svg circle all around the page, by binding the elements of interest to mousedown and mouseup events and rewriting the attribute cx and cy of the circle to the current location of the mouse. 我在网上找到了一个演示 ,通过将感兴趣的元素绑定到mousedownmouseup事件,并将该圈子的属性cxcy重写为鼠标的当前位置,可以在整个页面上拖动一个svg圈子。

However when testing the code on jsfiddle with my example (or even with the original code) something is not working. 但是,当使用我的示例(甚至使用原始代码)在jsfiddle上测试代码时,某些方法无法正常工作。 Could you please take a look and give me advice on what might be going wrong? 您能否看一下并给我一些可能出问题的建议?

I was able to find the solution to my question (thanks to a friend) and will post it as reference for others: 我能够找到问题的解决方案(感谢一位朋友),并将其发布为他人参考:

The main problem with pasting the code from this online demo into jsfiddle is that the order in which JavaScript libraries and functions is not predictable. 将此在线演示中的代码粘贴到jsfiddle的主要问题是JavaScript库和函数的顺序不可预测。

So some binding might be called before the binded function is defined. 因此,在定义绑定函数之前,可能会调用某些绑定。 Also, the code from the demo is more complicated that what I needed. 另外,演示中的代码比我需要的还要复杂。

  • Here is the code solution on jsfiddle 这是jsfiddle上的代码解决方案
  • Below is a working snippet for SO site 以下是适用于SO网站的代码段

 var dragging = false var updateGraphics = function (e) { if (dragging) { var parentOffset = $('#wheel').offset(); var relX = e.pageX - parentOffset.left; var relY = e.pageY - parentOffset.top; var cx = +$('#circle').attr('cx') var cy = +$('#circle').attr('cy') var r = +$('#circle').attr('r') var dx = relX - cx var dy = relY - cy //var dx = e.clientX - cx //var dy = e.clientY - cy console.debug('cx: ' + cx); console.debug('cy: ' + cy); console.debug('dx: ' + dx); console.debug('dy: ' + dy); console.debug('clientX: ' + e.clientX); console.debug('clientY: ' + e.clientY); console.debug('relX: ' + relX); console.debug('relY: ' + relY); var a = Math.atan2(dy, dx) var dotX = cx + r * Math.cos(a) var dotY = cy + r * Math.sin(a) $('#dot').attr('cx', dotX); $('#dot').attr('cy', dotY); $('#line_2').attr('x2', dotX); $('#line_2').attr('y2', dotY); } } $('svg').on('mousedown', function (e) { dragging = true updateGraphics(e) }) $('svg').on('mouseup', function (e) { dragging = false }) $('svg').on('mousemove', updateGraphics) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg id="wheel" width="200" height="175" style="background-color:lightgreen;"> <circle id="circle" cx="100" cy="85" r="75" stroke="black" stroke-width="2" fill="lightgray"/> <line id="line_1" x1="100" y1="85" x2="100" y2="160" stroke-dasharray="15,15" style="stroke:rgb(255,0,0);stroke-width:2"/> <line id="line_2" x1="100" y1="85" x2="100" y2="160" style="stroke:rgb(0,0,255);stroke-width:2"/> <circle id="dot" cx="100" cy="160" r="10" stroke="black" stroke-width="2" fill="red"/> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM