html5 canvas 畫一個矩形,在矩形里添加文字。 文字顏色與矩形背景顏色有關問題?


html5 canvas 畫一個矩形,在矩形里添加文字。 文字顏色與矩形背景顏色問題??
html5 canvas 畫一個矩形,在矩形里添加文字。 文字顏色與矩形背景顏色問題。

JScript code
 var c=this.callout[0];
    var cxt=c.getContext("2d");
        cxt.beginPath();
    cxt.moveTo(x0,y0);
    cxt.lineTo(x1,y1);
    cxt.lineTo(x2,y2);
    cxt.lineTo(x3,y3);
    cxt.lineTo(x4,y4);
    cxt.closePath();
    cxt.fillStyle="#000000";    
        cxt.fillText("hello world", x,y);
        cxt.fill();
    cxt.stroke();       

這樣子只有背景色而文字顏色顯示不了??
怎樣設置矩形背景色和文字顏色???


------解決方案--------------------
fillStyle strokeStyle 可以采用不同的顏色
例子

HTML code
<!DOCTYPE html>
<html>
<head>
    <title>Canvas beginePath example</title>
    <script>
        function beginDemo() {
            var canvas = document.getElementById("demo")
            var ctx = canvas.getContext('2d');
            ctx.beginPath();
            ctx.lineWidth = "3";
            ctx.strokeStyle = "blue";
            ctx.fillStyle = "orange";
            ctx.moveTo(100, 100);
            ctx.lineTo(100, 400);
            ctx.lineTo(400, 400);
            ctx.lineTo(400, 100);
            ctx.closePath();
            ctx.fill();
            ctx.stroke();
            ctx.font = "32pt Arial";
            ctx.strokeText("我是中文字", 120, 200);
            ctx.strokeStyle = "red";
            ctx.stroke();
        }
    </script>
</head>
<body onload="beginDemo();">
    <canvas id="demo" width="800" height="800"></canvas>
</body>
</html> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM