首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript中的动态Word Web

问Javascript中的动态Word Web
EN

Stack Overflow用户
提问于 2017-04-02 03:29:52
回答 1查看 149关注 0票数 0

我正在尝试设计一个类似于here的动画word网页。我们的目标是有一个固定在一个位置的中心单词气泡,并让其他单词气泡在它周围移动。

我希望使用chartjs中的气泡图作为基础,并删除图例、网格线、轴等来获得气泡。但是,除了文档中包含的工具提示之外,我还没有找到将文本添加到其中一个气泡的方法。有没有办法在气泡内添加文本,使其始终可见?或者,有没有人能给我指出一个更好的库来实现这一点?

EN

回答 1

Stack Overflow用户

发布于 2017-04-02 04:56:19

我是疯了,但我把它写在画布上了。无聊了哈哈。通过粘贴到一个新的html文件来测试它:D你可以玩参数,颜色,形状和新文本的新气泡。玩得开心!

代码语言:javascript
复制
    <!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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43161101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档