能否用一个stroke()函数,画出两种颜色的直线?貌似stroke函数只对最后一个strokeStyle设置的颜色起作用?多谢各位了

解决方案 »

  1.   

    <!DOCTYPE html> <html>
    <head>
      <script type="text/javascript">
    function draw()
    {
      var canvas = document.getElementById("MyCanvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
          ctx.beginPath();
          ctx.strokeStyle="green";  // Use strokeStyle to set the color.
          ctx.rect (5,5,300,250); 
          ctx.stroke();
          ctx.beginPath();
          ctx.strokeStyle="blue";  // Use strokeStyle to change the color.
          ctx.lineWidth = "5";
          ctx.moveTo(100,100);
          ctx.lineTo(130,100);
          ctx.moveTo(170,100);
          ctx.lineTo(200,100);
          ctx.stroke();
          ctx.beginPath();
          ctx.strokeStyle="red";  // Use strokeStyle to change the color.
          ctx.arc(150,125,100,0,Math.PI, false);
          ctx.stroke();
        }  
    }
      </script>
    </head>
    <body onload="draw();">
      <canvas id="MyCanvas" width="600" height="600"> </canvas> 
    </body>
    </html>
    参考
      

  2.   

    <!DOCTYPE html> <html>
    <head>
      <script type="text/javascript">
          function draw() {
              var canvas = document.getElementById("MyCanvas");
              if (canvas.getContext) {
                  var ctx = canvas.getContext("2d");
                  ctx.beginPath();
                  ctx.strokeStyle = "green";  // Use strokeStyle to set the color.
                  ctx.rect(5, 5, 300, 250);
                  ctx.stroke();
                  ctx.beginPath();
                  ctx.strokeStyle = "blue";  // Use strokeStyle to change the color.
                  ctx.lineWidth = "5";
                  ctx.moveTo(100, 100);
                  ctx.lineTo(130, 100);
                  ctx.stroke();
                  ctx.beginPath();
                  ctx.strokeStyle = "red"; 
                  ctx.moveTo(170, 100);
                  ctx.lineTo(200, 100);
                  ctx.stroke();
                  
              }
          }
      </script>
    </head>
    <body onload="draw();">
      <canvas id="MyCanvas" width="600" height="600"> </canvas> 
    </body>
    </html>
      

  3.   


    = =...只能stroke一次啊,因为是插件里的东西,插件原先是将路径全部处理好之后 ,然后stroke,因为这边需要不同颜色的直线,所以想请问下能否只stroke一次,而画出不同strokeStyle的直线?