How to draw the three triangle in single page using canvas?

后端 未结 3 385
清歌不尽
清歌不尽 2021-01-24 10:58



        
相关标签:
3条回答
  • 2021-01-24 11:25

    <!DOCTYPE HTML>
    <html>
       <head>
          
          <style>
             #test {
                width: 100px;
                height:100px;
                margin: 0px auto;
             }
          </style>
          
          <script type="text/javascript">
             function drawShape(){
                var canvas = document.getElementById('mycanvas');
                
                if (canvas.getContext){
                
                   var ctx = canvas.getContext('2d');
                
                   ctx.beginPath();
                   ctx.moveTo(25,25);
                   ctx.lineTo(105,25);
                   ctx.lineTo(25,105);
                   ctx.fill();
                
                   ctx.beginPath();
                   ctx.moveTo(125,125);
                   ctx.lineTo(125,45);
                   ctx.lineTo(45,125);
                   ctx.closePath();
                   ctx.stroke();
                }
    }
              
          </script>
       </head>
       
       <body id="test" onload="drawShape();">
          <canvas id="mycanvas"></canvas>
       </body>
       
    </html>

    0 讨论(0)
  • 2021-01-24 11:29

    function draw() {
      var canvas = document.getElementById('canvas');
      if (canvas.getContext){
        var ctx = canvas.getContext('2d');
    
        ctx.beginPath();
        ctx.moveTo(75,50);
        ctx.lineTo(100,75);
        ctx.lineTo(100,25);
        ctx.fill();
      }
    }
    <html>
     <body onload="draw();">
       <canvas id="canvas" width="100" height="100"></canvas>
     </body>
    </html>

    You can reffer following artice-Drawing shapes in canvas.

    0 讨论(0)
  • 2021-01-24 11:31

    `

    <!DOCTYPE html>
    <html>
    <head>
    <title>Triangle Canvas Example</title>
    </head>
    <body>
    <canvas id="myCanvas" width="500" height="500"></canvas>
    <script>
     var canvasElement = document.querySelector("#myCanvas");
    var context = canvasElement.getContext("2d");
     
    // the triangle
    context.beginPath();
    context.moveTo(200, 100);
    context.lineTo(100, 300);
    context.lineTo(300, 300);
    context.closePath();
     
    // the outline
    context.lineWidth = 10;
    context.strokeStyle = '#666666';
    context.stroke();
     
    // the fill color
    context.fillStyle = "#FFCC00";
    context.fill();
        </script>
    </body>
    </html>

    `Try this

    Html :

    <!DOCTYPE html>
    <html>
    <head>
        <title>Triangle Canvas Example</title>
    </head>
    <body>
        <canvas id="myCanvas" width="500" height="500"></canvas>
    
    
    
    </body>
    </html>
    

    javascript :

        var canvasElement = document.querySelector("#myCanvas");
    var context = canvasElement.getContext("2d");
    
    // the triangle
    context.beginPath();
    context.moveTo(100, 100);
    context.lineTo(100, 300);
    context.lineTo(300, 300);
    context.closePath();
    
    // the outline
    context.lineWidth = 10;
    context.strokeStyle = '#666666';
    context.stroke();
    
    // the fill color
    context.fillStyle = "#FFCC00";
    context.fill();
    
    0 讨论(0)
提交回复
热议问题