2016-04-23 5 views
0

シェイプをキャンバス全体にアニメーション化してエッジを叩いた後に跳ね返そうとしていますが、私のボールのように他の2つの図形に速度がないのは不明です。以下のコードを投稿します。JavaScriptで図形をアニメーション化する

<html> 

<body> 
    <section> 
    <div> 
     <canvas id="canvas" width="700" height="300"></canvas> 
    </div> 

    <script type="text/javascript"> 
    var canvas, 
     context, 
    x = Math.floor((Math.random() * 400) + 1),   //Ball x coordinate 
    y = Math.floor((Math.random() * 300) + 1),   //Ball y coordinate 
    dx = Math.floor((Math.random() * 10) + 1),    //X velocity 
    dy = Math.floor((Math.random() * 4) + 1),    //Y velocity 
    width = 700,  //Width of the background 
    height = 300;  //Height of the background 


function drawCircle(x,y,r) { //Draws the ball 
    context.beginPath(); 
    context.arc(x, y, r, 0, Math.PI*2, true); 
    context.fill(); 
} 


function drawRect(x,y,w,h) { //Draws the background 
    context.beginPath(); 
    context.rect(x,y,w,h); 
    context.closePath(); 
    context.fill(); 
} 

function start() { //Runs when the window loads. Stores canvas and context  in variables. Runs "draw" on an interval of 10ms 
    canvas = document.getElementById("canvas"); 
    context = canvas.getContext("2d"); 
    return setInterval(draw, 10); 
} 

function drawSquare(){ 
var canvas = document.getElementById('canvas'); 
    if (canvas.getContext){ 
    var ctx = canvas.getContext('2d'); 
    ctx.fillStyle = "white"; 
    ctx.beginPath(); 
    ctx.moveTo(200,50); 
    ctx.lineTo(250,50); 
    ctx.lineTo(300, 100); 
    ctx.lineTo(250,25); 
    ctx.fill(); 



} 

} 



function drawTri(){ 
var canvas = document.getElementById('canvas'); 
    if (canvas.getContext){ 
    var ctx = canvas.getContext('2d'); 

    ctx.beginPath(); 
    ctx.moveTo(75,50); 
    ctx.lineTo(100,75); 
    ctx.lineTo(75,25); 
    ctx.fill(); 
    } 

} 



function draw() { 
    context.clearRect(0, 0, width, height); //Clears the drawing space 
    context.fillStyle = "black";   //Sets fillstyle to black (for the  background) 
    drawRect(0,0, width, height);   //Draws the background 
    context.fillStyle = "white";   //Sets fillstyle to white (for the  ball) 
    drawCircle(x, y, 10); 
drawTri(); 
drawSquare(); //Calls function to draw the ball 
    if (x + dx > width || x + dx < 0)  //If the ball would leave the  width (right or left) on the next redraw... 
    dx = -dx;        //reverse the ball's velocity 
    if (y + dy > height || y + dy < 0)  //If the ball would leave the  height (top or bottom) on the next redraw... 
    dy = -dy;        //reverse the ball's velocity 
    x += dx;        //Increase ball x speed by  velocity 
    y += dy;        //Increase ball y speed by  velocity 
}   
window.onload = start;     //Run "start" function after the  window loads 
</script> 

    </section> 
</body> 
</html> 

答えて

0

あなたは変数を使用して図面を忘れています。そして、変数は同じです(2つの図形は常に一緒になります).xが円の場合は、rectがその背後にあるためです。だから、あなたはこのようにそれを追跡するために、より多くの変数を必要とする:circ1x、circ1y、circ2x、circ2y、rect1x、rect1y、rect2x、rect2y

0

あなたのボールは、変数の座標(x & Yに)

context.arc(x, y, r, 0, Math.PI*2, true); 
を使用して描画されています

あなたの形状は、ハードコード、一定の値を使用して描画されています:

ctx.moveTo(200,50); 
ctx.lineTo(250,50); 
ctx.lineTo(300, 100); 
ctx.lineTo(250,25); 

&

ctx.moveTo(75,50); 
ctx.lineTo(100,75); 
ctx.lineTo(75,25); 

アニメーションを作成するには、変数を使用して他の図形を描画し、それらの変数を更新します。例えば。

var x = 75, 
    y = 50; 
ctx.moveTo(x,y); 
ctx.lineTo(x + 25, y + 25); 
ctx.lineTo(x, y - 25); 
関連する問題