This question already has an answer here:
fillStyle not a function
                                
                                    (1个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试显示图像,但是context.fillStyle工作出现问题。它应该是吃豆人,但无法正常工作。能够解决此问题的任何帮助将不胜感激。

        <canvas> id="canvas" width="600" height="600">
            <p>Sorry, your browser doesnt understand the canvas element</p>
        </canvas>

        <script>
            var canvas;
            var context;
            canvas = document.getelementbyid('canvas');
            context = canvas.getContent('2d');
            function drawCharacter() {
                context.beginPath();
                context.moveTo(118, 118);
                context.lineTo(227, 73);
                context.bezierCurveTo(209, 30, 167, 0, 118, 0);
                context.bezierCurveTo(53, 0, 0.5, 53, 0, 118);
                context.bezierCurveTo(0, 183, 53, 235, 118, 235);
                context.bezierCurveTo(159, 235, 195, 215, 216, 183);
                context.lineTo(118, 118);
                context.closePath();
                context.fillStyle() = "rgb(249, 243, 161)";
                context.fill();
                context.stroke();
                context.beginPath();
                context.arc(118, 60, 10, 0, 2*Math.PI, false);
                context.fillStyle() = "rgb(0, 0, 0)";
                context.fill();
            }
            drawcharacter();
        </script>

最佳答案

CanvasRenderingContext2D.fillStyle是属性,而不是方法。删除括号:

context.fillStyle = "rgb(249, 243, 161)";

09-11 09:57