當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML canvas stroke()用法及代碼示例


畫布stroke()方法用於繪製所有moveTo()和lineTo()方法定義的路徑。畫布stroke()方法的默認顏色是黑色。

用法:

context.stroke()

示例1:


<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML canvas fillRect() Method 
    </title> 
</head> 
  
<body> 
    <canvas id="GFG"
            width="500" 
            height="300"> 
  </canvas> 
  
    <script> 
        var x =  
            document.getElementById("GFG"); 
        var contex =  
            x.getContext("2d"); 
        contex.rect(50, 50, 350, 200); 
        contex.fillStyle = "green"; 
        contex.fill(); 
        
        // Draw the path 
        contex.stroke(); 
    </script> 
  
</body> 
  
</html>

輸出:
canvas_fill

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML canvas fillRect() Method 
    </title> 
</head> 
  
<body> 
    <canvas id="GFG" 
            width="500"
            height="300"> 
  </canvas> 
  
    <script> 
        var x =  
            document.getElementById("GFG"); 
        var contex =  
            x.getContext("2d"); 
        contex.beginPath(); 
        contex.moveTo(50, 50); 
        contex.lineTo(50, 250); 
        contex.lineTo(250, 250); 
        contex.lineTo(250, 50); 
        contex.strokeStyle = "green"; 
        contex.stroke(); 
    </script> 
  
</body> 
  
</html>

輸出:

支持的瀏覽器:

  • 穀歌瀏覽器
  • Internet Explorer 9.0
  • 火狐瀏覽器
  • 蘋果瀏覽器
  • Opera


相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 HTML | canvas stroke() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。