亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    HTML5 canvas基本繪圖之繪制矩形

    <canvas></canvas>是HTML5中新增的標(biāo)簽,用于繪制圖形,這篇文章主要為大家詳細介紹了HTML5 canvas基本繪圖之繪制矩形方法,感興趣的小伙伴們可以參考一下

    <canvas></canvas>只是一個繪制圖形的容器,除了id、class、style等屬性外,還有height和width屬性。在<canvas>>元素上繪圖主要有三步:

    1.獲取<canvas>元素對應(yīng)的DOM對象,這是一個Canvas對象;
    2.調(diào)用Canvas對象的getContext()方法,得到一個CanvasRenderingContext2D對象;
    3.調(diào)用CanvasRenderingContext2D對象進行繪圖。

    繪制矩形rect()、fillRect()和strokeRect()

    ?context.rect( x , y , width , height ):只定義矩形的路徑;
    ?context.fillRect( x , y , width , height ):直接繪制出填充的矩形;
    ?context.strokeRect( x , y , width , height ):直接繪制出矩形邊框;

    JavaScript Code復(fù)制內(nèi)容到剪貼板

    1. <script type="text/javascript">

    2. var canvas = document.getElementById("canvas");

    3. var context = canvas.getContext("2d");

    4. //使用rect方法

    5. context.rect(10,10,190,190);

    6. context.lineWidth = 2;

    7. context.fillStyle = "#3EE4CB";

    8. context.strokeStyle = "#F5270B";

    9. context.fill();

    10. context.stroke();

    11. //使用fillRect方法

    12. context.fillStyle = "#1424DE";

    13. context.fillRect(210,10,190,190);

    14. //使用strokeRect方法

    15. context.strokeStyle = "#F5270B";

    16. context.strokeRect(410,10,190,190);

    17. //同時使用strokeRect方法和fillRect方法

    18. context.fillStyle = "#1424DE";

    19. context.strokeStyle = "#F5270B";

    20. context.strokeRect(610,10,190,190);

    21. context.fillRect(610,10,190,190);

    22. </script>

    HTML5 canvas基本繪圖之繪制矩形

    這里需要說明兩點:第一點就是stroke()和fill()繪制的前后順序,如果fill()后面繪制,那么當(dāng)stroke邊框較大時,會明顯的把stroke()繪制出的邊框遮住一半;第二點:設(shè)置fillStyle或strokeStyle屬性時,可以通過“rgba(255,0,0,0.2)”的設(shè)置方式來設(shè)置,這個設(shè)置的最后一個參數(shù)是透明度。

    另外還有一個跟矩形繪制有關(guān)的:清除矩形區(qū)域:context.clearRect(x,y,width,height)。
    接收參數(shù)分別為:清除矩形的起始位置以及矩形的寬和長。
    在上面的代碼中繪制圖形的最后加上:

    context.clearRect(100,60,600,100);

    可以得到以下效果:

    HTML5 canvas基本繪圖之繪制矩形

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號