javascript - Moving content of canvas with a specific framerate -


so have sprite animation goes fine when it's not in movement (i need go on 20 fps max because if not animation fast), when try move across screen, movement looks choppy. ideas on how make movement smoothly? increase framerate works, i'd see if there option aside of that.

var canvasa = document.createelement("canvas"); var canvasb = document.createelement("canvas");  var contexta = canvasa.getcontext("2d"); var contextb = canvasb.getcontext("2d");  canvasa.style.position = "absolute"; canvasb.style.position = "absolute";  var xpos = 0; var ypos = 0; var index = 0; var framesizex = 140; var framesizey = 395; var numframes = 20; var drawing_character = false; var xposition = 0; var now, then, elapsed, starttime, fps, fpsinterval; var fpscounter = setinterval(updatefps, 1000); var forw = true; var cfps = 0;  // pictures var background = new image(); background.src = "http://i.imgur.com/3fdb45h.png";  var character = new image(); character.src = "http://i.imgur.com/tzijajg.png";  function redraw() {     contextb.clearrect(0, 0, canvasb.width, canvasb.height);     contextb.drawimage(background, 0, 0); }  function drawchar() { // input= fps max     requestanimationframe(drawchar);      = date.now();     elapsed = - then;      if (elapsed > fpsinterval) {         = - (elapsed % fpsinterval);          contexta.clearrect(0, 0, 800, 600);            contexta.drawimage(character,                                    xpos,         ypos,         framesizex,         framesizey,         xposition,         0,         framesizex,         framesizey);          xpos += framesizex;         index += 1;         if (index >= numframes) {             xpos = 0;             ypos = 0;             index = 0;         } else if (xpos + framesizex > character.width) {             xpos = 0;             ypos += framesizey;         }          cfps++;     } }  function updatefps() {     document.getelementbyid("fps").innerhtml = "fps: " + cfps;     cfps = 0; }  function aqui() {     preparecanvas();     setinterval(redraw, 1000/60);     animarchar(20);      setinterval(move, 1); }  function move() {     if (forw === true) {         xposition += 1;         if (xposition === 300){             forw = false;}     } else {         xposition -= 1;         if (xposition === 0){             forw = true;}     } }  function preparecanvas() {     canvasb.width = 800;     canvasb.height = 600;     canvasa.width = canvasb.width;     canvasa.height = canvasb.height;     //document.body.appendchild(canvasb);     document.body.appendchild(canvasa); }  function animarchar(fps) {     fpsinterval = 1000 / fps;     = date.now();     starttime = then;     drawchar(); }  window.onload = function () {     aqui(); }; 

here jsfiddle: https://jsfiddle.net/qb171ghf/


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -