2016-07-17 10 views
2

Ich möchte einen Hintergrund für mein Skript auf einer Leinwand erstellen, die Bilder fallen lassen und den Bildschirm nach unten drehen. Könnte mir jemand erklären, wie ich ein Bild drehen und dann mit dem Element <canvas> auf den Bildschirm zeichnen würde? Ich habe den folgenden Code:Leinwand animieren Bilder Rotation

Equations.prototype.Draw = function() { 
     //increases the rotational value every loop 
     this.rotate = (this.rotate + 1) % 360; 
     //rotates the canvas 
     ctx.rotate(this.rotate*Math.PI/180); 
     //draw the image using current canvas rotation 
     ctx.drawImage(this.img,this.x,this.y); 
     //restore canvas to its previous state 
     ctx.rotate(-this.rotate*Math.PI/180); 
    }; 

ich dies versuchen und feststellen, dass die dreht Bild, sondern bewegt sich auch auf dem Bildschirm als auch in einer Kreisform um den Punkt (0,0) ich es an der gleichen Stelle bleiben wollen auf der Stelle drehen. Wie würde ich das beheben danke!

+0

[Leinwand Transformationen] (https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations) – Andreas

+0

Bitte fragen Sie nicht nach "Bitte schreib mir ein Tutorial". Recherchiere etwas, poste etwas Code und weise auf ein bestimmtes Problem hin! Siehe [fragen] und machen Sie bitte eine [mcve] –

+0

@ RokoC.Buljan Ich hätte meinen Code in der Frage sagen können, dass ich diesen Code habe, der Elemente auf dem Bildschirm bewegt, aber wie rotiere ich sie dann auch, während sie sich dann bewegen Ich bekomme ein Stück überarbeiteten Code zurück, der mir kein Verständnis gibt. Ich habe recherchiert und gefunden Rotation, Übersetzung und viele mehr, aber sie sind nicht sehr gut erklärt. Daher frage ich hier. Alles, was ich fragte, war, wie man ein Bild in Javascript eine sehr s imple Frage wahrscheinlich 10 Zeilen oder weniger einer Antwort und würde mir auch ein gutes Verständnis und 10 Zeilen scheint nicht wie ein längliches Tutorial für mich –

Antwort

2

Speichern Sie die context, umwandeln, drehen, malen, wiederherstellen.

function rand (min, max) { 
 
    return Math.floor(Math.random() * (max - min + 1)) + min; 
 
} 
 

 
var ctx = document.getElementById('cvs').getContext('2d'), 
 
    RAD = Math.PI/180, 
 
    width = window.innerWidth, 
 
    height = window.innerHeight, 
 
    fallingIconsAll = [], 
 
    FallingIcon = function() { 
 
     this.x = rand(0, width); 
 
     this.y = rand(0, height); 
 
     this.h = 32; 
 
     this.w = 32; 
 
     this.angle = rand(0, 360); 
 
     this.speed = rand(1, 4); 
 
     this.IMG = new Image(); 
 
     this.IMG.src = 'http://stackoverflow.com/favicon.ico'; 
 
     return this; 
 
    }; 
 
    
 
ctx.canvas.width = width, 
 
ctx.canvas.height = height, 
 

 
FallingIcon.prototype.move = function() { 
 
    // Manipulate Icon properties 
 
    if(this.y > height + this.h) { // (if has dropped) 
 
    this.y = -this.h;     // restart from top 
 
    this.x = rand(0, width);   // new X position 
 
    this.speed = rand(1, 4);   // new random speed 
 
    } 
 
    this.y += this.speed; 
 
    this.angle += this.speed % 360; 
 

 
    // Manipulate context 
 
    ctx.save();     // save context 
 
    ctx.translate(this.x, this.y); // move to point 
 
    ctx.rotate(this.angle * RAD); // rotate around that point 
 
    ctx.drawImage(this.IMG, -(this.IMG.width/2), -(this.IMG.height/2)); 
 
    ctx.restore();     // restore to initial coordinates 
 
}; 
 

 
// Create falling Icons 
 
for (var i = 0; i < 20; i++) { 
 
    fallingIconsAll.push(new FallingIcon()); 
 
} 
 

 
// Setup Canvas 
 
ctx.canvas.width = width; 
 
ctx.canvas.height = height; 
 

 
// Animation loop 
 
(function loop() { 
 
    ctx.clearRect(0, 0, width, height); 
 
    fallingIconsAll.forEach(function (Icon) { 
 
    Icon.move(); 
 
    }); 
 
    requestAnimationFrame(loop); 
 
}());
body{margin:0;} canvas{display:block;}
<canvas id="cvs"></canvas>

+0

Danke, das erklärt, warum meine Drehung um den Punkt (0,0) rotierte, anstatt die x- und y-Koordinaten Danke! –

+0

@ SamuelNewport du bist willkommen! –

Verwandte Themen