Javascript中的动画Word Web

时间:2017-04-01 19:29:52

标签: javascript chart.js

我正在尝试设计一个类似于找到here的动画文字网。目标是在一个位置固定一个中心文字泡泡,并在其周围留下其他文字泡泡。

我希望使用chartjs中的气泡图作为基础并删除图例,网格线,轴线等来获取气泡。但是除了文档中包含的工具提示之外,我还没有找到一种方法来向其中一个气泡添加文本。有没有办法在泡泡中添加文字,以便始终可见?或者,任何人都可以指向一个更好的图书馆的方向来实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

我很疯狂,但我在Canvas中编码。哈哈很无聊。通过粘贴到新的html文件来测试它:D您可以使用新文本来播放参数,颜色,形状和新气泡。玩得开心!

    <!DOCTYPE html>
    <html>

    <head>
      <title></title>

    </head>

<body>
    <canvas id="canvas"></canvas>

  <script>
      var canvas = document.getElementById("canvas");
      var ctx = canvas.getContext("2d");
      canvas.width = 400;
      canvas.height = 400;  
      var r = 0;


      var bubbles = [];

      bubbles.push(new Bubble(canvas.width/2, canvas.height/2, "Apple", "rgb(0,0,120)", 30, 20));
        bubbles.push(new Bubble(canvas.width/2 + 100, canvas.height/2, "Store", "rgb(0,0,200)", 30, 20));
         bubbles.push(new Bubble(canvas.width/2 - 100, canvas.height/2, "Ipad", "rgb(0,0,200)", 30, 20));
          bubbles.push(new Bubble(canvas.width/2, canvas.height/2 + 100, "iMac", "rgb(120,0,0)", 30, 20));



      setInterval(function(){update();}, 20);

      function update()
      {
          ctx.clearRect(0,0,canvas.width, canvas.height);
          for (var i=bubbles.length-1; i>=0; i--)
          {
              bubbles[i].update();

          }
      }

    function Vector(x,y)
    {
        this.x = x;
        this.y = y;

        this.startX = x;
        this.startY = y;

        this.add = function (v)
        {
            this.x += v.x;
            this.y += v.y;
        }

        this.mult = function(p)
        {
            this.x *= p;
            this.y *= p;
        }

         this.rotate = function(ang)
         {
        this.x = (this.startX - bubbles[0].position.x) * Math.cos(ang) 
                        - (this.startY - bubbles[0].position.y) * Math.sin(ang) 
                        + bubbles[0].position.x;

        this.y = (this.startX - bubbles[0].position.x) * Math.sin(ang) 
                        + (this.startY - bubbles[0].position.y) * Math.cos(ang) 
                        + bubbles[0].position.y;
         }

    }


    function Bubble(x,y,text, color, width, height)
    {
        this.position = new Vector(x,y);
        this.velocity = new Vector(0,0);
        this.acceleration = new Vector(0,0);

        this.text = text;
        this.color = color;

        this.width = width;
        this.height = height;

        this.draw = function()
        {   ctx.beginPath();

                if(this.text != "Apple")
                {
                    ctx.lineTo(bubbles[0].position.x, bubbles[0].position.y);
                }
             ctx.font = "bold 13pt Arial";
            ctx.fillStyle = this.color;
            ctx.strokeStyle = this.color;
              ctx.arc(this.position.x,this.position.y, this.width, 0, 2 * Math.PI, false);
               ctx.fill();
                ctx.fillStyle = "black";


              ctx.stroke();
                ctx.fillText(this.text, this.position.x - this.width / 2 - 6, this.position.y + this.height / 4);
        }

        this.update = function()
        {
              if(this.text != "Apple")
             {
                 r--;
                 if (r < -1440)
                 r = 1;

                this.position.rotate( Math.PI / 720 * r);



             }
              this.draw();
        }
    }   







  </script>
</body>

</html>