简体   繁体   English

在setInterval函数内部具有mousemove函数的SVG动画

[英]SVG animation with mousemove function inside setInterval function

I am trying to create something with SVG that responds to mouse movement combined with random movement. 我正在尝试使用SVG创建一些响应鼠标移动与随机移动相结合的东西。

you can check the code here 你可以在这里检查代码
https://jsfiddle.net/hass/Lfv2yjyf/ https://jsfiddle.net/hass/Lfv2yjyf/

$(document).ready(function(){
    var d = document;
    var mysvg = d.getElementById("mysvg");

    var mx,my,random,xmid,ymid,input;

    setInterval(function() {
        //svg size
        var svgw = $("svg").width();
        var svgh = $("svg").height();

        //center point of svg
        xmid = svgw/2;
        ymid = svgh/2;

        //random numbers
        random = {
            a: Math.floor(Math.random()*25),
            b: Math.floor(Math.random()*25),
            c: Math.floor(Math.random()*25)
        };

        //add event to svg
        mysvg.addEventListener("mousemove", function(e) {
            //aqcuire mouse position relative to svg
            mx = e.clientX;
            my = e.clientY;

            //add <path> to svg
            input = '<path d="M ' + xmid + ',' + ymid + ' l ' + 0 + ',' + 0 + ' ' + ((mx-xmid)/2) + ',' + random.a + ' ' + ((mx-xmid)-((mx-xmid)/2)) + ',' + ((my-ymid)-random.a) + ' ' + '" stroke="orange" stroke-width="7" stroke-linejoin="round" fill="none" />';
        });

        //display datas
        $("#status1").html("X Position : " + mx + "<br>Y Position: " + my + "<br><br>Center Point X: " + xmid + "<br><br>Center Point Y: " + ymid + "<br><br>Width: " + svgw + "<br>Height: " + svgh + "<br><br>Random Value a: " + random.a + "<br>Random Value b: " + random.b + "<br>Random Value c: " + random.c);

        $("#mysvg").html(input);
    }, 10);
});

My problem here is the random movement of the line at midpoint only responds when I move the mouse, I know it doesn't work because the random value is only acquired with mouse movement. 我的问题是线的中点处的随机移动仅在移动鼠标时才响应,我知道这是行不通的,因为随机值只能通过鼠标移动来获取。

what I'm trying to do is I want the random movement even without the mouse movement. 我想要做的是即使没有鼠标移动也希望随机移动。

so I want to know how I acquire the values of global object in setInterval every 10 milliseconds or whatever value I set at "setInterval" function but I prefer almost every millisecond so I could get the random vibrating effect. 因此,我想知道如何每10毫秒或我在“ setInterval”函数中设置的任何值获取setInterval中的全局对象的值,但是我更喜欢几乎每毫秒的时间,以便获得随机振动效果。

I also tried to write the "path" outside of mousemove function and it works and this is something I wanted to achieve 我还尝试在mousemove函数之外编写“路径”,它可以正常工作,这是我想要实现的目标

https://jsfiddle.net/hass/d2L4hda5/ https://jsfiddle.net/hass/d2L4hda5/

but the problem here is when I check the console (browser's development tool → console tab) the values of the mouse x and mouse y is "NaN". 但是这里的问题是,当我检查控制台(浏览器的开发工具→控制台选项卡)时,鼠标x和鼠标y的值为“ NaN”。 but the rendering works. 但渲染有效。

I cannot figure out what I'm missing here. 我无法弄清楚我在这里缺少什么。

so I want to get some advice if second link is what I want to achieve and get the right values of mouse x and mouse y or any other technique that works best. 因此,如果我想实现第二个链接,并希望获得鼠标x和鼠标y或其他效果最佳的技术的正确值,我想获得一些建议。

Regards. 问候。

I'm not entirely sure what you problem is here. 我不确定您的问题在这里。 Is your problem that you can't seem to see the values iof mx and my in your browser tools? 您的问题是您似乎无法在浏览器工具中看到iof mx和my的值吗? How are you trying to read them? 您如何阅读它们? Note that they aren't global variables. 请注意,它们不是全局变量。 They are local to the ready() function. 它们在ready()函数本地。 So, for instance, you won't be able to see the value if you simply type mx into the console. 因此,例如,如果您仅在控制台中键入mx则将看不到该值。

Note also, that you don't need to keep adding the mousemove event handler every time your interval timer function gets called. 还要注意,每次间隔计时器函数被调用时,您不必一直添加mousemove事件处理程序。 It can be totally independent. 它可以是完全独立的。 See below. 见下文。

 $(document).ready(function(){ var d = document; var mysvg = d.getElementById("mysvg"); var mx,my,random,xmid,ymid,input; //svg size var svgw = $("svg").width(); var svgh = $("svg").height(); //center point of svg xmid = svgw/2; ymid = svgh/2; //add event to svg mysvg.addEventListener("mousemove", function(e) { //aqcuire mouse position relative to svg mx = e.clientX; my = e.clientY; }); setInterval(function() { //random numbers random = { a: Math.floor(Math.random()*25), b: Math.floor(Math.random()*25), c: Math.floor(Math.random()*25) }; //add <path> to svg input = '<path d="M ' + xmid + ',' + ymid + ' l ' + 0 + ',' + 0 + ' ' + ((mx-xmid)/2) + ',' + random.a + ' ' + ((mx-xmid)-((mx-xmid)/2)) + ',' + ((my-ymid)-random.a) + ' ' + '" stroke="orange" stroke-width="7" stroke-linejoin="round" fill="none" />'; //display datas $("#status1").html("X Position : " + mx + "<br>Y Position: " + my + "<br><br>Center Point X: " + xmid + "<br><br>Center Point Y: " + ymid + "<br><br>Width: " + svgw + "<br>Height: " + svgh + "<br><br>Random Value a: " + random.a + "<br>Random Value b: " + random.b + "<br>Random Value c: " + random.c); $("#mysvg").html(input); }, 10); }); 
 html, body { width: 100%; height: 100%; padding: 0; margin: 0; } body { overflow: hidden; } #wrapper { width: 100%; min-height: 100%; margin: 0 auto; position: relative; } svg { position: absolute; bottom: 0; left: 0; right: 0; margin: 0 auto; width: 100%; height: 100%; outline: 1px solid blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <p id="status1"></p> <svg id="mysvg" width="300" height="300"></svg> </div> 

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

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