Advertisement
Ludwiq

Untitled

Jul 29th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <svg id="masterSVG" xmlns="http://www.w3.org/2000/svg" version="1.1" width="1900" height="930">
  8.         <rect id="rect" x="0" y="90" width="150" height="150" style="fill:red; fill-opacity:0; stroke:blue; stroke-opacity:1" />
  9.  
  10.         <!-- Prostokąt wykonany moim sposobem -->
  11.         <rect id="rect1" x="1000" y="100" width="100" height="100" style="fill:red" />
  12.  
  13.         <!-- Sposób wykorzystujący animacje svg -->
  14.         <rect id="rect2" x="1000" y="100" width="100" height="100" style="fill:green">
  15.             <animate attributename="x" attributetype="XML" from="1000" to="-1000" begin="2" dur="10s" fill="freeze" repeatcount="indefinite" />
  16.         </rect>
  17.  
  18.         <script>
  19.             //funckja przesuwająca prostokąt
  20.             function move(deltaTime) {
  21.                 //deltaTime dostosowuje prędkość obiektu do ilości wyświetlonych klatek na sekundę
  22.  
  23.                 var rect1 = document.getElementById("rect1"); //obiekt o id rect1
  24.                 var speed = 10 * deltaTime; //predkosc
  25.  
  26.                 rect1.setAttribute("x", rect1.getAttribute("x") - speed); //przesunięcie obiektu o "speed"
  27.  
  28.                 var xPosition = rect1.getAttribute("x"); //pozycja x
  29.                 console.log(xPosition);
  30.             }
  31.  
  32.             //co to jest requestAnimationFrame: http://creativejs.com/resources/requestanimationframe/
  33.             var animFrame = window.requestAnimationFrame ||
  34.             window.webkitRequestAnimationFrame ||
  35.             window.mozRequestAnimationFrame ||
  36.             window.oRequestAnimationFrame ||
  37.             window.msRequestAnimationFrame ||
  38.             null;
  39.  
  40.             //sprawdzam czy przeglądarka obsługuje requestAnimationFrame
  41.             if (animFrame !== null) {
  42.                 //jest obsługiwane
  43.                 var recursiveAnim = function () {
  44.                     mainloop();
  45.                     animFrame(recursiveAnim); //funkcja wykonuje samą siebie; jest nieskończona pętla
  46.                 };
  47.                 animFrame(recursiveAnim); //animFrame działa tak, że wykonuje funkcję tak szybko, jak przeglądarka będzie na to gotowa
  48.             } else {
  49.                 //nie jest obsługiwane, użyj setInterval aby zapętlić wykonywanie funkcji
  50.                 var ONE_FRAME_TIME = 1000.0 / 60.0;
  51.                 setInterval(mainloop, ONE_FRAME_TIME);
  52.             }
  53.  
  54.             var lastUpdate = Date.now();
  55.  
  56.             //główna pętla gry
  57.             var mainloop = function () {
  58.                 var now = Date.now();
  59.                 var fps = 1000 / (now - lastUpdate); //ilość klatek na sekundę
  60.                 var dt = (now - lastUpdate) / 10; //obliczanie deltaTime
  61.                 lastUpdate = now;
  62.  
  63.                 move(dt);
  64.             }
  65.         </script>
  66.     </svg>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement