Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Animation</title>
- </head>
- <script language="javascript">
- var ball = new Array(4);
- var curPos = 0;
- var startRolling;
- var origX = 0;
- var origY = 0;
- var powerVal = 1;
- var shot = 0;
- // assigning ball0.png to ball[0], ball1.png to ball[1], etc
- for (i=0; i < ball.length; i++) {
- ball[i] = "ball"+i+".png";
- }
- function moveRight(object, amount) {
- var newPos = parseInt(object.style.left);
- newPos = newPos+amount;
- object.style.left = newPos + "px";
- console.log("X:"+object.x);
- }
- function moveDown(object, amount) {
- var newPos = object.y;
- newPos = newPos+amount;
- object.style.top = newPos + "px";
- console.log("Y:"+object.y);
- }
- function rotateImg(imgToRotate, arrayOfImg, current) {
- if ((current >= (arrayOfImg.length-1)) || (current < 0)) {
- current = 0;
- } else {
- current++;
- }
- // change image source to the current image, e.g. ball[2].png
- imgToRotate.src = arrayOfImg[current];
- return current;
- }
- function roll() {
- var b = document.animatedBall;
- var bin = document.bin;
- b.style.display = "block";
- document.getElementById("rollButton").style.display = "none";
- if (origX == 0) {
- origX = getX(b);
- }
- if (origY == 0) {
- origY = getY(b);
- }
- curPos = rotateImg(b, ball, curPos);
- shot = powerVal*20;
- if (parseInt(b.style.left) < 530) {
- moveRight(b, 5);
- } else {
- shot = shot + 540;
- moveImg(b, shot, 235, 50);
- console.log("shot:"+shot);
- }
- if (getY(b) > 230) {
- var text = document.getElementById("wonLost");
- if (shot == getX(bin)) {
- moveDown(b, 45);
- text.innerHTML = "You won!";
- b.style.display = "none";
- } else {
- text.innerHTML = "Try again";
- }
- clearInterval(startRolling);
- }
- }
- function getX(image) {
- return parseInt(image.style.left);
- }
- function getY(image) {
- return image.y;
- }
- function moveImg(image, endX, endY, totalSteps) {
- var moveX = endX / totalSteps;
- if (moveX < 1) {
- moveX = 1;
- }
- var moveY = endY / totalSteps;
- if (moveY < 1) {
- moveY = 1;
- }
- if (getX(image) <= endX) {
- moveRight(image, moveX);
- }
- if (getY(image) <= endY) {
- moveDown(image, moveY);
- }
- }
- function resetBall(x, y) {
- var b = document.animatedBall;
- b.style.left = x + "px";
- b.style.top = y + "px";
- clearInterval(startRolling);
- document.getElementById("wonLost").innerHTML = "";
- document.getElementById("rollButton").style.display = "inline";
- document.animatedBall.style.display = "block";
- showAndSetPower(powerVal);
- randomBinPosition();
- }
- function randomBinPosition() {
- var r=Math.floor(Math.random()*5);
- r++;
- r = r*20;
- r = r+540;
- document.bin.style.left = r+"px";
- document.getElementById("binPos").innerHTML = r;
- }
- function showAndSetPower(newVal) {
- document.getElementById("powerDisplay").innerHTML=newVal;
- document.getElementById("shotPos").innerHTML = 540+(newVal*20);
- powerVal = newVal;
- }
- </script>
- </head>
- <body style="font-family:sans-serif;" onLoad="origX=40;origY=13;resetBall(origX, origY);">
- <img src="ball0.png" name="animatedBall"
- style="position:absolute;left:550px; top:280px; z-index:1" width="50">
- <img src="desk.jpg" name="desk"
- style="position:absolute; left:-50px; top:60px; z-index:-1;">
- <img src="bin.png" name="bin" width="55"
- style="position:absolute; left:550px; top: 280px; z-index:2;">
- <form style="position:absolute; top:200px; left:100px;">
- <input type="button" name="roll" id="rollButton" value="Roll"
- onClick="startRolling=setInterval('roll()',10);">
- <input type="button" name="reset" value="Reset"
- onClick="resetBall(origX, origY);">
- <b>Power: </b>
- <input type="range" name="power" id="power" value="1" min="1" max="5" style="max-width:75px;"
- onChange="showAndSetPower(this.value);"><span id="powerDisplay">1</span>
- <br>
- <span id="wonLost" style="color:red; position:absolute; top:50px; left:100px; font-size:1.2em;"></span>
- <div id="valueLog" style="font-size:0.6em; top:120px; left:300px; position:absolute;">
- Bin: <span id="binPos" style="">0</span><br>
- Shot: <span id="shotPos" style="">0</span></div>
- </form>
- <hr style="position:absolute; top:340px; min-width:700px; min-height:5px;">
- <p style="position:absolute; font-size:0.8em; top:400px; max-width:320px; line-height:1.5em; left: 100px">The bin to the right of the desk will move to a random position every time you click the <em>Reset</em> button. You have to select a power, and click the <em>Roll</em> button. This will only work in browsers that support the HTML5 slider, the <em>range</em> element. Currently this means webkit-based browsers like Apple Safari or Google Chrome. More info <a style="color:#666" href="post.html">available here</a>.</p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement