单击对象以执行某些功能

时间:2017-12-07 05:34:54

标签: javascript object animation canvas

我正在使用JavaScript进行编程,基本上我应该做的是在画布上绘制一个面,这些面具有一定的效果。

我应该做的其中一个效果是点击鼻子,它会将脸变成愤怒的脸。我已经为所有脸部(鼻子,眼睛等)创建了对象,而我正在尝试做的是我希望在点击鼻子时调用“angryFace”功能。鼻子已经是一个物体了。

长期坚持这一点。如果有人可以帮忙,我将不胜感激!在这里上传了整个代码。谢谢你们!

<!doctype html>

<html lang="en">

<head>
    <meta charset="utf-8">
    <title>COM1008 Assignment 2</title>
    <link rel="stylesheet" href="main1.css">
</head>

<main>
    <h1>COM 1008 Assignment 2</h1>
    <p>By Samuel Fung Chi Lun</p>
</main>

<body>
    <canvas id="canvasFrame" width="600" height="600"></canvas>
    <p>
        <button name="sadbutton" id="sadFace">Sad</button>
        <button name="angrybutton" id="angryFace">Angry</button>
        <button name="surprisedbutton" id="surprisedFace">Surprised</button>
        <button name="neutralbutton" id="neutralFace">Neutral</button>
    </p>
    <script>
        var canvas = document.getElementById('canvasFrame');
        var context = canvas.getContext('2d');
        //Function to obtain x and y coordinates of mouse positions - Obtained from 20/11/2017 lecture slides
        function getMouseXY(e) {
            var boundingRect = canvas.getBoundingClientRect();
            var offsetX = boundingRect.left;
            var offsetY = boundingRect.top;
            var w = (boundingRect.width - canvas.width);
            var h = (boundingRect.height - canvas.height);
            offsetX += w;
            offsetY += h;
            var mx = Math.round(e.clientX - offsetX);
            var my = Math.round(e.clientY - offsetY);
            return { x: mx, y: my };
        }

        const BROWS_UP = 190;
        const BROWS_DOWN = 170;

        //Creating an object for left eye brow
        var leftEyeB = {
            draw: function (x1,brows_direction1,x2,brows_direction2) {
                context.beginPath();
                context.lineWidth = 18;
                context.moveTo(x1, brows_direction1);
                context.lineTo(x2, brows_direction2)
                context.stroke();
            }
        };
        //Creating an object for right eye brow
        var rightEyeB = {
            draw: function (x1,brows_direction1,x2,brows_direction2) {
                context.beginPath();
                context.lineWidth = 18;
                context.moveTo(x1, brows_direction1);
                context.lineTo(x2, brows_direction2)
                context.stroke();
            }
        };
        //Creating an object for the head
        var faceShape = {
            draw: function() {
                context.beginPath();
                context.fillStyle = "rgb(255,255,0)"
                context.lineWidth = 3;
                context.arc(300, 300, 200, 0, Math.PI * 2, true);
                context.fill();
                context.stroke();
            }
        }
        //Creating an object for the eyes
        var eyes = {
            draw: function() {
                context.beginPath();
                context.fillStyle = "rgb(0,0,0)";
                context.moveTo(255, 250); //LeftEye
                context.arc(220, 250, 40, 0, Math.PI * 2, true);
                context.moveTo(415, 250);//Right Eye
                context.arc(380, 250, 40, 0, Math.PI * 2, true);
                context.fill();
                context.fill();
                context.stroke();
                context.beginPath();
                context.fillStyle = "rgb(255,255,255)";
                context.moveTo(240, 250); //LeftPupil
                context.arc(220, 250, 15, 0, Math.PI * 2, true);
                context.moveTo(400, 250); //Right Pupil
                context.arc(380, 250, 15, 0, Math.PI * 2, true);
                context.fill();
                context.stroke();
            }
        }
        //Creating an object for the nose
        var nose = {
            draw: function() {
                context.beginPath();
                context.fillStyle = "rgb(0,0,0)";
                context.moveTo(300, 275);
                context.lineTo(275, 325);
                context.lineTo(325, 325);
                context.fill();
                context.closePath();
                context.stroke();
            }
        }
        //Creating an object for the mouth
        var mouth = {
            frown: function() {
                context.beginPath();
                context.fillStyle = "rgb(0,0,0)";
                context.moveTo(305, 427);//Mouth
                context.arc(305, 427, 80, 0, Math.PI, true); 
                context.fill();
                context.closePath();
                context.stroke();
            },
            circle: function() {
                context.beginPath();
                context.fillStyle = "rgb(0,0,0)";
                context.arc(300, 400, 50, 0, Math.PI * 2, true);
                context.fill();
                context.stroke();
            },
            straight: function() {
                context.beginPath();
                context.fillStyle = "rgb(0,0,0)";
                context.rect(225, 390, 150, 20);//Mouth
                context.fill();
                context.stroke();
            }
        }
        //Drawing the sad face
        function sadFace() {
            faceShape.draw();
            eyes.draw();
            nose.draw();
            mouth.frown();
            leftEyeB.draw(175,BROWS_DOWN,265,BROWS_DOWN);
            rightEyeB.draw(335,BROWS_DOWN,425,BROWS_DOWN);
        }
        //Drawing the angry face
        function angryFace() {
            faceShape.draw();
            eyes.draw();
            nose.draw();
            mouth.frown();
            leftEyeB.draw(175,BROWS_DOWN,265,BROWS_UP);
            rightEyeB.draw(335,BROWS_UP,425,BROWS_DOWN);
        }
        //Drawing the surprised face
        function surprisedFace() {
            faceShape.draw();
            eyes.draw();
            nose.draw();
            mouth.circle();
            leftEyeB.draw(175,BROWS_UP,265,BROWS_DOWN);
            rightEyeB.draw(335,BROWS_DOWN,425,BROWS_UP);
        }
        //Drawing the neutral face
        function neutralFace() {
            faceShape.draw();
            eyes.draw();
            nose.draw();
            mouth.circle();
            leftEyeB.draw(175,BROWS_DOWN,265,BROWS_DOWN);
            rightEyeB.draw(335,BROWS_DOWN,425,BROWS_DOWN);
        }
        //Not sure how to properly do the bottom part. It does not work at all for the eyebrows one so please help!
        function effects() {
            //Click on the eyebrows to raise them
            if (mousePosition.x > x1 && mousePosition.x < x2 && mousePosition.y < BROWS_UP && mousePosition.y > BROWS_DOWN) {
                BROWS_UP += 20;
            }
            //Click on eye to show angry face
            if (mousePosition = on coordinates of the eye) {
                angryFace();
            }
            //Click on nose to show happy face
            if (mousePosition = on coordinates of the nose) {
                smileFace();
            }
        }

        canvas.addEventListener('click', function(evt) {
            effects(evt);
        });

        neutralFace();

        //Linking the face functions to the buttons
        var angryButton = document.getElementById("angryFace");
        var sadButton = document.getElementById("sadFace");
        var surprisedButton = document.getElementById("surprisedFace");
        var neutralButton = document.getElementById("neutralFace");
        sadButton.addEventListener("click", sadFace);
        angryButton.addEventListener("click", angryFace);
        surprisedButton.addEventListener("click", surprisedFace);
        neutralButton.addEventListener("click", neutralFace);
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当你做脸时,对你来说,有不同的对象(说鼻子,眼睛等)。对于DOM,只有一个对象,即Canvas。所以当你点击时,你点击画布而不是鼻子。

