为什么运行出来是黑色的,都找了一周的问题

来源:2-4 多边形的填充和closePath

逆向的艺术

2015-10-04 13:49

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <style type="text/css"></style>
</head>
<body>
    <canvas id="canvas"style="border:1px solid #aaa;display:block;margin:50px auto;"></canvas>
    <script>
        window.onload=function(){
            var canvas=document.getElementById("canvas");
            canvas.height=768;
            canvas.width=1024;
            var context=canvas.getContext("2d");

            drawRect(context,100,100,400,400,20,"green","red");

        }

        function drawRect(cxt,x,y,width,height,borderWidth,borerColor,fillColor){
            cxt.beginPath();
            cxt.moveTo(x,y);
            cxt.lineTo(x + width,y);
            cxt.lineTo(x + width,y + height);
            cxt.lineTo(x,y + height);
            cxt.lineTo(x,y);
            cxt.closePath();
            
            cxt.lineWidth="borderWidth";
            cxt.fillStyle="fillColor";
            cxt.strokeStyle="borerColor";

            
            cxt.fill();
            cxt.stroke();
        }
        
    </script>
</body>
</html>


http://img.mukewang.com/5610bd630001d61415560984.jpg

写回答 关注

3回答

  • Y_du
    2015-10-04 17:15:11
    已采纳
    cxt.fillStyle=fillColor;
    cxt.strokeStyle=borerColor;

    fillColor和borerColor是变量,不能用引号。

    Y_du 回复逆向的艺术

    对代码不熟悉,多写写就好了

    2015-10-04 20:27:29

    共 2 条回复 >

  • Y_du
    2015-10-04 17:17:07
    cxt.lineWidth=borderWidth;

    这个也是相同的问题

  • 逆向的艺术
    2015-10-04 14:08:51

    真的是完全检测不出bug,好忧伤

Canvas绘图详解

Canvas系列教程第二课,详解Canvas各接口,让同学彻底掌握Canvas绘图

72881 学习 · 422 问题

查看课程

相似问题