获取在 animateMotion 中设置的路径上移动的移动 SVG 圆的坐标

时间:2021-03-23 15:51:38

标签: javascript html svg

我需要通过跟踪移动对象(在本例中为圆形)上的鼠标移动来检查用户的响应。如果鼠标不在圆上,我需要通过比较鼠标坐标和圆坐标来计算偏移量。

但是每当我检查圆的值时,它们都不会改变,并且会保持它们的初始值。

这是一个简单的例子:

function clickCircle() {
  var circle = document.getElementById("circle");
  console.log('baseVal x: ' + circle.cx.baseVal.value);
  console.log('animVal x: ' + circle.cx.animVal.value);

}
    <p>Click on the moving circle</p>
    <svg width="1200" height="1200"> 
        <circle id="circle" cx="60" cy="60" r="20" fill="green" onclick="clickCircle();"> 
            <animateMotion id="ani" dur="10s" repeatCount="indefinite"
          path="M20, 60 C20, 
                -50 180, 150 180, 
                 60 C180-60 20, 
                 150 20, 60 z" /> 
        </circle> 
    </svg> 

有人知道如何从正在使用 animateMotion 制作动画的移动圆圈获取坐标吗?

1 个答案:

答案 0 :(得分:2)

您可以拖入动画图标,并跟踪其属性

或者使用 JavaScript 计算它的中心 x,y 位置:

#include <unistd.h> 
#include <stdio.h> 
#include <sys/socket.h> 
#include <stdlib.h> 
#include <netinet/in.h> 
#include <string.h> 
#define PORT 7777

int main(int argc, char const *argv[]) 
{ 
    int server_fd, new_socket, valread; 
    struct sockaddr_in6 address; 
    int opt = 1; 
    int addrlen = sizeof(address); 
    char buffer[1024] = {0}; 
    char *hello = "Hello from server"; 
       
    // Creating socket file descriptor 
    if ((server_fd = socket(AF_INET6, SOCK_STREAM, 0)) == 0) 
    { 
        perror("socket failed"); 
        exit(EXIT_FAILURE); 
    } 
       
    // Forcefully attaching socket to the port 8080 
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, 
                                                  &opt, sizeof(opt))) 
    { 
        perror("setsockopt"); 
        exit(EXIT_FAILURE); 
    } 
    address.sin6_family = AF_INET6; 
    address.sin6_addr = in6addr_any; 
    address.sin6_port = htons( PORT ); 

    // Forcefully attaching socket to the port 8080 
    if (bind(server_fd, (struct sockaddr *)&address,  
                                 sizeof(address))<0) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 

    if (listen(server_fd, 3) < 0) 
    { 
        perror("listen"); 
        exit(EXIT_FAILURE); 
    } 

    if ((new_socket = accept(server_fd, (struct sockaddr *)&address,  
                       (socklen_t*)&addrlen))<0) 
    { 
        perror("accept"); 
        exit(EXIT_FAILURE); 
    } 

    valread = read( new_socket , buffer, 1024); 
    printf("%s\n",buffer ); 
    send(new_socket , hello , strlen(hello) , 0 ); 
    printf("Hello message sent\n"); 
    return 0; 
} 

另请阅读:https://schneide.blog/2018/03/05/some-tricks-for-working-with-svg-in-javascript/

注意!下面的这段代码将永远添加圈节点!

#include <stdio.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#include <string.h> 
#define PORT 7777 
   
int main(int argc, char const *argv[]) 
{ 
    int sock = 0, valread; 
    struct sockaddr_in serv_addr; 
    char *hello = "Hello from client"; 
    char buffer[1024] = {0}; 

    if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) < 0) 
    { 
        printf("\n Socket creation error \n"); 
        return -1; 
    } 
   
    serv_addr.sin6_family = AF_INET6; 
    serv_addr.sin6_port = htons(PORT); 

    // Convert IPv4 and IPv6 addresses from text to binary form 
    if(inet_pton(AF_INET6, "fe80::1061:a71b:5d99:c790", &serv_addr.sin6_addr)<=0)  
    { 
        printf("\nInvalid address/ Address not supported \n"); 
        return -1; 
    } 
   
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
    { 
        printf("\nConnection Failed \n"); 
        return -1; 
    } 

    send(sock , hello , strlen(hello) , 0 ); 
    printf("Hello message sent\n"); 
    valread = read( sock , buffer, 1024); 
    printf("%s\n",buffer ); 
    return 0; 
}


或者试试 JSFiddle:https://jsfiddle.net/dannye/ph705b49/

Dan was Authorware toch鞋跟 wat makkelijker... Alles去了?