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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。