Created
June 28, 2016 19:28
-
-
Save Otto42/a902376a6d70fe77563f35d561403c2f to your computer and use it in GitHub Desktop.
An animated red ball SVG. Move it around with WASD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" | |
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | |
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"> | |
<circle cx="50" cy="50" r="50" fill="red" /> | |
<script type="text/javascript"><![CDATA[ | |
var KEY = { w:87, a:65, s:83, d:68 }; | |
var moveSpeed = 5; | |
var circle = document.getElementsByTagName("circle")[0]; | |
var x = circle.getAttribute('cx')*1, | |
y = circle.getAttribute('cy')*1; | |
document.documentElement.addEventListener('keydown',function(evt){ | |
switch (evt.keyCode){ | |
case KEY.w: | |
circle.setAttribute('cy',y-=moveSpeed); | |
// Alternatively: | |
// circle.cy.baseVal.value = (y-=moveSpeed); | |
break; | |
case KEY.s: | |
circle.setAttribute('cy',y+=moveSpeed); | |
break; | |
case KEY.a: | |
circle.setAttribute('cx',x-=moveSpeed); | |
break; | |
case KEY.d: | |
circle.setAttribute('cx',x+=moveSpeed); | |
break; | |
} | |
},false); | |
]]></script> | |
</svg> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have to view image in new tab to move it around.