代码之家  ›  专栏  ›  技术社区  ›  Budda

如何在web2.0中绘图?

  •  3
  • Budda  · 技术社区  · 14 年前

    谢谢。

    抱歉,如果这个问题有点“愚蠢”,但我不能让它更聪明。

    3 回复  |  直到 8 年前
        1
  •  4
  •   João Louros    14 年前

    你想做什么的一个简单例子:

    <html>
      <head>
        <title>Hexagon canvas tutorial</title>
        <script type="text/javascript">
          function draw(){
            //first let's get canvas HTML Element to draw something on it
            var canvas = document.getElementById('tutorial');
            //then let's see if the browser supports canvas element
            if (canvas.getContext){
              var ctx = canvas.getContext('2d');
    
                //Pick Hexagon color, this one will be blue
                ctx.fillStyle = "rgb(0, 0, 255)";
                //let's start a path
                ctx.beginPath();
                //move cursor to position x=10 and y=60, and move it around to create an hexagon
                ctx.moveTo(10,60);
                ctx.lineTo(40,100);
                ctx.lineTo(80,100);
                ctx.lineTo(110,60);
                ctx.lineTo(80,20);
                ctx.lineTo(40,20);
                //fill it and you got your first Hexagon
                ctx.fill();
    
                //This one will be green, but we will draw it like the first one
                ctx.fillStyle = "rgb(0, 255, 0)";
                ctx.beginPath();
                ctx.moveTo(110,160);
                ctx.lineTo(140,200);
                ctx.lineTo(180,200);
                ctx.lineTo(210,160);
                ctx.lineTo(180,120);
                ctx.lineTo(140,120);
                ctx.fill();
            }
          }
        </script>
        <style type="text/css">
          canvas { border: 1px solid black; }
        </style>
      </head>
      <body onload="draw();">
        <canvas id="tutorial" width="300" height="300"></canvas>
      </body>
    </html>
    
        2
  •  4
  •   David Hedlund    14 年前
        3
  •  1
  •   mkoistinen    14 年前

    我怀疑你听说过画布元素。您可以从这里开始: http://en.wikipedia.org/wiki/Canvas_element

    祝你好运!