Advertisement
vertrex

map-preview.user.js

Aug 20th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', function(){
  2. if(!document.documentElement || document.documentElement.tagName != 'Resource' || !(!document.documentElement.getAttribute('type') || document.documentElement.getAttribute('type') == 'aamap')) {
  3.     return;
  4. }
  5.  
  6. // This code was written by Tyler Akins and has been placed in the
  7. // public domain.  It would be nice if you left this header intact.
  8. // Base64 code from Tyler Akins -- http://rumkin.com
  9.  
  10. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  11.  
  12. function encode64(input) {
  13.    var output = "";
  14.    var chr1, chr2, chr3;
  15.    var enc1, enc2, enc3, enc4;
  16.    var i = 0;
  17.  
  18.    do {
  19.       chr1 = input.charCodeAt(i++);
  20.       chr2 = input.charCodeAt(i++);
  21.       chr3 = input.charCodeAt(i++);
  22.  
  23.       enc1 = chr1 >> 2;
  24.       enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  25.       enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  26.       enc4 = chr3 & 63;
  27.  
  28.       if (isNaN(chr2)) {
  29.          enc3 = enc4 = 64;
  30.       } else if (isNaN(chr3)) {
  31.          enc4 = 64;
  32.       }
  33.  
  34.       output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  35.          keyStr.charAt(enc3) + keyStr.charAt(enc4);
  36.    } while (i < input.length);
  37.    
  38.    return output;
  39. }
  40.  
  41. // end of code by Tyler Akins
  42.  
  43. var margin = 5;
  44. var stroke = 2;
  45. var arrow = 'M -5,0 -5,20 -15,20 0,40 15,20 5,20 5,0 Z';
  46.  
  47. var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  48. //var circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  49. //circle.setAttribute('r', '50');
  50. //circle.setAttribute('cx', '50');
  51. //circle.setAttribute('cy', '50');
  52. //circle.setAttribute('stroke', 'red');
  53. //circle.setAttribute('stroke-width', '1');
  54. //svg.appendChild(circle);
  55.  
  56. var minx = 10000;
  57. var maxx = -10000;
  58. var miny = 10000;
  59. var maxy = -10000;
  60.  
  61. var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
  62. var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
  63. marker.setAttribute('id', 'spawn');
  64. marker.setAttribute('markerWidth', '30');
  65. marker.setAttribute('markerHeight', '40');
  66. marker.setAttribute('refX', '2');
  67. marker.setAttribute('refY', '15');
  68. marker.setAttribute('orient', 'auto');
  69. marker.setAttribute('viewBox', '0 0 40 30');
  70. var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  71. path.setAttribute('d', arrow);
  72. path.setAttribute('fill', 'red');
  73. path.setAttribute('transform', 'translate(0,15) rotate(-90)');
  74. marker.appendChild(path);
  75. defs.appendChild(marker);
  76. svg.appendChild(defs);
  77.  
  78. var walls = document.getElementsByTagName('Wall');
  79. for(var i = walls.length - 1; i >= 0; --i) {
  80.     var str = '';
  81.     var points = walls[i].getElementsByTagName('Point');
  82.     for(var j = points.length - 1; j >= 0; --j) {
  83.         var x = parseFloat(points[j].getAttribute('x'));
  84.         var y = -parseFloat(points[j].getAttribute('y'));
  85.         str += x + ',' + y + ' ';
  86.         if(minx > x) minx = x;
  87.         if(maxx < x) maxx = x;
  88.         if(miny > y) miny = y;
  89.         if(maxy < y) maxy = y;
  90.     }
  91.     var polyline = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
  92.     polyline.setAttribute('points', str);
  93.     polyline.setAttribute('stroke', 'black');
  94.     polyline.setAttribute('stroke-width', stroke);
  95.     polyline.setAttribute('fill', 'none');
  96.     svg.appendChild(polyline);
  97. }
  98.  
  99. var zones = document.getElementsByTagName('Zone');
  100. for(var i = zones.length - 1; i >= 0; --i) {
  101.     var zone = zones[i];
  102.     var color = '#ffa500';
  103.     var alpha = 1;
  104.     var effect = zone.getAttribute('effect');
  105.     if(effect && effect == 'win') {
  106.         color = 'lime'
  107.     } else if(effect && effect == 'death') {
  108.         color = 'red'
  109.     } else if(effect && effect == 'flag') {
  110.         color = '#008'
  111.     } else if(effect && effect == 'ball') {
  112.         color = 'brown'
  113.     }
  114.  
  115.     var shape = zone.getElementsByTagName('ShapeCircle')[0];
  116.     if(!shape) shape = zone.getElementsByTagName('ShapePolygon')[0];
  117.     if(!shape) continue;
  118.     var point = shape.getElementsByTagName('Point')[0];
  119.     if(!point) continue;
  120.  
  121.     var colorEl = shape.getElementsByTagName('Color')[0];
  122.     if(colorEl) {
  123.         if(colorEl.getAttribute('hexCode')) {
  124.             color = "#" + colorEl.getAttribute('hexCode').substr(2);
  125.         } else {
  126.             color = 'rgb(' + parseFloat(colorEl.getAttribute('red'))*100 + '%, ' + parseFloat(colorEl.getAttribute('green'))*100 + '%, ' + parseFloat(colorEl.getAttribute('blue'))*100 + '%)';
  127.         }
  128.         if(colorEl.hasAttribute('alpha')) {
  129.             alpha = colorEl.getAttribute('alpha');
  130.         }
  131.     }
  132.  
  133.     if(shape.tagName == 'ShapePolygon') {
  134.         var points = shape.getElementsByTagName('Point');
  135.         if(points.length <= 2) continue;
  136.         var basex = parseFloat(points[0].getAttribute('x'));
  137.         var basey = -parseFloat(points[0].getAttribute('y'));
  138.         var scale = parseFloat(shape.getAttribute('scale'));
  139.         if(!scale) scale = 1;
  140.         var str = '';
  141.         for(var j = points.length - 1; j > 0; --j) {
  142.             var x = parseFloat(points[j].getAttribute('x'));
  143.             var y = -parseFloat(points[j].getAttribute('y'));
  144.             str += (x*scale+basex)+ ',' + (y*scale+basey) + ' ';
  145.         }
  146.         var polygon = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
  147.         polygon.setAttribute('points', str);
  148.         polygon.setAttribute('stroke', color);
  149.         polygon.setAttribute('opacity', alpha);
  150.         polygon.setAttribute('stroke-width', stroke);
  151.         polygon.setAttribute('fill', 'none');
  152.         svg.appendChild(polygon);
  153.     } else {
  154.         var circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  155.         var radius = shape.getAttribute('radius');
  156.         if(!radius) radius = shape.getAttribute('scale');
  157.         circle.setAttribute('r', radius);
  158.         circle.setAttribute('cx', parseFloat(point.getAttribute('x')));
  159.         circle.setAttribute('cy', -parseFloat(point.getAttribute('y')));
  160.         circle.setAttribute('stroke', color);
  161.         circle.setAttribute('opacity', alpha);
  162.         circle.setAttribute('stroke-width', stroke);
  163.         circle.setAttribute('fill', 'none');
  164.         svg.appendChild(circle);
  165.     }
  166. }
  167.  
  168. var spawns = document.getElementsByTagName('Spawn');
  169. for(var i = spawns.length - 1; i >= 0; --i) {
  170.     var spawn = spawns[i];
  171.     var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  172.     path.setAttribute('marker-end', 'url(#spawn)');
  173.     path.setAttribute('stroke-width', '.5');
  174.     path.setAttribute('stroke', 'red');
  175.     path.setAttribute('d', 'M 0 0 0 1');
  176.  
  177.     var transform = 'translate(' + spawn.getAttribute('x') + ',' + -parseFloat(spawn.getAttribute('y')) + ')';
  178.     transform += ' scale(1 -1) rotate(-90)';
  179.     if(spawn.getAttribute('angle')) {
  180.         transform += ' rotate(' + spawn.getAttribute('angle') + ')';
  181.     } else {
  182.         var xdir = parseFloat(spawn.getAttribute('xdir'));
  183.         var ydir = parseFloat(spawn.getAttribute('ydir'));
  184.  
  185.         var l = Math.sqrt(xdir*xdir + ydir*ydir);
  186.         transform += ' matrix(' + (xdir/l) + ',' + (ydir/l) + ',' + (-ydir/l) + ',' + (xdir/l) + ',0,0)';
  187.     }
  188.     path.setAttribute('transform', transform);
  189.     svg.appendChild(path);
  190. }
  191.  
  192. svg.setAttribute('viewBox', (minx - margin) + ',' + (miny-margin) + ',' + (maxx-minx+2*margin) + ',' + (maxy-miny+2*margin));
  193. svg.setAttribute('width', window.innerWidth + 'px');
  194. svg.setAttribute('height', window.innerHeight - 5 + 'px');
  195.  
  196. var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  197. rect.setAttribute('x', minx-margin);
  198. rect.setAttribute('y', miny-margin);
  199. rect.setAttribute('width', maxx - minx + 2*margin);
  200. rect.setAttribute('height', maxy - miny + 2*margin);
  201. rect.setAttribute('fill', 'white');
  202. svg.insertBefore(rect, svg.getElementsByTagName('defs')[0].nextSibling);
  203.  
  204. document.documentElement.replaceChild(svg, document.getElementsByTagName('Map')[0]);
  205.  
  206. window.addEventListener('resize', function(){
  207.     svg.setAttribute('width', window.innerWidth + 'px');
  208.     svg.setAttribute('height', window.innerHeight - 5 + 'px');
  209. }, false);
  210.  
  211. svg.addEventListener('click', function(){
  212.     if(!confirm("Really convert this SVG to a data: URL for saving (may take some time)")) {
  213.         return false;
  214.     }
  215.     var serializer = new XMLSerializer();
  216.     var str = serializer.serializeToString(this);
  217.     window.location.href = 'data:image/svg+xml;base64,' + encode64('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd">' + str);
  218. }, false);
  219. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement