当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML canvas beginPath()用法及代码示例


画布beginPath()方法用于启动路径或重置当前路径。 moveTo(),lineTo(),quadricCurveTo(),bezierCurveTo(),arcTo()和arc()方法用于创建路径。此外,stroke()方法用于在画布上绘制路径。

用法:

context.beginPath();

范例1:


<!DOCTYPE html>  
<html>  
  
<head>  
    <title>  
        HTML canvas beginPath() Property  
    </title>  
</head>  
  
<body style="text-align:center;">  
      
    <h1 style="color:green;"> 
        GeeksforGeeks 
    </h1>  
      
    <h2>HTML canvas beginPath() Property</h2>  
      
    <canvas id="GFG" width="500" height="300"></canvas>  
      
    <script>  
        var GFG = document.getElementById("GFG");  
        var context = GFG.getContext("2d"); 
          
        // Create a path 
        context.beginPath(); 
          
        // Set the path width 
        context.lineWidth = "8"; 
          
        // Set the path color 
        context.strokeStyle = "green"; 
          
        context.moveTo(100, 250); 
        context.lineTo(150, 50); 
        context.lineTo(250, 250); 
          
        context.stroke(); 
          
        context.beginPath(); 
    </script>  
</body>  
  
</html>                    

输出:

范例2:

<!DOCTYPE html>  
<html>  
  
<head>  
    <title>  
        HTML canvas beginPath() Property  
    </title>  
</head>  
  
<body style="text-align:center;">  
         
    <h1 style="color:green;"> 
        GeeksforGeeks 
    </h1>  
         
    <h2>HTML canvas beginPath() Property</h2>  
      
    <canvas id="GFG" width="500" height="300"></canvas>  
         
    <script>  
        var GFG = document.getElementById("GFG");  
        var context = GFG.getContext("2d"); 
          
        // Set the path width 
        context.lineWidth = 5; 
          
        // Create path 
        context.beginPath(); 
        context.moveTo(100, 20); 
        context.lineTo(230, 200); 
          
        // Set path color 
        context.strokeStyle = 'green';   
        context.stroke(); 
          
        // Create another path 
        context.beginPath(); 
        context.moveTo(230, 200);   
        context.quadraticCurveTo(230, 200, 260, 100); 
          
        // Set path color 
        context.strokeStyle = 'blue';   
        context.stroke();   
          
        // Create another path 
        context.beginPath(); 
        context.moveTo(260, 100);     
        context.bezierCurveTo(290, -40, 300, 190, 400, 150); 
          
        // Set path color 
        context.strokeStyle = 'orange';     
        context.stroke(); 
        context.closePath();   
   </script>   
</body>  
  
</html> 

支持的浏览器:下面列出了HTML canvas beginPath()方法支持的浏览器:

  • 谷歌浏览器
  • Internet Explorer 9.0
  • 火狐浏览器
  • 苹果浏览器
  • Opera


相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 HTML | canvas beginPath() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。