我目前正在尝试用画布制作1张快乐的面孔和1张悲伤的面孔,但问题是我无法让2张面孔出现,只能出现一张。

    <!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>My Site's Title</title>
</head>
<body>

    <canvas id="myDrawing" width="800" height="200" style="border:1px solid #EEE">
    </canvas>
            <script>
               var canvas = document.getElementById("myDrawing");
                var ctx = canvas.getContext("2d");

                var x = canvas.width / 2;
                var y = canvas.height / 2;
                var radius = 75;
                var startAngle = 0;
                var endAngle = 2 * Math.PI;
 function drawFace() {


                ctx.beginPath();
                ctx.arc(x, y, radius, startAngle, endAngle);
                ctx.stroke();
                ctx.fillStyle = "yellow";
                ctx.fill();
                }

                function drawSmile(){
                var x = canvas.width / 2;
                var y = 150
                var radius = 40;
                var startAngle = 1.1 * Math.PI;
                var endAngle = 1.9 * Math.PI;

                ctx.beginPath();
                ctx.arc(x, y, radius, startAngle, endAngle);
                ctx.lineWidth = 7;

                // line color
                ctx.strokeStyle = 'black';
                ctx.stroke();
                }


                function drawEyes(){
                var centerX = 40;
                var centerY = 0;
                var radius = 10;

                // save state
                ctx.save();

                // translate context so height is 1/3'rd from top of enclosing circle
                ctx.translate(canvas.width / 2, canvas.height / 3);

                // scale context horizontally by 50%
                ctx.scale(.5, 1);

                // draw circle which will be stretched into an oval
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

                // restore to original state
                ctx.restore();

                // apply styling
                ctx.fillStyle = 'black';
                ctx.fill();
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'black';
                ctx.stroke();

                //left eye
                var centerX = -40;
                var centerY = 0;
                var radius = 10;

                // save state
                ctx.save();

                // translate context so height is 1/3'rd from top of enclosing circle
                ctx.translate(canvas.width / 2, canvas.height / 3);

                // scale context horizontally by 50%
                ctx.scale(.5, 1);

                // draw circle which will be stretched into an oval
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

                // restore to original state
                ctx.restore();

                // apply styling
                ctx.fillStyle = 'black';
                ctx.fill();
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'black';
                ctx.stroke();
                }
                drawFace()

   function drawHappyFace(){
   drawFace();
   drawEyes();
   drawSmile();
}

drawHappyFace();

// SECOND FACE - HAPPY FACE

<canvas id="canvas" width="200" height="200" style="border:1px solid #EEE">
    </canvas>
            <script>
               var canvas = document.getElementById("canvas");
                var ctx = canvas.getContext("2d");

                var x = canvas.width / 2;
                var y = canvas.height / 2;
                var radius = 75;
                var startAngle = 0;
                var endAngle = 2 * Math.PI;
 function drawFace() {


                ctx.beginPath();
                ctx.arc(x, y, radius, startAngle, endAngle);
                ctx.stroke();
                ctx.fillStyle = "yellow";
                ctx.fill();
                }

                function drawSmile(){
                var x = canvas.width / 2;
                var y = 150
                var radius = 40;
                var startAngle = 1.9 * Math.PI;
                var endAngle = 1.1 * Math.PI;

                ctx.beginPath();
                ctx.arc(x, y, radius, startAngle, endAngle);
                ctx.lineWidth = 7;

                // line color
                ctx.strokeStyle = 'black';
                ctx.stroke();
                }


                function drawEyes(){
                var centerX = 40;
                var centerY = 0;
                var radius = 10;

                // save state
                ctx.save();

                // translate context so height is 1/3'rd from top of enclosing circle
                ctx.translate(canvas.width / 2, canvas.height / 3);

                // scale context horizontally by 50%
                ctx.scale(.5, 1);

                // draw circle which will be stretched into an oval
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

                // restore to original state
                ctx.restore();

                // apply styling
                ctx.fillStyle = 'black';
                ctx.fill();
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'black';
                ctx.stroke();

                //left eye
                var centerX = -40;
                var centerY = 0;
                var radius = 10;

                // save state
                ctx.save();

                // translate context so height is 1/3'rd from top of enclosing circle
                ctx.translate(canvas.width / 2, canvas.height / 3);

                // scale context horizontally by 50%
                ctx.scale(.5, 1);

                // draw circle which will be stretched into an oval
                ctx.beginPath();
                ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

                // restore to original state
                ctx.restore();

                // apply styling
                ctx.fillStyle = 'black';
                ctx.fill();
                ctx.lineWidth = 2;
                ctx.strokeStyle = 'black';
                ctx.stroke();
                }
                drawFace()

   function drawHappyFace(){
   drawFace();
   drawEyes();
   drawSmile();
}

drawHappyFace();
            </script>
</body>
</html>
</body>
</html>


由于某种原因,我只会一次显示一张面孔,但我希望同时显示两个面孔!

最佳答案

您不能拥有多个具有相同名称的功能,因为第二个功能会覆盖(隐藏)第一个功能;它们是否位于相同的<script>标记中无关紧要(它们甚至可以位于不同的文件中)

如果您是初学者,请更改第二张脸的功能名称,但是您应该为该功能提供参数,以便使用单个功能在多个画布和形状之间进行选择

它看起来像这样:
http://jsfiddle.net/Y5rUH/2/

10-07 22:37