Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /** 文段集合 */
  2. var ParagraphsHub = (function() {
  3.  
  4. var data = [
  5. '6666666666666666',
  6. '第一第一',
  7. '哪哪哪哪',
  8. '测试',
  9. '什么什么什么',
  10. '67676767676',
  11. '垃圾啊',
  12. '什么鬼呀',
  13. '快来看呀',
  14. '我的我的我的',
  15. '坏蛋坏蛋',
  16. '刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏刷屏',
  17. '6666666666666666666666666',
  18. '什么东西hahahhahahhahah',
  19. '哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈',
  20. '啊',
  21. '!',
  22. '测试'
  23. ];
  24.  
  25. var dataCount = data.length;
  26.  
  27. return {
  28. fetch: function(count) {
  29. // 模拟 数据 获取 逻辑可以改掉, 比如改成从远程服务器获取,
  30. // 只要返回结果是个Promise并且最后得到的是一个字符串数组
  31.  
  32. var rCount = Math.floor(Math.random() * count + 1);
  33. var list = [];
  34.  
  35. while (rCount--) {
  36. var index = Math.floor(Math.random() * dataCount);
  37. list.push(data[index]);
  38. }
  39.  
  40. return new Promise(function(resolve, reject) {
  41. resolve(list);
  42. });
  43. }
  44. };
  45.  
  46. })();
  47.  
  48.  
  49. /**
  50. * 弹幕舞台
  51. * ------------------------
  52. * | xxxxx xxxxx x|***
  53. * | xxxxxxxxxxx xxx|******** *****
  54. * | x xxx |
  55. * | |
  56. * ------------------------
  57. */
  58.  
  59. var DMStage = (function() {
  60. var TRACKS = 5;
  61. var MAX_TRACKS = 10;
  62.  
  63. // constructor
  64. function DMStage(canvasId) {
  65. this._canvas = document.getElementById(canvasId);
  66.  
  67. // 挂起弹幕数
  68. this._pendingParagraphs = [];
  69. // 弹幕轨道
  70. this._paragraphTracks = [];
  71. // 弹幕轨道排版中最后一条的位置
  72. this._lastParagraphPos = [];
  73.  
  74. window.addEventListener('resize', _resize.bind(this));
  75.  
  76. _resize.call(this);
  77. }
  78.  
  79. // public methods //////////////////////////////////////////////////////////
  80.  
  81. var api = DMStage.prototype;
  82.  
  83. api.show = function(text) {
  84. //var textWidth = _measureText.call(this, text)
  85. console.log(text);
  86. };
  87.  
  88. // private methods /////////////////////////////////////////////////////////
  89.  
  90. function _resize() {
  91. this._canvas.width = document.documentElement.clientWidth;
  92. this._canvas.height = document.documentElement.clientHeight;
  93. }
  94.  
  95. function _measureText(text) {
  96. var ctx = this._canvas.getContext("2d");
  97. ctx.font = "30px Arial";
  98. return ctx.measureText(text);
  99. }
  100.  
  101.  
  102. return DMStage;
  103. })();
  104.  
  105.  
  106. // main ////////////////////////////////////////////////////////////////////////
  107.  
  108. var stage = new DMStage('dm-stage');
  109.  
  110. // 弹幕更新
  111. next(100);
  112.  
  113. function next(delay) {
  114. setTimeout(function() {
  115. ParagraphsHub.fetch(30)
  116. .then(function(list) {
  117. stage.show(list);
  118.  
  119. // 下一波
  120. next();
  121. });
  122. }, delay || Math.floor(Math.random() * 6000 + 1000));
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement