2016-05-02 23 views
6

Voglio rendere più "fluida" l'animazione del mio Rect. Attualmente è davvero goffo. Conosco la ragione per questo. Una delle coordinate diventa valore ricercato prima dell'altro.Riattiva Rect più liscio

Ad esempio se sono attualmente a (0,0) e devo andare a (150,75) e incrementare ogni uno ugualmente ogni secondo, il cavo y verrà molto prima di x-cord.

enter image description here

var canvas = document.getElementById('canvas'); 
 
var ctx = document.getElementById('canvas').getContext('2d'); 
 

 
var aniTimer; 
 

 
var x; 
 
var y; 
 

 
var tx = tx || 0; 
 
var ty = ty || 0; 
 

 
var xDir; 
 
var yDir; 
 

 
function followMouse(e) { 
 
    x = e.offsetX; 
 
    y = e.offsetY; 
 
    cancelAnimationFrame(aniTimer); 
 
    moveObject(); 
 
} 
 

 
function moveObject() { 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
    
 
    if (x < tx) { 
 
    xDir = -1; 
 
    } else { 
 
    xDir = 1; 
 
    } 
 
    if (y < ty) { 
 
    yDir = -1; 
 
    } else { 
 
    yDir = 1; 
 
    } 
 
    tx = tx != x ? tx + xDir : tx; 
 
    ty = ty != y ? ty + yDir : ty; 
 
    
 
    
 
    ctx.fillRect(tx - 25 , ty + 25, 50, 10); 
 
    if (tx != x || ty != y) { 
 
    aniTimer = window.requestAnimationFrame(moveObject); 
 
    } 
 
} 
 

 
function resizeCanvas() { 
 
    canvas.width = window.innerWidth; 
 
    canvas.height = window.innerHeight; 
 
}; 
 

 
canvas.addEventListener('mousemove', _.throttle(function(e) { 
 
    followMouse(e); 
 
}, 100)); 
 

 
window.addEventListener('resize', resizeCanvas, false); 
 

 
resizeCanvas();
html, 
 
body { 
 
    margin: 0; 
 
    height: 100%; 
 
} 
 

 
canvas { 
 
    display: block; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<canvas id="canvas"></canvas>

+0

che tipo di animazione cosa vuoi realizzare? finora non è molto chiaro –

+0

Aggiungerò un'immagine con la rappresentazione, dammi solo un minuto. – Paran0a

risposta

5

Questo perché si imposta dir [+ -1, -1 +] invece [dx, dy] (lo spostamento reale), che non sono sempre proporzionale.

Dai un'occhiata alla frammento modificato:

var canvas = document.getElementById('canvas'); 
 
var ctx = document.getElementById('canvas').getContext('2d'); 
 

 
var x; 
 
var y; 
 

 
var tx = tx || 0; 
 
var ty = ty || 0; 
 

 
var xDir; 
 
var yDir; 
 

 
function followMouse(e) { 
 
    x = e.pageX; 
 
    y = e.pageY; 
 
    moveObject(); 
 
} 
 

 
function moveObject() { 
 
    ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
    var scale = 0.2 * Math.max(canvas.width, canvas.height); 
 
    xDir = (x - tx)/scale; 
 
    yDir = (y - ty)/scale; 
 
    tx = tx != x ? tx + xDir : tx; 
 
    ty = ty != y ? ty + yDir : ty; 
 

 

 
    ctx.fillRect(tx - 25, ty + 25, 50, 10); 
 
    if (tx != x || ty != y) { 
 
    window.requestAnimationFrame(moveObject); 
 
    } 
 
} 
 

 
function resizeCanvas() { 
 
    canvas.width = window.innerWidth; 
 
    canvas.height = window.innerHeight; 
 
}; 
 

 
canvas.addEventListener('mousemove', _.throttle(function(e) { 
 
    followMouse(e); 
 
}, 100)); 
 

 
window.addEventListener('resize', resizeCanvas, false); 
 

 
resizeCanvas();
html, 
 
body { 
 
    margin: 0; 
 
    height: 100%; 
 
} 
 
canvas { 
 
    display: block; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<canvas id="canvas" style="border: 1px solid black"></canvas>

+0

Mi prenderò un po 'di tempo per cercare di capire la tua risposta. Lo contrassegnerò come corretto allora. Qualche possibilità di sapere perché inizia a rallentare dopo aver spostato il mouse un po '. Prova a muovere il mouse su e giù/a destra ea sinistra per 20 secondi, dopodiché vedrai che il movimento non è più fluido. Atleast nel mio caso. – Paran0a

+0

è leggermente in ritardo perché il ritardo di 100 ms è piuttosto ampio. Metti 30 ms per avere qualcosa di più liscio. Ma sinceramente non ho provato a sistemare l'intero codice, solo il problema di direzione che hai menzionato –

+0

Ok, mi sono chiesto se forse lo sapessi. Grazie per la risposta! – Paran0a