Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. /* Attempts to screen-shot a element onto a canvas */
  2. var css = function(el, styles){
  3. for (var style in styles) {
  4. if(!styles.hasOwnProperty(style)) continue;
  5. el.style[style] = styles[style];
  6. }
  7. return el;
  8. };
  9. var selectElement = function(callback){
  10. var selection = document.createElement("div");
  11. css(selection, {
  12. "pointer-events": "none",
  13. "transition-property": "top, left, width, height",
  14. "transition-duration": "0.1s",
  15. "transition-timing-function": "linear"
  16. });
  17. document.body.appendChild(selection);
  18. var mouseMoveListener = function(e){
  19. //console.log("MOVEMENT");
  20. var bounds = e.target.getBoundingClientRect();
  21. css(selection, {
  22. "position": "fixed",
  23. "z-index": 1000,
  24. "background": "rgba(255,0,0,0.5)",
  25. "top": bounds.top+"px",
  26. "left": bounds.left+"px",
  27. "height": bounds.height+"px",
  28. "width": bounds.width+"px"
  29. });
  30. e.stopPropagation();
  31. e.preventDefault();
  32. return false;
  33. };
  34. var mouseDownListener = function(e){
  35. try {
  36. callback(e.target);
  37. }catch(e){
  38. console.log("Problem occurred in callback: ", e);
  39. }
  40. document.body.removeChild(selection);
  41. document.removeEventListener("mousemove", mouseMoveListener);
  42. document.removeEventListener("mousedown", mouseDownListener);
  43. e.preventDefault();
  44. e.stopPropagation();
  45. return false;
  46. };
  47. document.addEventListener("mousemove", mouseMoveListener);
  48. document.addEventListener("mousedown", mouseDownListener);
  49. };
  50. var tokenize = function(str, separator, leftBound, rightBound) {
  51. var start = 0;
  52. var result = [];
  53. var b = 0;
  54. for(var i = 0; i < str.length; i++){
  55. if(str[i] == leftBound)b++;
  56. if(str[i] == rightBound)b--;
  57. if(separator.indexOf(str[i])!==-1 && b === 0){
  58. if(i-start !== 0)
  59. result.push(str.substr(start, i-start));
  60. start = i+1;
  61. }
  62. }
  63. if(start !== i){
  64. result.push(str.substr(start, i-start));
  65. }
  66. return result;
  67. };
  68. //Collapse whitespace
  69. var collapse = function(str, whitespace) {
  70. return str.replace(/\s+/g, " ");
  71. };
  72. var drawElement = function(ctx, element){
  73. var b = element.getBoundingClientRect();
  74. var s = window.getComputedStyle(element);
  75. if(element instanceof HTMLImageElement){
  76. /*ctx.beginPath();
  77. ctx.moveTo(element.offsetLeft, element.offsetTop);
  78. ctx.lineTo(element.offsetLeft+element.clientWidth, element.offsetTop+element.clientHeight);
  79. ctx.moveTo(element.offsetLeft+element.clientWidth, element.offsetTop);
  80. ctx.lineTo(element.offsetLeft, element.offsetTop+element.clientHeight);
  81. ctx.stroke();
  82. ctx.closePath();*/
  83. ctx.drawImage(element, b.left, b.top, b.width, b.height);
  84. return;
  85. }
  86. //ctx.strokeRect(b.left, b.top, b.width, b.height);
  87. ctx.fillStyle = s.backgroundColor;
  88. //console.log(s.backgroundColor, s.backgroundImage);
  89. ctx.fillRect(b.left, b.top, b.width, b.height);
  90. //Draw the borders TODO: border radius
  91. ctx.beginPath();
  92. if(parseInt(s.borderLeftWidth, 10) > 0) {
  93. var lw = ctx.lineWidth;
  94. var ls = ctx.strokeStyle;
  95. ctx.moveTo(b.left, b.top);ctx.lineTo(b.left, b.top+b.height);
  96. ctx.lineWidth = parseInt(s.borderLeftWidth, 10);
  97. ctx.strokeStyle = s.borderLeftColor;
  98. ctx.stroke();
  99. ctx.lineWidth = lw;
  100. ctx.strokeStyle = ls;
  101. }
  102. var bounds = element.getBoundingClientRect();
  103. for(var i = 0; i < element.childNodes.length; i++){
  104. var child = element.childNodes[i];
  105. if(child.nodeType == 1){
  106. drawElement(ctx, child);
  107. }
  108. if(child.nodeType == 3){
  109. var range = document.createRange();
  110. range.selectNodeContents(child);
  111. var rects = range.getClientRects();
  112. ctx.font = s.font;
  113. var textHeight = parseInt(s.fontSize, 10);
  114. var text = collapse(child.textContent);
  115. var lineStart = 0;
  116. for(var j = 0; j < rects.length; j++){
  117. ctx.fillStyle = s.color;
  118. for(var w = 1; w < text.length; w++){
  119. if(ctx.measureText(text.substr(lineStart, w)).width >= rects[j].width) break;
  120. }
  121. ctx.fillText(collapse(text.substr(lineStart, w)), rects[j].left, rects[j].top+textHeight);
  122. lineStart += w;
  123. }
  124. }
  125. }
  126. };
  127. selectElement(function(element){
  128. var canvas = document.createElement("canvas");
  129. var context = canvas.getContext("2d");
  130. var bounds = element.getBoundingClientRect();
  131. canvas.width = bounds.width;
  132. canvas.height = bounds.height;
  133. context.translate(-bounds.left, -bounds.top);
  134.  
  135. drawElement(context, element);
  136. var container = document.createElement("div");
  137. var close = document.createElement("a");
  138. close.href="#";
  139. container.appendChild(close);
  140. css(close, {
  141. "position": "absolute",
  142. "top": "-0.5em",
  143. "right": "-0.5em",
  144. "width": "1em",
  145. "height": "1em",
  146. "display": "block",
  147. "background": "black",
  148. "border": "3px solid white",
  149. "box-shadow": "0 3px 10px black",
  150. "text-align": "center",
  151. "font-family": "arial",
  152. "color": "white",
  153. "text-decoration": "none",
  154. "border-radius": "100%",
  155. "line-height": "1.1",
  156. "font-size": "19px",
  157. "font-weight": "bold"
  158. });
  159. close.addEventListener("click", function(e){
  160. container.parentNode.removeChild(container);
  161. e.preventDefault();
  162. e.stopPropagation();
  163. return false;
  164. });
  165. close.appendChild(document.createTextNode("\u00d7"));
  166. container.appendChild(canvas);
  167.  
  168. //We can't call toDataURL on a canvas that has cross-domain images on it
  169. // So slap it on the screen and call it good
  170. css(container, {
  171. "position": "fixed",
  172. "left": 0,
  173. "right": 0,
  174. "top": 0,
  175. "bottom": 0,
  176. "width": canvas.width+"px",
  177. "height": canvas.height+"px",
  178. "background": "white",
  179. "padding": "12px",
  180. "border": "1px solid #eee",
  181. "border-radius": "5px",
  182. "box-shadow": "0 5px 50px black",
  183. "margin": "auto",
  184. "z-index": 10000
  185. });
  186. document.body.appendChild(container);
  187. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement