根据鼠标坐标移动对象

时间:2018-01-11 14:38:32

标签: javascript canvas

如何让以下脚本生效?我希望能够将船移动到鼠标坐标。空间本身应该移动,船应该留在中心视图中。 如果你能修改脚本会很好,因为每个人都写不同的函数。

最后,如果单击按钮,我希望船只移动到该目的地 我尝试了它,当它工作,船从未移动,我不知道它是否与prototype.update功能有关。

prototype.update.render是否与我写this.updatethis.render相同?



<script>
function domanDown(evt)
{
switch (evt)
        {
                case 38:  /* Up arrow was pressed or button pressed*/
                offsetY+=100 ;
                break;
                case 40:  /* Down arrow was pressed or button pressed*/
                offsetY-=100;
                break;
                case 37:  /* Left arrow was pressedor button pressed*/
                offsetX+=100;
                break;
                case 39:  /* Right arrow was pressed or button pressed*/
                offsetX-=100;
                break;
        }
}
window.requestAnimationFrame = function() {
	return window.requestAnimationFrame ||
		window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		function(f) {
			window.setTimeout(f,1e3/60);
		}
}();
					var Obj = function (x,y,sp,size){
						this.size = size;
						this.x = x;
						this.y = y;
					    this.color = sp;
						this.selected = 0;
					}
						var Planet_Class = function (x,y,sp){
						this.type = 'Planet_Class';
						this.depot = 11;
						this.xtype = new Obj(x,y,sp,10);
					  	w.objects.push(this);     
					  Planet_Class.prototype.update =function () {  
						}
						Planet_Class.prototype.render =function () {  
						  ctx.save();
						  ctx.beginPath();
						  ctx.fillStyle = this.xtype.color;
						  ctx.fillRect(this.xtype.x, this.xtype.y,this.xtype.size,this.xtype.size);
						  ctx.fill();
						  ctx.restore();
						}
					}
					var Usership = function(x,y,sp){
					this.depot = 10;
					this.type = 'Usership';
					this.xtype = new Obj(x,y,sp,10);
					w.objects.push(this);
					Usership.prototype.update =function (x,y) {  
					this.xtype.x = x || 20;
					this.xtype.y = y || 20;
					}
					Usership.prototype.render =function () {  
					ctx.save();
				    ctx.beginPath();
					ctx.fillStyle = this.xtype.color;
					ctx.fillRect(this.xtype.x, this.xtype.y,this.xtype.size,this.xtype.size);
					ctx.fill();
				    ctx.restore();
					}
					}
					var World = function(){     
						this.objects = new Array();
						this.count = function(type,sp){
							var cnt = 0;    
							for(var k = 0;k<this.objects.length;k++)
							cnt++;
						return cnt;
						}
					}
					renderWorld = function(){
						requestAnimationFrame(renderWorld);
						var spliceArray = Array();
						ctx.beginPath();
						objcnt = w.objects.length;
						ctx.clearRect(0, 0, canvas.width, canvas.height);
					    ctx.fillStyle = "white";
						ctx.fill();   
						i = 0;
						while(i < objcnt){         
					    w.objects[i].update();        
						w.objects[i].render();    
						if(w.objects[i].depot < 1)
						spliceArray.push(i);       				
						i++;   
						}
						for(var k = 0;k<spliceArray.length;k++)  
						{
						w.objects.splice(spliceArray[k],1);  }
						
					}
					function r1(max,min){
						return (Math.floor(Math.random() * (max - min)) + 1);
					}
						canvas = document.getElementById("canvas");
						ctx = canvas.getContext("2d"),
						width = 1024,
						height = 768;
						offsetX = (canvas.width/2)-300;   /Startpoint x for the Ship
						offsetY = (canvas.height/2)-300;   /Startpoint y for the Ship
						generateshipx = -offsetX + (canvas.width/2);
						generateshipy = -offsetY + (canvas.height/2);
						mX = generateshipx;     
						mY = generateshipy;   
						w = new World();
						new Usership(generateshipx,generateshipy,'green');
					    for(i=1;i<4;i++){
						 new Planet_Class(r1(600,2),r1(600,2),'red');
						}
						canvas.addEventListener("click", function (e) {
						mX = e.pageX- canvas.offsetLeft -offsetX) ;
						mY = e.pageY - canvas.offsetTop -offsetY) ;
						});
						renderWorld();

		</script>
&#13;
 
<html>
<head>
 </head>
 <body  style="background-color:black;">
  
 <canvas style="z-index:1" width="1024" height="768" id="canvas"></canvas>
<input type="button" style="z-index:2; position:absolute; top:300; left:10" value="uo" onCLick="domanDown(38)()">
<input type="button" style="z-index:2; position:absolute; top:340; left:10" value="down" onCLick="domanDown(40)">
<input type="button" style="z-index:2; position:absolute; top:380; left:10" value="left" onCLick="domanDown(37)">
<input type="button" style="z-index:2; position:absolute; top:420; left:10" value="right" onCLick="domanDown(39)">
<input type="button" style="z-index:2; position:absolute; top:460; left:10" value="move" onCLick="moveObj()">

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<style>
			body {
				background-color: black;
			}
			
			canvas {
				position: absolute;
				margin-left: auto;
				margin-right: auto;
				left: 0;
				right: 0;
				cursor: crosshair;
				border: solid 1px white;
				border-radius: 10px;
			}
		</style>
	</head>
	
	<body>
		<canvas id="canvas"></canvas>
		<script type="application/javascript">
			
			var canvasWidth = 180;
			var canvasHeight = 160;
			var canvas = null;
			var ctx = null;
			var bounds = null;
			var isRotating = false;
			var isMoving = false;
			
			var pan = {
				x: 0.0,
				y: 0.0
			};
			
			var target = {
				x: 0.0,
				y: 0.0,
				dist: 0.0
			};
			
			var ship = {
				x: (canvasWidth * 0.5)|0,
				y: (canvasHeight * 0.5)|0,
				width: 10.0,
				height: 10.0,
				rotation: 0.0,
				deltaRotation: 0.0
			};
			
			var stars = [];
				stars.length = 100;
			
			window.onload = function() {
				canvas = document.getElementById("canvas");
				canvas.width = canvasWidth;
				canvas.height = canvasHeight;
				ctx = canvas.getContext("2d");
				bounds = canvas.getBoundingClientRect();
				
				for (var i = 0; i < stars.length; ++i) {
					stars[i] = {
						x: (Math.random() * 4 * canvasWidth - 2 * canvasWidth)|0,
						y: (Math.random() * 4 * canvasHeight - 2 * canvasHeight)|0
					};
				}
				
				loop();
			}
			
			window.onmousedown = function(e) {
				isRotating = true;
				isMoving = false;
				target.x = pan.x + e.clientX - bounds.left;
				target.y = pan.y + e.clientY - bounds.top;
			}
			
			function loop() {
				// Tick
				// Rotate to face target
				if (isRotating) {
					// Vector creation
					var tX = ship.x - target.x;
					var tY = ship.y - target.y;
					var tL = Math.sqrt(tX**2 + tY**2);
					var fX = Math.sin(ship.rotation);
					var fY = -Math.cos(ship.rotation);
					var sX = Math.sin(ship.rotation + Math.PI * 0.5);
					var sY = -Math.cos(ship.rotation + Math.PI * 0.5); 
					
					// Normalization
					tX = tX / tL;
					tY = tY / tL;
					
					// Left or right?
					var a = 1.0 - Math.abs(tX * fX + tY * fY);
					ship.rotation +=  0.075 * (sX * tX + sY * tY < 0.0 ? 1.0 : -1.0);
					
					if (a < 0.01) {
						target.dist = tL;
						isRotating = false;
						isMoving = true;
					}
				}
				
				// Accelerate to target
				if (isMoving) {
					ship.x += Math.sin(ship.rotation) * 1.0;
					ship.y -= Math.cos(ship.rotation) * 1.0;
					
					if (--target.dist < 0.0) {
						isMoving = false;
					}
				}
			
				// Render
				// Update pan coordinates
				pan.x = ship.x - canvasWidth * 0.5;
				pan.y = ship.y - canvasHeight * 0.5;
			
				// Draw background
				ctx.fillStyle = "#222222";
				ctx.fillRect(0,0,canvasWidth,canvasHeight);
				
				ctx.fillStyle = "white";
				for (var i = 0; i < stars.length; ++i) {
					ctx.fillRect(
						stars[i].x - pan.x,
						stars[i].y - pan.y,
						2,
						2
					);
				}
				
				// Draw ship
				ctx.strokeStyle = "white";
				ctx.fillStyle = "darkred";
				ctx.translate(canvasWidth * 0.5,canvasHeight * 0.5);
				ctx.rotate(ship.rotation);
				ctx.beginPath();
				ctx.moveTo(-ship.width * 0.5,ship.height * 0.5);
				ctx.lineTo(ship.width * 0.5,ship.height * 0.5);
				ctx.lineTo(0.0,-ship.height * 0.5);
				ctx.lineTo(-ship.width * 0.5,ship.height * 0.5);
				ctx.fill();
				ctx.stroke();
				ctx.rotate(-ship.rotation);
				ctx.translate(-canvasWidth * 0.5,-canvasHeight * 0.5);
				
				requestAnimationFrame(loop);
			}
			
		</script>
	</body>
</html>