SVG PacMan 中的鬼眼在 Firefox 中正确呈现,但在其他浏览器中无法正确呈现 [英] Ghost eyes in SVG PacMan are rendered correctly in Firefox, but not in other browsers

查看:40
本文介绍了SVG PacMan 中的鬼眼在 Firefox 中正确呈现,但在其他浏览器中无法正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 SVG PacMan 的 GitHub 存储库可在此处获得:https://github.com/FlatAssembler/SVG-吃豆子

The GitHub repository of my SVG PacMan is available here: https://github.com/FlatAssembler/SVG-Pacman

你可以在这里看到它:http://flatassembler.github.io/pacman.html

绘制鬼魂的功能在这里:

The function for drawing ghosts is here:

      function drawGhost(x, y, color, id, transparent) {
        //Duhovi su geometrijski likovi omedeni crtama (dno) i kubicnom Bezierovom krivuljom (vrh).
        if (/Firefox/.test(navigator.userAgent)) {
          var svg = document.createElementNS(XML_namespace_of_SVG, "svg");
          svg.setAttribute("x", x - 8);
          svg.setAttribute("y", y - 16);
          var path = document.createElementNS(XML_namespace_of_SVG, "path");
          path.setAttribute("fill", color);
          var d = "M " + 0 + " " + (16 + 8);
          d +=
            "C " +
            3 +
            " " +
            0 +
            " " +
            (8 + 5) +
            " " +
            0 +
            " " +
            (8 + 8) +
            " " +
            (16 + 8);
          d += " l -4 -3 l -4 3 l -4 -3 Z";
          path.setAttribute("d", d);
          svg.setAttribute("id", id);
          if (transparent) svg.setAttribute("fill-opacity", 0.5); //Siluete (bijeli duhovi).
          svg.appendChild(path);
          var left_eye = document.createElementNS(
            XML_namespace_of_SVG,
            "circle"
          );
          left_eye.setAttribute("cx", 5);
          left_eye.setAttribute("cy", 15);
          left_eye.setAttribute("r", 2);
          left_eye.setAttribute("fill", "black");
          svg.appendChild(left_eye);
          var right_eye = document.createElementNS(
            XML_namespace_of_SVG,
            "circle"
          );
          right_eye.setAttribute("cx", 11);
          right_eye.setAttribute("cy", 15);
          right_eye.setAttribute("r", 2);
          right_eye.setAttribute("fill", "black");
          svg.appendChild(right_eye);
          zaslon.appendChild(svg);
        } else {
          var path = document.createElementNS(XML_namespace_of_SVG, "path");
          path.setAttribute("fill", color);
          var d = "M " + (x - 8) + " " + (y + 8);
          d +=
            "C " +
            (x - 5) +
            " " +
            (y - 16) +
            " " +
            (x + 5) +
            " " +
            (y - 16) +
            " " +
            (x + 8) +
            " " +
            (y + 8);
          d += " l -4 -3 l -4 3 l -4 -3 Z";
          path.setAttribute("d", d);
          path.setAttribute("id", id);
          if (transparent) path.setAttribute("fill-opacity", 0.5); //Siluete (bijeli duhovi).
          zaslon.appendChild(path);
        }
      }

如果我在所有浏览器中都启用了鬼魂的眼睛,在 Firefox 以外的浏览器中,鬼魂不再动画.我用来制作鬼魂动画的代码如下:

If I enable the eyes of the ghosts to be rendered in all browsers, in browsers other than Firefox the ghosts are no longer animated. The code I use for animating the ghosts is as follows:

          for (var i = 0; i < 3; i++) {
            if (
              jeLiPacmanPojeoDuha[i] &&
              brojacGlavnePetlje - kadaJePacmanPojeoVelikuTocku < 30
            )
              //Ako je PacMan nedavno pojeo duha, animiraj bijelu siluetu...
              zaslon
                .getElementById("bijeli" + (i + 1))
                .setAttribute(
                  "transform",
                  "translate(" +
                    (20 / 5) *
                      brojacAnimacijskePetlje *
                      xKomponentaSmjeraPacmana[smjerKretanjaSiluete[i]] +
                    " " +
                    (20 / 5) *
                      brojacAnimacijskePetlje *
                      yKomponentaSmjeraPacmana[smjerKretanjaSiluete[i]] +
                    ")"
                );
            //... inace animiraj duha.
            else
              zaslon
                .getElementById("duh" + (i + 1))
                .setAttribute(
                  "transform",
                  "translate(" +
                    (20 / 5) *
                      brojacAnimacijskePetlje *
                      xKomponentaSmjeraPacmana[smjerDuha[i]] +
                    " " +
                    (20 / 5) *
                      brojacAnimacijskePetlje *
                      yKomponentaSmjeraPacmana[smjerDuha[i]] +
                    ")"
                );
         }

就好像 SVG 转换在 Firefox 以外的浏览器中对 元素本身没有影响.我该怎么办?

It is as if SVG transforms, in browsers other than Firefox, have no effect on <svg> elements themselves. What should I do?

推荐答案

自己解决了,这里是我修改drawGhost函数的方法:

Solved it myself, here is how I modified the drawGhost function:

        function drawGhost(x, y, color, id, transparent) {
            //Duhovi su geometrijski likovi omedeni crtama (dno) i kubicnom Bezierovom krivuljom (vrh).
            var svg = document.createElementNS(XML_namespace_of_SVG, "g");
            svg.setAttribute("x", x - 8);
            svg.setAttribute("y", y - 16);
            var path = document.createElementNS(XML_namespace_of_SVG, "path");
            path.setAttribute("fill", color);
            var d = "M " + (x - 8) + " " + (y + 8);
            d +=
                    "C " +
                    (x - 5) +
                    " " +
                    (y - 16) +
                    " " +
                    (x + 5) +
                    " " +
                    (y - 16) +
                    " " +
                    (x + 8) +
                    " " +
                    (y + 8);
            d += " l -4 -3 l -4 3 l -4 -3 Z";
            path.setAttribute("d", d);
            svg.setAttribute("id", id);
            if (transparent)
                svg.setAttribute("fill-opacity", 0.5); //Siluete (bijeli duhovi).
            svg.appendChild(path);
            var left_eye = document.createElementNS(
                    XML_namespace_of_SVG,
                    "circle"
                    );
            left_eye.setAttribute("cx", x - 8 + 5);
            left_eye.setAttribute("cy", y - 16 + 15);
            left_eye.setAttribute("r", 2);
            left_eye.setAttribute("fill", "black");
            svg.appendChild(left_eye);
            var right_eye = document.createElementNS(
                    XML_namespace_of_SVG,
                    "circle"
                    );
            right_eye.setAttribute("cx", x - 8 + 11);
            right_eye.setAttribute("cy", y - 16 + 15);
            right_eye.setAttribute("r", 2);
            right_eye.setAttribute("fill", "black");
            svg.appendChild(right_eye);
            zaslon.appendChild(svg);
        }

这篇关于SVG PacMan 中的鬼眼在 Firefox 中正确呈现,但在其他浏览器中无法正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