要实现这一点,你必须使用坐标。你必须假设每个容器的位置,并点击画布,检查坐标。如果坐标在某个对象的范围内,则触发必要的功能。

canvas.addEventListener('click', function(event){
  var x = event.clientX, y = event.clientY;
  if(x> 280 && x <335 && y > 280 && y <335) {
    // nose is clicked.
    angryFace();
  }
  else {
    neutralFace();
  }
})

以下是部分样本:

var canvas = document.getElementById('canvasFrame');
var context = canvas.getContext('2d');
//Function to obtain x and y coordinates of mouse positions - Obtained from 20/11/2017 lecture slides
function getMouseXY(e) {
  var boundingRect = canvas.getBoundingClientRect();
  var offsetX = boundingRect.left;
  var offsetY = boundingRect.top;
  var w = (boundingRect.width - canvas.width);
  var h = (boundingRect.height - canvas.height);
  offsetX += w;
  offsetY += h;
  var mx = Math.round(e.clientX - offsetX);
  var my = Math.round(e.clientY - offsetY);
  return {
    x: mx,
    y: my
  };
}

const BROWS_UP = 190;
const BROWS_DOWN = 170;

//Creating an object for left eye brow
var leftEyeB = {
  draw: function(x1, brows_direction1, x2, brows_direction2) {
    context.beginPath();
    context.lineWidth = 18;
    context.moveTo(x1, brows_direction1);
    context.lineTo(x2, brows_direction2)
    context.stroke();
  }
};
//Creating an object for right eye brow
var rightEyeB = {
  draw: function(x1, brows_direction1, x2, brows_direction2) {
    context.beginPath();
    context.lineWidth = 18;
    context.moveTo(x1, brows_direction1);
    context.lineTo(x2, brows_direction2)
    context.stroke();
  }
};
//Creating an object for the head
var faceShape = {
  draw: function() {
    context.beginPath();
    context.fillStyle = "rgb(255,255,0)"
    context.lineWidth = 3;
    context.arc(300, 300, 200, 0, Math.PI * 2, true);
    context.fill();
    context.stroke();
  }
}
//Creating an object for the eyes
var eyes = {
  draw: function() {
    context.beginPath();
    context.fillStyle = "rgb(0,0,0)";
    context.moveTo(255, 250); //LeftEye
    context.arc(220, 250, 40, 0, Math.PI * 2, true);
    context.moveTo(415, 250); //Right Eye
    context.arc(380, 250, 40, 0, Math.PI * 2, true);
    context.fill();
    context.fill();
    context.stroke();
    context.beginPath();
    context.fillStyle = "rgb(255,255,255)";
    context.moveTo(240, 250); //LeftPupil
    context.arc(220, 250, 15, 0, Math.PI * 2, true);
    context.moveTo(400, 250); //Right Pupil
    context.arc(380, 250, 15, 0, Math.PI * 2, true);
    context.fill();
    context.stroke();
  }
}
//Creating an object for the nose
var nose = {
  draw: function() {
    context.beginPath();
    context.fillStyle = "rgb(0,0,0)";
    context.moveTo(300, 275);
    context.lineTo(275, 325);
    context.lineTo(325, 325);
    context.fill();
    context.closePath();
    context.stroke();
  }
}
//Creating an object for the mouth
var mouth = {
  frown: function() {
    context.beginPath();
    context.fillStyle = "rgb(0,0,0)";
    context.moveTo(305, 427); //Mouth
    context.arc(305, 427, 80, 0, Math.PI, true);
    context.fill();
    context.closePath();
    context.stroke();
  },
  circle: function() {
    context.beginPath();
    context.fillStyle = "rgb(0,0,0)";
    context.arc(300, 400, 50, 0, Math.PI * 2, true);
    context.fill();
    context.stroke();
  },
  straight: function() {
    context.beginPath();
    context.fillStyle = "rgb(0,0,0)";
    context.rect(225, 390, 150, 20); //Mouth
    context.fill();
    context.stroke();
  }
}
//Drawing the sad face
function sadFace() {
  faceShape.draw();
  eyes.draw();
  nose.draw();
  mouth.frown();
  leftEyeB.draw(175, BROWS_DOWN, 265, BROWS_DOWN);
  rightEyeB.draw(335, BROWS_DOWN, 425, BROWS_DOWN);
}
//Drawing the angry face
function angryFace() {
  faceShape.draw();
  eyes.draw();
  nose.draw();
  mouth.frown();
  leftEyeB.draw(175, BROWS_DOWN, 265, BROWS_UP);
  rightEyeB.draw(335, BROWS_UP, 425, BROWS_DOWN);
}
//Drawing the surprised face
function surprisedFace() {
  faceShape.draw();
  eyes.draw();
  nose.draw();
  mouth.circle();
  leftEyeB.draw(175, BROWS_UP, 265, BROWS_DOWN);
  rightEyeB.draw(335, BROWS_DOWN, 425, BROWS_UP);
}
//Drawing the neutral face
function neutralFace() {
  faceShape.draw();
  eyes.draw();
  nose.draw();
  mouth.circle();
  leftEyeB.draw(175, BROWS_DOWN, 265, BROWS_DOWN);
  rightEyeB.draw(335, BROWS_DOWN, 425, BROWS_DOWN);
}

function effects() {
  mousePosition = getMouseXY(e);
  console.log(mousePosition);

}
neutralFace();

canvas.addEventListener('click', function(event){
  var x = event.clientX, y = event.clientY;
  if(x> 280 && x <335 && y > 280 && y <335) {
    // nose is clicked.
    angryFace();
  }
  else {
    neutralFace();
  }
})

//Linking the face functions to the buttons
var angryButton = document.getElementById("angryFace");
var sadButton = document.getElementById("sadFace");
var surprisedButton = document.getElementById("surprisedFace");
var neutralButton = document.getElementById("neutralFace");
sadButton.addEventListener("click", sadFace);
angryButton.addEventListener("click", angryFace);
surprisedButton.addEventListener("click", surprisedFace);
neutralButton.addEventListener("click", neutralFace);
<!-- This is not the original markup. This is a part of an edit to make the code executable. -->
<canvas id='canvasFrame' width=600 height=600></canvas>

<div>
  <button id="angryFace">Angry Face</button>
  <button id="sadFace">Sad Face</button>
  <button id="surprisedFace">Surprised Face</button>
  <button id="neutralFace">Neutral Face</button>
</div>