Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1.  
  2. var Roller = ( function ( ) {
  3. var canvas = null,
  4. ctx = null,
  5. arc = 0,
  6. cWidth = 0,
  7. cHeight = 0,
  8. centerPos = null,
  9. currentAngle = 0,
  10. progressSpeed = .001,
  11. rolledItems = [ ],
  12. rollAnimation = null,
  13. animationInfo = null,
  14. arrowLoaded = false,
  15. arrowImg = new Image( ),
  16. defaultWeapon = new Image( );
  17.  
  18. var Config = {
  19. itemsNumOrg: 4,
  20. itemsNum: 4,
  21. defaultWeaponImage: 'https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17P7NdTRH-t26q4SZlvD7PYTQgXtu5cB1g_zMyoD0mlOx5UM5ZWClcYCUdgU3Z1rQ_FK-xezngZO46MzOziQ1vSMmtCmIyxfkgx5SLrs4SgJFJKs/96fx96f',
  22. defaultWeaponObject: null,
  23. imageWidth: 96
  24. };
  25.  
  26. function Roller( ) {
  27. canvas = document.getElementById( 'roller' );
  28. ctx = canvas.getContext( '2d' );
  29.  
  30. arc = ( 360 / Config.itemsNum ) * Math.PI / 180;
  31. currentAngle = -arc * 2;
  32.  
  33. cWidth = canvas.width;
  34. cHeight = canvas.height;
  35. centerPos = { x: cWidth / 2, y: cHeight / 2 + 15 };
  36.  
  37. arrowImg.src = 'static/images/roller_arrow.png';
  38. arrowImg.onload = drawArrow;
  39.  
  40. defaultWeapon.src = Config.defaultWeaponImage;
  41. defaultWeapon.onload = saveDefaultWeapon;
  42.  
  43. rollAnimation = requestAnimationFrame( drawRoller );
  44. }
  45.  
  46. var updateNum = function (num) {
  47. Config.itemsNum = num;
  48. arc = ( 360 / Config.itemsNum ) * Math.PI / 180;
  49. currentAngle = -arc * 2
  50. };
  51.  
  52. var drawRoller = function( ) {
  53. ctx.clearRect( 0, 0, cWidth, cHeight );
  54.  
  55. ctx.beginPath( );
  56. ctx.arc( centerPos.x, centerPos.y, cWidth / 2 - 5, 0, 2 * Math.PI, false );
  57. ctx.arc( centerPos.x, centerPos.y, cWidth / 2, 2 * Math.PI, 0, true );
  58. ctx.fillStyle = '#09090b';
  59. ctx.fill( );
  60. ctx.closePath( );
  61.  
  62. if( animationInfo !== null ) {
  63. var now = Date.now( ),
  64. p = ( now - animationInfo.start ) / animationInfo.duration,
  65. c = moveAnim( p );
  66.  
  67. if( now - animationInfo.start < animationInfo.duration )
  68. currentAngle = animationInfo.dest * c;
  69. else {
  70. if( typeof animationInfo.callback === 'function' ) {
  71. animationInfo.callback( );
  72.  
  73. animationInfo.callback = null;
  74. }
  75. }
  76. } else {
  77. currentAngle += progressSpeed;
  78. }
  79.  
  80. for( var i = 0; i < Config.itemsNum; i++ ) {
  81. var angle = currentAngle - i * arc,
  82. weapon = rolledItems[ i ];
  83.  
  84. drawImage( weapon, angle, i );
  85. }
  86.  
  87. if( arrowLoaded )
  88. drawArrow( );
  89.  
  90. rollAnimation = requestAnimationFrame( drawRoller );
  91. };
  92.  
  93. var drawArrow = function( ) {
  94. ctx.drawImage( arrowImg, centerPos.x - arrowImg.width / 2, 0 );
  95.  
  96. arrowLoaded = true;
  97. };
  98.  
  99. var saveDefaultWeapon = function( ) {
  100. Config.defaultWeaponObject = this;
  101. };
  102.  
  103. var moveAnim = function( n ) {
  104. return --n * n * n + 1;
  105. };
  106.  
  107. var drawImage = function( weapon, angle, i ) {
  108. var th1 = angle,
  109. th2 = angle + arc,
  110. th3 = ( th1 + th2 ) / 2,
  111. position = applyAngle( centerPos, th3, ( cWidth - Config.imageWidth ) / 2 - 30 );
  112.  
  113. var position2 = applyAngle( centerPos, th3, 40 );
  114.  
  115. ctx.fillStyle = !( i % 2 ) ? 'rgba(9, 9, 11, .3)' : 'rgba(255, 255, 255, .05)';
  116.  
  117. ctx.beginPath( );
  118. ctx.arc( centerPos.x, centerPos.y, cWidth / 2 - 5, angle, angle + arc, false );
  119. ctx.arc( centerPos.x, centerPos.y, 0, angle + arc, angle, true );
  120. ctx.fill( );
  121. ctx.closePath( );
  122.  
  123. ctx.fillStyle = '#fff';
  124.  
  125. if( weapon ) {
  126. var text_width = (ctx.measureText(weapon['percent']).width / 2);
  127.  
  128. ctx.font = "300 12px Montserrat";
  129. ctx.fillText(weapon['price'], position.x, position.y + 28 );
  130.  
  131. ctx.font = "500 20px Montserrat";
  132. ctx.fillText(weapon['percent'], position2.x - text_width, position2.y );
  133.  
  134. if( weapon.imageObject )
  135. ctx.drawImage( weapon.imageObject, position.x - Config.imageWidth / 2, position.y - Config.imageWidth / 2 );
  136. else {
  137. var img = new Image( );
  138.  
  139. img.src = weapon.image;
  140. img.onload = function( ) { weapon.imageObject = this; };
  141. }
  142. } else {
  143. if( Config.defaultWeaponObject )
  144. ctx.drawImage( Config.defaultWeaponObject, position.x - Config.imageWidth / 2, position.y - Config.imageWidth / 2 );
  145. }
  146. };
  147.  
  148. var applyAngle = function( point, angle, distance ) {
  149. return {
  150. x: point.x + ( Math.cos( angle ) * distance ),
  151. y: point.y + ( Math.sin( angle ) * distance )
  152. };
  153. };
  154.  
  155. Roller.prototype.updateItems = function( data ) {
  156. rolledItems = data;
  157. updateNum(rolledItems.length);
  158. };
  159.  
  160. Roller.prototype.startRoll = function( index, callback ) {
  161. if( index > Config.itemsNum - 1 )
  162. return;
  163.  
  164. var destAngle = 0;
  165.  
  166. destAngle += index * arc; //index * ilosc_katu_na_element
  167. destAngle += 6 * Math.PI; //ile razy ma sie obracac (polowa)
  168. destAngle -= (1 + Math.random()) * ((Math.PI / Config.itemsNum) * 2); //losowanie przesiniecia dla pojedynczego elementu
  169.  
  170. animationInfo = {
  171. duration: 10000,
  172. start: new Date( ),
  173. dest: destAngle,
  174. callback: callback
  175. };
  176. };
  177.  
  178. Roller.prototype.restartRoll = function ( ) {
  179. animationInfo = null;
  180. rolledItems = [ ];
  181. updateNum(Config.itemsNumOrg);
  182. };
  183.  
  184. return Roller;
  185. } )( );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement