5

Its my first time here and I don't know how to indent this sorry :/

I have an image of a van and I am trying to move it across the screen to as if it is driving. Once that is done I will scale the image to appear as if it is moving away (and getting smaller).

I need this to be done in standard javascript without any packages (such as JQuery) please.

What I've got is a van which for a reason I can't break down is moving along 2 paths instead of one. Also moving in the wrong direction (it should move along the path y=-25x so that every 25 pixels moved to the right it should move 1 pixel upwards).

To illustrate what I am trying to achieve, please see this image: https://i.stack.imgur.com/9WIfr.jpg

This is my javascript file:

var viewWidth = 800;        
var viewHeight = 480;        
var fps = 30;        
var delay = getFrame(fps);        
var vanWidth, vanHeight, vanObj;   

function initVan() {        
  vanObj = document.getElementById("van");        
  vanObj.style.position = "absolute";        
  vanObj.src = "pics/delivery/van.png";        
  vanWidth = 413;        
  vanHeight = 241;        
  var startX = 0-vanWidth;        
  var startY = viewHeight-vanHeight;        
  setPosition(startX,startY);        
  transition(startX,startY,3000);        
}

function transition(startX,startY,time) {        
  //the intention of this is to follow a path y=-25x in mathematical terms
  var endX = viewWidth;        
  var endY = startY-(endX/-25);        
  //note that this is the velocity per millisecond
  var velocityX = (endX-startX)/time;        
  var velocityY = (endY-startY)/time;        
  alert(endY+", "+startY);        
  move(velocityX,velocityY,endX,endY);        
}

function move(vX,vY,eX,eY) {        
  var posX = getX();        
  var posY = getY();        
  if (posX<=eX || posY<=eY) {        
    //velocityX (in milliseconds) * delay = the amount of pixels moved in one frame @fps=30
    var moveX = vX*delay;        
    var moveY = vY*delay;        
    var newX = posX+moveX;        
    var newY = posY+moveY;        
    setPosition(newX,newY);        
    setTimeout(function() {        
        move(vX,vY,eX,eY);        
    }, delay);        
  }        
} 


function getX() {        
  return vanObj.offsetLeft;        
}    

function getY() {        
  return vanObj.offsetTop;        
}  

function setPosition(newX,newY) {        
  vanObj.style.left = newY + "px";        
  vanObj.style.top = newX + "px";        
}        

function setSize(scaleX,scaleY) {        
  vanWidth *= scaleX;        
  vanHeight *= scaleY;        
  vanObj.width = vanWidth;        
  vanObj.height = vanHeight;        
}      

function getFrame(fps) {        
  return Math.floor(1000/fps);        
}  

This is my HTML file:

<script type="text/javascript" src="delivery.js"> </script>
<body onLoad="initVan();">
<img id="van" width=413 height=241/>

Ozzy
  • 8,244
  • 7
  • 55
  • 95
  • How to format Stack Overflow posts: http://stackoverflow.com/editing-help – Matt Ball Nov 05 '11 at 17:09
  • You've indented it rather well by the way. The best way is to simple paste your indented code, highlight it in the editor, and press the code button (or the CTRL+K combination). – Madara's Ghost Nov 05 '11 at 17:10
  • Thanks, actually it wouldn't allow me to post this and the error message said use CTRL+K so I did that. – Ozzy Nov 05 '11 at 17:17

2 Answers2

5

Unless you have a no-libraries requirement, or particularly enjoy reinventing the wheel, I'd solve this using jQuery's effects library, and in particular .animate: http://api.jquery.com/animate/. See the first example on that page.

$(document).ready(function() {
  $('#van')
    .attr({
      width: 413,
      height: 241  //etc.
    })
    .animate({
      width: "70%",
      height: "70%"  //etc.
  }, 3000);
});

Less code means less maintenance. Means happy customer.

Gaute Løken
  • 7,522
  • 3
  • 20
  • 38
  • Thanks for your answer, but I really have no desire to incorporate a 243kb javascript file on a very simplistic (with the exception of this 1 javascript animation) website. – Ozzy Nov 05 '11 at 17:33
  • 2
    You don't have to serve the javascript file. Use the CDN. That way the js-file that the browser is probably allready caching from using it on another site will be used for yours as well. – Gaute Løken Nov 05 '11 at 17:43
  • 1
    The minified and gzipped version of jQuery is only ~30kb. And if you get it from Googles cdn (https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js), theres a great change that the user has loaded it from some other page already (because almost everybody gets it from Google). Edit: Mithon was first – Webbies Nov 05 '11 at 17:43
  • Ohh, I understand. Thanks guys. I quit javascript a long time ago before this JQuery package came, so I missed out a lot. – Ozzy Nov 05 '11 at 17:45
2

Even though you already accepted an answer, heres a way to do it without jQuery.

Not finished, but the concept works.

   window.onload = function () {
    updateVan(0);
    function updateVan(i)
    {
        var t = setTimeout(function () {
            document.getElementById("van").style.marginLeft = i + "px";
            document.getElementById("van").style.marginTop = (i/10) + "px";
            document.getElementById("van").style.height = (100-(i/10)) + "px";
            document.getElementById("van").style.width = (100-(i/10)) + "px";
            if (i < 300) updateVan(i+1);
        },30);
    }
}

Working demo here: http://webbies.dk/tmp/tmp.html

Webbies
  • 947
  • 9
  • 17