GLB Canvas Replay Conversion

sdfsdf

  1. // ==UserScript==
  2. // @name GLB Canvas Replay Conversion
  3. // @namespace pbr/canvas
  4. // @include http://goallineblitz.com/game/replay.pl?game_id=*&pbp_id=*
  5. // @copyright 2012, pabst
  6. // @license (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/3.0/
  7. // @version 12.11.30
  8. // @description sdfsdf
  9. // ==/UserScript==
  10.  
  11. /*
  12. *
  13. * pabst did this 12/11/27+
  14. *
  15. *
  16. */
  17.  
  18. unsafeWindow.pause();
  19. document.getElementById("offense_container").style.visibility = "hidden";
  20. document.getElementById("defense_container").style.visibility = "hidden";
  21. document.getElementById("offense_container").style.display = "none";
  22. document.getElementById("defense_container").style.display = "none";
  23.  
  24. var play_data = null;
  25. var imageData = [];
  26.  
  27. if (!window.requestAnimationFrame) {
  28. requestAnimFrame = (function () {
  29. return window.requestAnimationFrame ||
  30. window.webkitRequestAnimationFrame ||
  31. window.mozRequestAnimationFrame ||
  32. window.oRequestAnimationFrame ||
  33. window.msRequestAnimationFrame ||
  34. function(callback, element) {
  35. console.log("no request animation frame!!");
  36. window.setTimeout(callback, 1000 / 20);
  37. };
  38. }) ();
  39. };
  40.  
  41. window.setTimeout(
  42. function() {
  43. if (unsafeWindow.defaultFrameSpeed != 100) {
  44. console.log("replays must be set to 10fps for this script to work : "+unsafeWindow.defaultFrameSpeed);
  45. unsafeWindow.play();
  46. }
  47. else {
  48. main();
  49. }
  50. }
  51. , 100);
  52.  
  53. function main() {
  54. var container = document.getElementById("replay_container")
  55. container.style.height = "500px";
  56. container.style.width = "520px";
  57. var area = document.getElementById("replay_area");
  58. container.removeChild(area);
  59.  
  60. play_data = fixFrames();
  61. fixButtons();
  62.  
  63. initBackground();
  64. initForeground();
  65. initOffense();
  66. initDefense();
  67. initReplayInfo();
  68.  
  69. drawFrame(0,0);
  70. setTimeout(function() {
  71. isPlaying = true;
  72. document.getElementById("pause_button").visibility = "visible";
  73. document.getElementById("play_button").visibility = "hidden";
  74. render();
  75. }, 2500);
  76. }
  77.  
  78. function initBackground() {
  79. var canvas = document.createElement("canvas");
  80. canvas.style.position = 'absolute';
  81. canvas.setAttribute("id","background");
  82. canvas.setAttribute("width","520px");
  83. canvas.setAttribute("height","500px");
  84.  
  85. var container = document.getElementById("replay_container")
  86. container.appendChild(canvas);
  87.  
  88. var context = canvas.getContext("2d");
  89. context.save();
  90. }
  91.  
  92. function initForeground() {
  93. var canvas = document.createElement("canvas");
  94. canvas.style.position = 'absolute';
  95. canvas.setAttribute("id","foreground");
  96. canvas.setAttribute("width","520px");
  97. canvas.setAttribute("height","500px");
  98.  
  99. var container = document.getElementById("replay_container")
  100. container.appendChild(canvas);
  101.  
  102. var context = canvas.getContext("2d");
  103. context.save();
  104. }
  105.  
  106. function initOffense() {
  107. var canvas = document.createElement("canvas");
  108. canvas.style.position = 'absolute';
  109. canvas.style.top = "20px";
  110.  
  111. canvas.setAttribute("id","offense");
  112. canvas.setAttribute("width","20px");
  113. canvas.setAttribute("height","460px");
  114.  
  115. var container = document.getElementById("replay_container")
  116. container.appendChild(canvas);
  117.  
  118. canvas.addEventListener("click", offenseClicked, false);
  119. }
  120.  
  121. function offenseClicked() {
  122. renderOffenseLineup();
  123. }
  124.  
  125. function initDefense() {
  126. var canvas = document.createElement("canvas");
  127. canvas.style.position = 'absolute';
  128. canvas.style.left = "500px";
  129. canvas.style.top = "20px";
  130.  
  131. canvas.setAttribute("id","defense");
  132. canvas.setAttribute("width","20px");
  133. canvas.setAttribute("height","460px");
  134.  
  135. var container = document.getElementById("replay_container")
  136. container.appendChild(canvas);
  137.  
  138. canvas.addEventListener("click", defenseClicked, false);
  139. }
  140.  
  141. function defenseClicked() {
  142. renderDefenseLineup();
  143. }
  144.  
  145. function initReplayInfo() {
  146. var canvas = document.createElement("canvas");
  147. canvas.style.position = 'absolute';
  148. canvas.setAttribute("id","replayInfo");
  149. canvas.setAttribute("width","520px");
  150. canvas.setAttribute("height","20px");
  151.  
  152. var container = document.getElementById("replay_container")
  153. container.appendChild(canvas);
  154.  
  155. var canvas = document.createElement("canvas");
  156. canvas.style.position = 'absolute';
  157. canvas.style.top = "480px";
  158. canvas.setAttribute("id","playResults");
  159. canvas.setAttribute("width","520px");
  160. canvas.setAttribute("height","20px");
  161.  
  162. var container = document.getElementById("replay_container")
  163. container.appendChild(canvas);
  164. }
  165.  
  166. function getTeamBColor(id) {
  167. var team = unsafeWindow.ptid[id];
  168. var position = unsafeWindow.players[id].position;
  169. var fcolor = unsafeWindow.home_color2+"_border_home";
  170. var bcolor = unsafeWindow.home_color1;
  171. if (unsafeWindow.home != team) {
  172. fcolor = unsafeWindow.away_color2+"_border";
  173. bcolor = unsafeWindow.away_color1;
  174. }
  175. bcolor = bcolor.replace("mid_","medium");
  176. bcolor = bcolor.replace("_","");
  177. return bcolor;
  178. }
  179.  
  180. function getTeamFColor(bcolor) {
  181. var canvas = document.createElement("canvas");
  182. var context = canvas.getContext("2d");
  183. context.strokeStyle = bcolor;
  184. var color = context.strokeStyle.toString().slice(1);
  185. var r = parseInt(color.slice(0,2), 16);
  186. var g = parseInt(color.slice(2,4), 16);
  187. var b = parseInt(color.slice(4,6), 16);
  188. if ((r+g+b)/3 > 128) {
  189. return "black";
  190. }
  191. else {
  192. return "white";
  193. }
  194. }
  195.  
  196. function renderReplayInfo() {
  197. if (imageData["replayInfo"] == null) {
  198. var canvas = document.createElement("canvas");
  199. canvas.setAttribute("width","520px");
  200. canvas.setAttribute("height","20px");
  201. var context = canvas.getContext("2d");
  202.  
  203. var gradient = context.createLinearGradient(0,0,0,20);
  204. gradient.addColorStop(0,"rgb(100,100,100)");
  205. gradient.addColorStop(1,"rgb(40,40,40)");
  206.  
  207. context.lineWidth = 2;
  208. context.fillStyle = gradient;
  209. context.fillRect(0,0,520,20);
  210.  
  211. context.font = "bold 18px sans-serif";
  212. context.fillStyle = "#ffcc00";
  213. var info = document.getElementsByTagName("h1")[0].textContent;
  214. var length = context.measureText(info.toString());
  215. context.fillText(info.toString(), 260-length.width/2,16);
  216.  
  217. var p = play_data[0][1];
  218. var id = p.id;
  219. var bcolor = getTeamBColor(id);
  220. var fcolor = getTeamFColor(bcolor);
  221.  
  222. context.lineWidth = 2;
  223. context.fillStyle = bcolor;
  224. context.strokeStyle = fcolor;
  225.  
  226. context.beginPath();
  227. context.moveTo(0,0);
  228. context.lineTo(40, 0);
  229. context.lineTo(60,20);
  230. context.lineTo(0,20);
  231. context.lineTo(0,0);
  232. context.closePath();
  233. context.fill();
  234. context.stroke();
  235. context.font="bold 16px sans-serif";
  236. var score = document.getElementById("off_score").textContent;
  237. var length = context.measureText(score.toString());
  238. context.fillStyle = fcolor;
  239. context.fillText(score.toString(), 20-length.width/2,16);
  240.  
  241. var p = play_data[0][12];
  242. var id = p.id;
  243. var bcolor = getTeamBColor(id);
  244. var fcolor = getTeamFColor(bcolor);
  245.  
  246. context.lineWidth = 2;
  247. context.fillStyle = bcolor;
  248. context.strokeStyle = fcolor;
  249.  
  250. context.beginPath();
  251. context.moveTo(520,0);
  252. context.lineTo(480, 0);
  253. context.lineTo(460,20);
  254. context.lineTo(520,20);
  255. context.lineTo(520,0);
  256. context.closePath();
  257. context.fill();
  258. context.stroke();
  259.  
  260. context.font="bold 16px sans-serif";
  261. var score = document.getElementById("def_score").textContent;
  262. var length = context.measureText(score.toString());
  263. context.fillStyle = fcolor;
  264. context.fillText(score.toString(), 500-length.width/2,16);
  265.  
  266. imageData["replayInfo"] = canvas;
  267. }
  268. var canvas = document.getElementById("replayInfo");
  269. var context = canvas.getContext("2d");
  270. context.drawImage(imageData["replayInfo"],0,0);
  271.  
  272. if (imageData["playResults"] == null) {
  273. var canvas = document.createElement("canvas");
  274. canvas.setAttribute("width","520px");
  275. canvas.setAttribute("height","20px");
  276. var context = canvas.getContext("2d");
  277.  
  278. var gradient = context.createLinearGradient(0,0,0,20);
  279. gradient.addColorStop(0,"rgb(100,100,100)");
  280. gradient.addColorStop(1,"rgb(40,40,40)");
  281.  
  282. context.lineWidth = 2;
  283. context.fillStyle = gradient;
  284. context.fillRect(0,0,520,20);
  285.  
  286. var size = 14;
  287. do {
  288. context.font = "bold "+size+"px sans-serif";
  289. context.fillStyle = "#ffcc00";
  290. var info = document.getElementById("outcome_content").textContent;
  291. var length = context.measureText(info.toString());
  292. size--;
  293. }
  294. while (length.width > 520);
  295. context.fillText(info.toString(), 260-length.width/2,14);
  296. imageData["playResults"] = canvas;
  297. }
  298. var canvas = document.getElementById("playResults");
  299. var context = canvas.getContext("2d");
  300. context.drawImage(imageData["playResults"],0,0);
  301. }
  302.  
  303. function renderOffenseBar(frame, pct, loc) {
  304. if (imageData["offenseBar"] == null) {
  305. var canvas = document.createElement("canvas");
  306. canvas.setAttribute("width","20px");
  307. canvas.setAttribute("height","480px");
  308. var context = canvas.getContext("2d");
  309. var p = play_data[0][1];
  310. var id = p.id;
  311. var bcolor = getTeamBColor(id);
  312. var fcolor = getTeamFColor(bcolor);
  313.  
  314. context.lineWidth = 3;
  315. context.fillStyle = bcolor;
  316. context.strokeStyle = fcolor;
  317. context.fillRect(0,0, 20,460);
  318. context.strokeRect(0,0, 20,460);
  319. var teamName = document.getElementsByClassName("secondary_container")[0].textContent;
  320. context.fillStyle = fcolor;
  321. context.lineWidth=1;
  322. context.font="bold 14px sans-serif";
  323. context.save();
  324. context.rotate(Math.PI/2);
  325. context.fillText(teamName, 50,-6);
  326. context.restore();
  327. imageData["offenseBar"] = canvas;
  328. }
  329.  
  330. var canvas = document.getElementById("offense");
  331. var context = canvas.getContext("2d");
  332. context.drawImage(imageData["offenseBar"],0,0);
  333.  
  334. }
  335.  
  336. var offenseLineupVisible = false;
  337. function renderOffenseLineup(frame, pct, loc) {
  338. offenseLineupVisible = !offenseLineupVisible;
  339. if (offenseLineupVisible == true) {
  340. var canvas = document.createElement("canvas");
  341. canvas.setAttribute("id","offenseLineup");
  342. canvas.setAttribute("width","200px");
  343. canvas.setAttribute("height","480px");
  344. canvas.style.position = "absolute";
  345. canvas.style.left = "20px";
  346. var context = canvas.getContext("2d");
  347.  
  348. var id = unsafeWindow.play_data[0][1].id;
  349. var team = unsafeWindow.ptid[id];
  350. var bcolor = getTeamBColor(id);
  351. var fcolor = getTeamFColor(bcolor);
  352.  
  353. context.font="14px sans-serif";
  354. context.lineWidth = 2;
  355. context.fillStyle = bcolor;
  356. context.strokeStyle = fcolor;
  357. context.fillRect(0,130, 200,500);
  358. context.strokeRect(0,130, 200,500);
  359.  
  360. for (var i=1; i<12; i++) {
  361. var id = unsafeWindow.play_data[0][i].id;
  362. var position = unsafeWindow.players[id].position;
  363. var name = unsafeWindow.players[id].name;
  364.  
  365. var x = i;
  366. var y = 216;
  367. context.fillStyle = bcolor
  368. context.fillRect(0,(x-1)*24+y, 200,x*24+y);
  369. context.strokeRect(0,(x-1)*24+y, 200,x*24+y);
  370. context.fillStyle = fcolor;
  371. context.fillText(name, 25, x*24-10+y+3);
  372. context.drawImage(imageData[id], 5, (x-1)*24+y+4);
  373. }
  374.  
  375. var image = new Image();
  376. image.src = "http://goallineblitz.com/game/team_pic.pl?team_id="+team;
  377. var imgcanvas = document.createElement("canvas");
  378. var imgcontext = imgcanvas.getContext("2d");
  379. imgcontext.scale(75/image.width, 75/image.height);
  380. imgcontext.drawImage(image, 0, 0);
  381.  
  382. context.drawImage(imgcanvas, 5,135);
  383.  
  384. context.font = "64px sans-serif";
  385. context.fillColor = fcolor;
  386. var score = document.getElementById("off_score").textContent;
  387. var length = context.measureText(score.toString());
  388. context.fillText(score.toString(), 138-length.width/2,200);
  389.  
  390. var container = document.getElementById("replay_container");
  391. container.appendChild(canvas);
  392. }
  393. else {
  394. var lineup = document.getElementById("offenseLineup");
  395. lineup.parentNode.removeChild(lineup);
  396. }
  397. }
  398.  
  399. function renderDefenseBar(frame, pct, loc) {
  400. if (imageData["defenseBar"] == null) {
  401. var canvas = document.createElement("canvas");
  402. canvas.setAttribute("width","20px");
  403. canvas.setAttribute("height","460px");
  404. var context = canvas.getContext("2d");
  405. var p = play_data[0][12];
  406. var id = p.id;
  407. var bcolor = getTeamBColor(id);
  408. var fcolor = getTeamFColor(bcolor);
  409.  
  410. context.lineWidth = 3;
  411. context.fillStyle = bcolor;
  412. context.strokeStyle = fcolor;
  413. context.fillRect(0,0, 20,460);
  414. context.strokeRect(0,0, 20,460);
  415. var teamName = document.getElementsByClassName("secondary_container")[1].textContent;
  416. context.fillStyle = fcolor;
  417. context.lineWidth=1;
  418. context.font="bold 14px sans-serif";
  419. context.save();
  420. context.rotate(Math.PI/2);
  421. context.fillText(teamName, 50,-6);
  422. context.restore();
  423. imageData["defenseBar"] = canvas;
  424. }
  425.  
  426. var canvas = document.getElementById("defense");
  427. var context = canvas.getContext("2d");
  428. context.drawImage(imageData["defenseBar"],0,0);
  429. }
  430.  
  431. var defenseLineupVisible = false;
  432. function renderDefenseLineup(frame, pct, loc) {
  433. defenseLineupVisible = !defenseLineupVisible;
  434. if (defenseLineupVisible == true) {
  435. var canvas = document.createElement("canvas");
  436. canvas.setAttribute("id","defenseLineup");
  437. canvas.setAttribute("width","200px");
  438. canvas.setAttribute("height","480px");
  439. canvas.style.position = "absolute";
  440. canvas.style.left = "300px";
  441. var context = canvas.getContext("2d");
  442.  
  443. var id = unsafeWindow.play_data[0][13].id;
  444. var team = unsafeWindow.ptid[id];
  445. var bcolor = getTeamBColor(id);
  446. var fcolor = getTeamFColor(bcolor);
  447.  
  448. context.font="14px sans-serif";
  449. context.lineWidth = 2;
  450. context.fillStyle = bcolor;
  451. context.strokeStyle = fcolor;
  452. context.fillRect(0,130, 200,460);
  453. context.strokeRect(0,130, 200,460);
  454.  
  455. for (var i=12; i<23; i++) {
  456. var id = unsafeWindow.play_data[0][i].id;
  457. var position = unsafeWindow.players[id].position;
  458. var name = unsafeWindow.players[id].name;
  459.  
  460. var x = i-11;
  461. var y = 216;
  462. context.fillStyle = bcolor;
  463. context.fillRect(0,(x-1)*24+y, 200,x*24+y);
  464. context.strokeRect(0,(x-1)*24+y, 200,x*24+y);
  465. context.fillStyle = fcolor;
  466. context.fillText(name, 25, x*24-10+y+3);
  467. context.drawImage(imageData[id], 5, (x-1)*24+y+4);
  468. }
  469.  
  470. var image = new Image();
  471. image.src = "http://goallineblitz.com/game/team_pic.pl?team_id="+team;
  472. var imgcanvas = document.createElement("canvas");
  473. var imgcontext = imgcanvas.getContext("2d");
  474. imgcontext.scale(75/image.width, 75/image.height);
  475. imgcontext.drawImage(image, 0, 0);
  476.  
  477. context.drawImage(imgcanvas, 5,135);
  478.  
  479. context.font = "64px sans-serif";
  480. context.fillColor = fcolor;
  481. var score = document.getElementById("def_score").textContent;
  482. var length = context.measureText(score.toString());
  483. context.fillText(score.toString(), 138-(length.width/2),200);
  484.  
  485. var container = document.getElementById("replay_container");
  486. container.appendChild(canvas);
  487. }
  488. else {
  489. var lineup = document.getElementById("defenseLineup");
  490. lineup.parentNode.removeChild(lineup);
  491. }
  492. }
  493.  
  494. function drawFrame(frame, pct) {
  495. var loc = renderField(frame, pct);
  496.  
  497. var canvas = document.getElementById("foreground");
  498. var context = canvas.getContext("2d");
  499. context.restore();
  500. context.save();
  501. context.clearRect(20, 0, 500, 1160);
  502.  
  503. renderFirstDown(frame, pct, loc);
  504. renderVisionCone(frame, pct, loc);
  505. renderPlayers(frame, pct, loc);
  506. renderBall(frame, pct, loc);
  507. renderIcons(frame, pct, loc);
  508.  
  509. renderOffenseBar(frame, pct, loc);
  510. renderDefenseBar(frame, pct, loc);
  511. renderReplayInfo();
  512. }
  513.  
  514. var initialTime = null;
  515. var currentTime = null;
  516. var isPlaying = false;
  517.  
  518. var frameMultiplier = 1;
  519.  
  520. function render() {
  521. if (isPlaying == false) return;
  522.  
  523. currentTime = new Date();
  524. if (initialTime == null) {
  525. initialTime = currentTime;
  526. }
  527.  
  528. var diff = currentTime - initialTime;
  529. var frame = diff/100 * frameMultiplier;
  530. pct = frame - Math.floor(frame);
  531. frame = Math.floor(frame) % play_data.length;
  532.  
  533. try {
  534. drawFrame(frame, pct);
  535. lastTime = currentTime;
  536. var scrubber = 480/play_data.length*(frame+pct);
  537. document.getElementById("scrubber_handle").style.left = scrubber+"px";
  538. }
  539. catch (e) {
  540. window.console.log(e);
  541. }
  542.  
  543. measureFps();
  544.  
  545. if (frame >= play_data.length-1) {
  546. initialTime = null;
  547. setTimeout(function() {
  548. drawFrame(0,0);
  549. setTimeout(function() { render(); }, 2500);
  550. }, 2500);
  551. }
  552. else {
  553. requestAnimFrame(render);
  554. }
  555. }
  556.  
  557. function renderIcons(frame, pct, loc) {
  558. var canvas = document.getElementById("foreground");
  559. var context = canvas.getContext("2d");
  560.  
  561. for (var p=1; p<play_data[frame].length; p++) {
  562. var left = play_data[frame];
  563. if (left[p].icon != null) {
  564. var right = play_data[Math.min(frame+1, play_data.length-1)];
  565. var xdiff = right[p].x - left[p].x;
  566. var ydiff = right[p].y - left[p].y;
  567. var x = left[p].x + xdiff * pct;
  568. var y = left[p].y + ydiff * pct + loc - 20;
  569. var image = new Image();
  570. image.src = "/images/replay_icons/"+left[p].icon+".gif";
  571. context.drawImage(image, x-14, y-16);
  572. }
  573. }
  574. }
  575.  
  576. function renderVisionCone(frame, pct, loc) {
  577. var p = play_data[frame][0];
  578. if ((p.tx != null) && (p.ty != null)) {
  579. var left = play_data[frame][0];
  580. var right = play_data[Math.min(frame+1, play_data.length-1)][0];
  581.  
  582. var xdiff = right.x - left.x;
  583. var ydiff = right.y - left.y;
  584. var ballx = left.x + xdiff * pct - 16;
  585. var bally = left.y + ydiff * pct + loc;
  586.  
  587. var xdiff = right.tx - left.tx;
  588. var ydiff = right.ty - left.ty;
  589. var targetx = left.tx + xdiff * pct - 16;
  590. var targety = left.ty + ydiff * pct + loc - 20;
  591.  
  592. var canvas = document.getElementById("foreground");
  593. var context = canvas.getContext("2d");
  594. context.drawImage(imageData["target"], targetx+11, targety-5);
  595.  
  596. /*
  597. var gradient = context.createRadialGradient(250,900,0,300,950,1);
  598. gradient.addColorStop(0,"rgba(200,200,200,1)");
  599. gradient.addColorStop(1,"rgba(200,200,200,0.0)");
  600. context.beginPath();
  601. context.moveTo(250,900);
  602. context.lineTo(250,950);
  603. context.lineTo(300,900);
  604. context.lineTo(250,900);
  605. context.closePath();
  606. context.fillStyle = gradient;
  607. context.strokeStyle = gradient;
  608. context.fill();
  609. context.stroke();
  610. */
  611. }
  612. }
  613.  
  614. function renderFirstDown(frame, pct, loc) {
  615. for (var i=1; i<play_data[0].length; i++) {
  616. var p = play_data[0][i];
  617. if ((unsafeWindow.players[p.id].position == "C") || (unsafeWindow.players[p.id].position == "LS")) {
  618. var y = p.y+loc;
  619.  
  620. var canvas = document.getElementById("foreground");
  621. var context = canvas.getContext("2d");
  622.  
  623. var dir = document.getElementById("time_ytg");
  624. var dirText = dir.innerHTML;
  625. var togo = "";
  626. if (dirText.indexOf(" inches ") != -1) {
  627. togo = 0.3;
  628. }
  629. else {
  630. if (dirText.indexOf(" G on ") == -1) {
  631. var p2 = dirText.indexOf(" &amp; ")+7;
  632. var p1 = dirText.indexOf(" on ");
  633. togo = parseFloat(dirText.substring(p2,p1));
  634. if (dirText.substring(p2-10,p2-9) == "4") {
  635. context.strokeStyle = "rgb(255,0,0)";
  636. }
  637. else {
  638. context.strokeStyle = "rgb(255,255,0)";
  639. }
  640. }
  641. }
  642. var dy = togo * 9;
  643. if (parseFloat(play_data[0][1].y) > parseFloat(play_data[0][0].y)) {
  644. dy = dy * -1;
  645. }
  646.  
  647. context.beginPath();
  648. context.moveTo(20,y-dy);
  649. context.lineTo(500,y-dy);
  650. context.stroke();
  651.  
  652. context.lineWidth = 2;
  653. context.beginPath();
  654. context.strokeStyle = "rgb(0,0,255)";
  655. context.moveTo(20,y);
  656. context.lineTo(500,y);
  657. context.stroke();
  658. break;
  659. }
  660. }
  661. }
  662.  
  663. function renderField(frame, pct) {
  664. var left = play_data[frame];
  665. var right = play_data[Math.min(frame+1, play_data.length-1)];
  666.  
  667. var ydiff = right[0].y - left[0].y;
  668. var y = left[0].y + ydiff * pct;
  669.  
  670. var location = -1 * y + 250;
  671. location = Math.max(location, -660);
  672. location = Math.min(location, 0);
  673.  
  674. var canvasbg = document.getElementById("background");
  675. var contextbg = canvasbg.getContext("2d");
  676.  
  677. contextbg.drawImage(imageData["field"], 20, 0-location, 480, 500, 20, 0, 480, 500);
  678. return location;
  679. }
  680.  
  681. function renderPlayers(frame, pct, loc) {
  682. /*
  683. for (var f=0; f<play_data.length; f++) {
  684. for (var p=0; p<play_data[f].length; p++) {
  685. if (play_data[f][p].icon != null) {
  686. console.log(f+") "+p+" -- "+play_data[f][p].icon);
  687. }
  688. }
  689. }
  690. */
  691. var canvas = document.getElementById("foreground");
  692. var context = canvas.getContext("2d");
  693.  
  694. for (var p=1; p<play_data[frame].length; p++) {
  695. var left = play_data[frame];
  696. var right = play_data[Math.min(frame+1, play_data.length-1)];
  697.  
  698. var xdiff = right[p].x - left[p].x;
  699. var ydiff = right[p].y - left[p].y;
  700. var x = left[p].x + xdiff * pct;
  701. var y = left[p].y + ydiff * pct + loc - 20;
  702.  
  703. if (imageData[left[p].id] != null) {
  704. context.drawImage(imageData[left[p].id], x, y);
  705. }
  706. }
  707.  
  708. }
  709.  
  710. function renderBall(frame, pct, loc) {
  711. var left = play_data[frame];
  712. var right = play_data[Math.min(frame+1, play_data.length-1)];
  713.  
  714. var xdiff = right[0].x - left[0].x;
  715. var ydiff = right[0].y - left[0].y;
  716. var x = left[0].x + xdiff * pct + 3;
  717. var y = left[0].y + ydiff * pct + loc - 20;
  718.  
  719. var canvasfg = document.getElementById("foreground");
  720. var contextfg = canvasfg.getContext("2d");
  721.  
  722. if ((left[0].z < 5) || (right[0].z < 5)) {
  723. contextfg.drawImage(imageData["ball"], x, y);
  724. }
  725. else {
  726. if (imageData["rotated-ball"] == null) {
  727. var mul = 1;
  728. if (left[0].x < right[0].x) {
  729. mul = 1;
  730. }
  731. else {
  732. mul = -1;
  733. }
  734. if (left[0].y < right[0].y) {
  735. mul = mul * -1;
  736. }
  737. else {
  738. mul = mul * 1;
  739. }
  740. var rotation = mul * Math.atan(Math.abs(left[0].x-right[0].x)/Math.abs(left[0].y-right[0].y));
  741. rotation += 2*Math.PI;
  742. var canvas = document.createElement("canvas");
  743. canvas.setAttribute("width",16);
  744. canvas.setAttribute("height",16);
  745. var context = canvas.getContext("2d");
  746. context.save();
  747. context.translate(8, 8);
  748. context.rotate(rotation);
  749. context.drawImage(imageData["passing-ball"], -8, -8);
  750. context.restore();
  751.  
  752. imageData["rotated-ball"] = canvas;
  753. }
  754. contextfg.drawImage(imageData["rotated-ball"], x, y); //fix me!!
  755. }
  756. }
  757.  
  758. function getFromCanvas(src, w, h) {
  759. var image = new Image();
  760. image.src = src;
  761.  
  762. var canvas = document.createElement("canvas");
  763. canvas.setAttribute("width",w);
  764. canvas.setAttribute("height",h);
  765.  
  766. var context = canvas.getContext("2d");
  767. context.drawImage(image,0,0);
  768.  
  769. return canvas;
  770. }
  771.  
  772. function prepareImages() {
  773. imageData["ball"] = getFromCanvas("http://goallineblitz.com/images/ball.gif", 16, 16);
  774. imageData["passing-ball"] = getFromCanvas("http://goallineblitz.com/images/passing-ball.gif", 16, 16); //fix me!!
  775. imageData["kicking-ball"] = getFromCanvas("http://goallineblitz.com/images/kicking-ball.gif", 16, 16); //fix me!!
  776. imageData["field"] = getFromCanvas("http://goallineblitz.com/images/game/fields/"+unsafeWindow.field+".jpg", 520, 1160);
  777.  
  778. var canvas = document.createElement("canvas");
  779. canvas.setAttribute("width","26");
  780. canvas.setAttribute("height","26");
  781. var context = canvas.getContext("2d");
  782. context.beginPath();
  783. context.strokeStyle = "rgb(255,255,255)";
  784. context.lineWidth = 3;
  785. context.moveTo(13,0);
  786. context.lineTo(13,26);
  787. context.moveTo(0,13);
  788. context.lineTo(26,13);
  789. context.stroke();
  790. context.beginPath();
  791. context.strokeStyle = "rgb(255,0,0)";
  792. context.lineWidth = 2;
  793. context.arc(13, 13, 10, 0, 2*Math.PI, false);
  794. context.closePath();
  795. context.stroke();
  796. imageData["target"] = canvas;
  797. canvas = null;
  798.  
  799. var done = [];
  800. for (var i=1; i<unsafeWindow.play_data[0].length; i++) {
  801. var p = unsafeWindow.play_data[0][i];
  802. var id = p.id;
  803. if (imageData[id] != null) continue;
  804.  
  805. var team = unsafeWindow.ptid[id];
  806. var position = unsafeWindow.players[id].position;
  807. var fcolor = unsafeWindow.home_color2+"_border_home";
  808. var bcolor = unsafeWindow.home_color1;
  809. if (unsafeWindow.home != team) {
  810. fcolor = unsafeWindow.away_color2+"_border";
  811. bcolor = unsafeWindow.away_color1;
  812. }
  813.  
  814. var fimage = new Image();
  815. fimage.src = "http://goallineblitz.com/images/dots/"+fcolor+"/"+position+".gif";
  816.  
  817. var bimage = new Image();
  818. bimage.src = "http://goallineblitz.com/images/dots/"+bcolor+".gif";
  819.  
  820. var canvas = document.createElement("canvas");
  821. canvas.setAttribute("width","16");
  822. canvas.setAttribute("height","16");
  823. var context = canvas.getContext("2d");
  824.  
  825. if ((bimage.complete == false) || (fimage.complete == false)) {
  826. if (bimage.complete == false) {
  827. done.push(fimage.src);
  828. }
  829. if (fimage.complete == false) {
  830. done.push(fimage.src);
  831. }
  832. }
  833. else {
  834. context.drawImage(bimage,0,0);
  835. context.drawImage(fimage,0,0);
  836. imageData[id] = canvas;
  837. }
  838. }
  839. if (done.length > 0) {
  840. console.log("noooooo greasemonkey, no one would ever want to use worker threads...");
  841. console.log(done);
  842. setTimeout(prepareImages, 250);
  843. }
  844. }
  845.  
  846. function beginPressed() {
  847. pausePressed();
  848. initialTime = null;
  849. currentTime = null;
  850. drawFrame(0,0);
  851. }
  852.  
  853. function rwPressed() {
  854. if (isPlaying == true) {
  855. frameMultiplier = Math.max(frameMultiplier - 0.25, 0);
  856. console.log(frameMultiplier);
  857. }
  858. else {
  859. currentTime = currentTime.setMilliseconds(currentTime.getMilliseconds() - 100);
  860. currentTime = new Date(currentTime);
  861. var diff = currentTime - initialTime;
  862. var frame = diff/100 * frameMultiplier;
  863. pct = frame - Math.floor(frame);
  864. frame = Math.floor(frame) % play_data.length;
  865. if (frame < 0) frame += play_data.length;
  866. drawFrame(frame, pct);
  867. }
  868. }
  869.  
  870. function pausePressed() {
  871. console.log("pause");
  872.  
  873. document.getElementById("pause_button").visibility = "hidden";
  874. document.getElementById("play_button").visibility = "visible";
  875.  
  876. isPlaying = false;
  877. }
  878.  
  879. function playPressed() {
  880. console.log("play");
  881. document.getElementById("pause_button").visibility = "visible";
  882. document.getElementById("play_button").visibility = "hidden";
  883. isPlaying = true;
  884. var t = currentTime;
  885. currentTime = new Date();
  886. initialTime = currentTime - (t-initialTime);
  887.  
  888. render();
  889. }
  890.  
  891. function ffPressed() {
  892. if (isPlaying == true) {
  893. frameMultiplier = frameMultiplier + 0.25;
  894. console.log(frameMultiplier);
  895. }
  896. else {
  897. currentTime = currentTime.setMilliseconds(currentTime.getMilliseconds() + 100);
  898. currentTime = new Date(currentTime);
  899. var diff = currentTime - initialTime;
  900. var frame = diff/100 * frameMultiplier;
  901. pct = frame - Math.floor(frame);
  902. frame = Math.floor(frame) % play_data.length;
  903. drawFrame(frame, pct);
  904. }
  905. }
  906.  
  907. function endPressed() {
  908. pausePressed();
  909. initialTime = null;
  910. currentTime = null;
  911. drawFrame(play_data.length-1,0);
  912. }
  913.  
  914. function fixButtons() {
  915. var buttons = document.getElementsByClassName("button left");
  916. var begin = buttons[2];
  917. begin.addEventListener("click",beginPressed);
  918.  
  919. var rewind = buttons[3];
  920. rewind.addEventListener("click",rwPressed);
  921.  
  922. var pause = buttons[4];
  923. pause.addEventListener("click",playPressed);
  924.  
  925. var play = buttons[5];
  926. play.addEventListener("click",pausePressed);
  927.  
  928. var ff = buttons[6];
  929. ff.addEventListener("click",ffPressed);
  930.  
  931. var end = buttons[7];
  932. end.addEventListener("click",endPressed);
  933. }
  934.  
  935. function fixFrames() {
  936. var stime = new Date();
  937. var uwpd = new Array();
  938. for (var i=0; i<unsafeWindow.play_data.length; i++) {
  939. uwpd.push(new Array());
  940. }
  941.  
  942. var missing = 0;
  943. for (var p=0; p<unsafeWindow.play_data[0].length; p++) {
  944. var id = unsafeWindow.play_data[0][p].id;
  945.  
  946. for (var cf=0; cf<unsafeWindow.play_data.length; cf++) {
  947. var found = false;
  948. for (var i=0; i<unsafeWindow.play_data[cf].length; i++) {
  949. if (id == unsafeWindow.play_data[cf][i].id) {
  950. found = true;
  951. uwpd[cf].push(eval(uneval(unsafeWindow.play_data[cf][i])));
  952. break;
  953. }
  954. }
  955. if (!found) {
  956. uwpd[cf].push(eval(uneval(uwpd[cf-1][p])));
  957. missing++;
  958. }
  959. }
  960. }
  961. var time = new Date() - stime;
  962. console.log("uwpd copy ("+missing+" missing): "+time.toFixed(0)+"ms");
  963.  
  964. for (var p=1; p<uwpd[0].length; p++) {
  965. for (var f=uwpd.length-1; f>0; f--) {
  966. if (uwpd[f][p].icon != null) {
  967. for (var z=f+1; z<Math.min(uwpd.length, f+8); z++) {
  968. uwpd[z][p].icon = uwpd[f][p].icon;
  969. }
  970. }
  971. }
  972. }
  973. var time = new Date() - stime;
  974. console.log("uwpd icons: "+time.toFixed(0)+"ms");
  975.  
  976. prepareImages();
  977.  
  978. return uwpd;
  979. }
  980.  
  981. var frameCounter = 0;
  982. var lastTime = 0;
  983. var fps = [];
  984. function measureFps() {
  985. frameCounter++;
  986. if (frameCounter > 300) {
  987. var now = new Date();
  988. var delta = now - lastTime;
  989. fps.push(frameCounter / delta);
  990.  
  991. frameCounter = 0;
  992. lastTime = now;
  993. }
  994.  
  995. if (fps.length == 10) {
  996. console.log(fps);
  997. fps = [];
  998. }
  999.  
  1000. }