Advertisement
0x3c0

rotate4

Jul 16th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.94 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='us-ascii'>
  5. <title>rotate4</title>
  6. <style>
  7. html{background-color:black;}
  8. body{text-align:center;}
  9. #board{position:relative;background-color:#101020;color:white;font-size:25px;width:8em;height:8em;display:inline-block;-moz-user-select:none;-webkit-user-select:none;}
  10. #board .piece{transition:left 0.2s,top 0.2s,color 0.2s;transition-timing-function:ease-in-out;position:absolute;width:1.52em;height:1.52em;line-height:1.52em;font-family:"Ubuntu Mono";font-weight:bold;margin:0.2em;border:0.04em solid #444;background-color:black;}
  11. #board .interface{position:absolute;top:0;left:0;opacity:0;width:100%;height:100%;border:none;padding:0;cursor:default;}
  12. </style>
  13. </head>
  14. <body>
  15. <div id='board'></div>
  16. <div>
  17. <button>toggle mouse input</button>
  18. </div>
  19. <script>
  20. var B=document.getElementById('board');
  21. var I;
  22. var divs=[];
  23. var state=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
  24. var letters='abcdefghijklmnop';
  25.  
  26. function init()
  27. {
  28. for(var i=0;i<4;i++)
  29. for(var j=0;j<4;j++)
  30. {
  31. var div=document.createElement('div');
  32. div.className='piece';
  33. div.textContent=letters[i*4+j];
  34. B.appendChild(div);
  35. divs.push(div);
  36. }
  37. redraw();
  38. I=document.createElement('input');
  39. I.className='interface';
  40. B.appendChild(I);
  41. I.addEventListener('click',click,false);
  42. I.addEventListener('contextmenu',click,false);
  43. I.addEventListener('keydown',key,false);
  44. I.focus();
  45. }
  46.  
  47. function scramble()
  48. {
  49. state=[0];
  50. for(var i=1;i<16;i++)
  51. {
  52. var r=Math.floor(Math.random()*(i+1));
  53. state[i]=state[r];
  54. state[r]=i;
  55. }
  56. }
  57.  
  58. function redraw()
  59. {
  60. for(var i=0;i<4;i++)for(var j=0;j<4;j++)
  61. {
  62. var x=state[i+j*4]%4,y=state[i+j*4]>>2;
  63. divs[x+y*4].style.left=i*2+'em';
  64. divs[x+y*4].style.top=j*2+'em';
  65. divs[x+y*4].style.color=(i===x&&j===y)?'#9f9':'white';
  66. }
  67. }
  68.  
  69. function move(x,y,ccw)
  70. {
  71. var a=x+4*y,b=a+4,c=a+5,d=a+1;
  72. if(ccw){b=a+1;d=a+4;}
  73. state[a]=[state[b],
  74. state[b]= state[c],
  75. state[c]= state[d],
  76. state[d]= state[a]][0];
  77. }
  78.  
  79. function autoplay()
  80. {
  81. if(autoqueue.length===0)return;
  82. else
  83. {
  84. var m=autoqueue.shift();
  85. var x=m%3,y=(m-x)/3%3,ccw=Math.floor(m/9);
  86. move(x,y,ccw);
  87. redraw();
  88. setTimeout(autoplay,200);
  89. }
  90. }
  91.  
  92. var autoqueue=[];
  93.  
  94. function click(e)
  95. {
  96. e.preventDefault();
  97. var x=e.clientX-B.offsetLeft,y=e.clientY-B.offsetTop;
  98. var unit=2*parseInt(getComputedStyle(B).fontSize);
  99. x/=unit;
  100. y/=unit;
  101. if(x<0.5||x>=3.5||y<0.5||y>=3.5)return false;
  102. /*if(x<0.5)x=1;
  103. if(x>=3.5)x=3;
  104. if(y<0.5)y=1;
  105. if(y>=3.5)y=3;*/
  106. move(Math.floor(x-0.5),Math.floor(y-0.5),e.shiftKey^(e.type==='contextmenu'));
  107. redraw();
  108. }
  109.  
  110. function key(e)
  111. {
  112. e.preventDefault();
  113. var keymap={
  114. 55:0,56:1,57:2,71:3,67:4,82:5,72:6,84:7,78:8,//dvorak
  115. 85:3,73:4,79:5,74:6,75:7,76:8,//qwerty
  116. 36:0,38:1,33:2,37:3,12:4,39:5,35:6,40:7,34:8,//numpad, numlock off
  117. 103:0,104:1,105:2,100:3,101:4,102:5,97:6,98:7,99:8//numpad, numlock on
  118. };
  119. if(e.keyCode in keymap)
  120. {
  121. var m=keymap[e.keyCode];
  122. move(m%3,(m-m%3)/3,e.shiftKey);
  123. redraw();
  124. }
  125. }
  126.  
  127. init();
  128. </script>
  129. </body>
  130. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement