Advertisement
fbcyborg

bouncingballinputsvalidate.html

Nov 22nd, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.07 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <title>Bouncing Ball with inputs</title>
  4.     <style>
  5.     form {
  6.     width:330px;
  7.     margin:20px;
  8.     background-color:brown;
  9.     padding:20px;
  10. }
  11. input:valid {background:green;}
  12. input:invalid {background:red;}
  13.     </style>
  14.     <script type="text/javascript">
  15.     var cwidth = 400;
  16.     var cheight = 300;
  17.     var ballrad = 10;
  18.     var boxx = 20;
  19.     var boxy = 30;
  20.     var boxwidth = 350;
  21.     var boxheight = 250;
  22.     var boxboundx = boxwidth+boxx-ballrad;
  23.     var boxboundy = boxheight+boxy-ballrad;
  24.     var inboxboundx = boxx+ballrad;
  25.     var inboxboundy = boxy+ballrad;
  26.     var ballx = 50;
  27.     var bally = 60;
  28.     var ctx;
  29.     var ballvx = 4;
  30.     var ballvy = 8;
  31.    
  32. function init(){  
  33. ctx = document.getElementById('canvas').getContext('2d');  
  34.   ctx.lineWidth = ballrad;
  35.   moveball();  
  36.   setInterval(moveball,100);  
  37. }  
  38. function moveball(){  
  39.    ctx.clearRect(boxx,boxy,boxwidth,boxheight);
  40.    moveandcheck();
  41.   ctx.beginPath();
  42.   ctx.fillStyle ="rgb(200,0,50)";
  43.   ctx.arc(ballx, bally, ballrad,0,Math.PI*2,true);
  44.   ctx.fill();
  45.    ctx.strokeRect(boxx,boxy,boxwidth,boxheight);
  46. }
  47. function moveandcheck() {
  48.     var nballx = ballx + ballvx;
  49.     var nbally = bally +ballvy;
  50.  
  51.   if (nballx > boxboundx) {
  52.      ballvx =-ballvx;
  53.      nballx = boxboundx;
  54.   }
  55.   if (nballx < inboxboundx) {
  56.     nballx = inboxboundx
  57.      ballvx = -ballvx;
  58.  }
  59.  if (nbally > boxboundy) {
  60.      nbally = boxboundy;
  61.      ballvy =-ballvy;
  62.   }
  63.   if (nbally < inboxboundy) {
  64.    nbally = inboxboundy;
  65.      ballvy = -ballvy;
  66.  }
  67.  ballx = nballx;
  68.  bally = nbally;
  69. }
  70. function change() {
  71.     ballvx = Number(f.hv.value);
  72.     ballvy = Number(f.vv.value);
  73.     return false;
  74. }
  75. </script>
  76. </head>
  77. <body onLoad="init();">  
  78. <canvas id="canvas" width="400" height="300">
  79. Your browser doesn't support the HTML5 element canvas.
  80. </canvas>  
  81. <br/>
  82. <form name="f" id="f" onSubmit="return change();">
  83.   Horizontal velocity <input name="hv" id="hv" value="4" type="number" min="-10" max="10" />
  84. <br>
  85.   Vertical velocity <input name="vv" id="vv" value="8" type="number" min="-10" max="10"/>
  86. <input type="submit" value="CHANGE"/>
  87. </form>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement