0
我正在研究初學者JavaScript(Canvas)遊戲,併爲遊戲創建了鼠標形狀。我想把它變成一個JavaScript對象(構造函數和原型),並儘可能減少代碼量。請有人幫助我。看到它之後我通常都很好。提前致謝。下面是鼠標代碼:來自多個形狀的Javascript對象
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1200" height="900"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// *********************************** Head and Body **********
var bodyX = 0;
var bodyY = 0;
var bodyRadius = 25;
var headX = 22;
var headY = 0;
var headRadius = 14;
// save state
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// scale context horizontally
context.scale(2, 1);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(bodyX, bodyY, bodyRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#909090';
context.fill();
//***************************************** HEAD *****************
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// scale context horizontally
context.scale(2, 1);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(headX, headY, headRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#707070';
context.fill();
//************************************ Right Ear *************
var rtEarX = 26;
var rtEarY = 14;
var rtEarRadius = 8;
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(rtEarX, rtEarY, rtEarRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#707070';
context.fill();
//******************************* Left Ear ***************
var ltEarX = 26;
var ltEarY = -14;
var ltEarRadius = 8;
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(ltEarX, ltEarY, ltEarRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#707070';
context.fill();
//************************************ Right Eye ********************
var rtEyeX = 40;
var rtEyeY = -10;
var rtEyeRadius = 2;
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(rtEyeX, rtEyeY, rtEyeRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#ff0000';
context.fill();
//********************************** Left Eye ********
var ltEyeX = 40;
var ltEyeY = 10;
var ltEyeRadius = 2;
context.save();
// translate context
context.translate(canvas.width/2, canvas.height/2);
// draw circle which will be stretched into an oval
context.beginPath();
context.arc(ltEyeX, ltEyeY, ltEyeRadius, 0, 2 * Math.PI, false);
// restore to original state
context.restore();
// apply styling
context.fillStyle = '#ff0000';
context.fill();
</script>
</body>
</html>