园林绿化
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

813 lines
23 KiB

  1. (function (global) {
  2. "use strict";
  3. var util = newUtil();
  4. var inliner = newInliner();
  5. var fontFaces = newFontFaces();
  6. var images = newImages();
  7. // Default impl options
  8. var defaultOptions = {
  9. // Default is to fail on error, no placeholder
  10. imagePlaceholder: undefined,
  11. // Default cache bust is false, it will use the cache
  12. cacheBust: false,
  13. };
  14. var domtoimage = {
  15. toSvg: toSvg,
  16. toPng: toPng,
  17. toJpeg: toJpeg,
  18. toBlob: toBlob,
  19. toPixelData: toPixelData,
  20. impl: {
  21. fontFaces: fontFaces,
  22. images: images,
  23. util: util,
  24. inliner: inliner,
  25. options: {},
  26. },
  27. };
  28. global.domtoimage = domtoimage;
  29. /**
  30. * @param {Node} node - The DOM Node object to render
  31. * @param {Object} options - Rendering options
  32. * @param {Function} options.filter - Should return true if passed node should be included in the output
  33. * (excluding node means excluding it's children as well). Not called on the root node.
  34. * @param {String} options.bgcolor - color for the background, any valid CSS color value.
  35. * @param {Number} options.width - width to be applied to node before rendering.
  36. * @param {Number} options.height - height to be applied to node before rendering.
  37. * @param {Object} options.style - an object whose properties to be copied to node's style before rendering.
  38. * @param {Number} options.quality - a Number between 0 and 1 indicating image quality (applicable to JPEG only),
  39. defaults to 1.0.
  40. * @param {String} options.imagePlaceholder - dataURL to use as a placeholder for failed images, default behaviour is to fail fast on images we can't fetch
  41. * @param {Boolean} options.cacheBust - set to true to cache bust by appending the time to the request url
  42. * @return {Promise} - A promise that is fulfilled with a SVG image data URL
  43. * */
  44. function toSvg(node, options) {
  45. options = options || {};
  46. copyOptions(options);
  47. return Promise.resolve(node)
  48. .then(function (node) {
  49. return cloneNode(node, options.filter, true);
  50. })
  51. .then(embedFonts)
  52. .then(inlineImages)
  53. .then(applyOptions)
  54. .then(function (clone) {
  55. return makeSvgDataUri(clone, options.width || util.width(node), options.height || util.height(node));
  56. });
  57. function applyOptions(clone) {
  58. if (options.bgcolor) {
  59. clone.style.backgroundColor = options.bgcolor;
  60. }
  61. if (options.width) {
  62. clone.style.width = options.width + "px";
  63. }
  64. if (options.height) {
  65. clone.style.height = options.height + "px";
  66. }
  67. if (options.style) {
  68. Object.keys(options.style).forEach(function (property) {
  69. clone.style[property] = options.style[property];
  70. });
  71. }
  72. return clone;
  73. }
  74. }
  75. /**
  76. * @param {Node} node - The DOM Node object to render
  77. * @param {Object} options - Rendering options, @see {@link toSvg}
  78. * @return {Promise} - A promise that is fulfilled with a Uint8Array containing RGBA pixel data.
  79. * */
  80. function toPixelData(node, options) {
  81. return draw(node, options || {}).then(function (canvas) {
  82. return canvas.getContext("2d").getImageData(0, 0, util.width(node), util.height(node)).data;
  83. });
  84. }
  85. /**
  86. * @param {Node} node - The DOM Node object to render
  87. * @param {Object} options - Rendering options, @see {@link toSvg}
  88. * @return {Promise} - A promise that is fulfilled with a PNG image data URL
  89. * */
  90. function toPng(node, options) {
  91. return draw(node, options || {}).then(function (canvas) {
  92. return canvas.toDataURL();
  93. });
  94. }
  95. /**
  96. * @param {Node} node - The DOM Node object to render
  97. * @param {Object} options - Rendering options, @see {@link toSvg}
  98. * @return {Promise} - A promise that is fulfilled with a JPEG image data URL
  99. * */
  100. function toJpeg(node, options) {
  101. options = options || {};
  102. return draw(node, options).then(function (canvas) {
  103. return canvas.toDataURL("image/jpeg", options.quality || 1.0);
  104. });
  105. }
  106. /**
  107. * @param {Node} node - The DOM Node object to render
  108. * @param {Object} options - Rendering options, @see {@link toSvg}
  109. * @return {Promise} - A promise that is fulfilled with a PNG image blob
  110. * */
  111. function toBlob(node, options) {
  112. return draw(node, options || {}).then(util.canvasToBlob);
  113. }
  114. function copyOptions(options) {
  115. // Copy options to impl options for use in impl
  116. if (typeof options.imagePlaceholder === "undefined") {
  117. domtoimage.impl.options.imagePlaceholder = defaultOptions.imagePlaceholder;
  118. } else {
  119. domtoimage.impl.options.imagePlaceholder = options.imagePlaceholder;
  120. }
  121. if (typeof options.cacheBust === "undefined") {
  122. domtoimage.impl.options.cacheBust = defaultOptions.cacheBust;
  123. } else {
  124. domtoimage.impl.options.cacheBust = options.cacheBust;
  125. }
  126. }
  127. function draw(domNode, options) {
  128. return toSvg(domNode, options)
  129. .then(util.makeImage)
  130. .then(util.delay(100))
  131. .then(function (image) {
  132. var canvas = newCanvas(domNode);
  133. canvas.getContext("2d").drawImage(image, 0, 0);
  134. return canvas;
  135. });
  136. function newCanvas(domNode) {
  137. var canvas = document.createElement("canvas");
  138. canvas.width = options.width || util.width(domNode);
  139. canvas.height = options.height || util.height(domNode);
  140. if (options.bgcolor) {
  141. var ctx = canvas.getContext("2d");
  142. ctx.fillStyle = options.bgcolor;
  143. ctx.fillRect(0, 0, canvas.width, canvas.height);
  144. }
  145. return canvas;
  146. }
  147. }
  148. function cloneNode(node, filter, root) {
  149. if (!root && filter && !filter(node)) {
  150. return Promise.resolve();
  151. }
  152. return Promise.resolve(node)
  153. .then(makeNodeCopy)
  154. .then(function (clone) {
  155. return cloneChildren(node, clone, filter);
  156. })
  157. .then(function (clone) {
  158. return processClone(node, clone);
  159. });
  160. function makeNodeCopy(node) {
  161. if (node instanceof HTMLCanvasElement) {
  162. return util.makeImage(node.toDataURL());
  163. }
  164. return node.cloneNode(false);
  165. }
  166. function cloneChildren(original, clone, filter) {
  167. var children = original.childNodes;
  168. if (children.length === 0) {
  169. return Promise.resolve(clone);
  170. }
  171. return cloneChildrenInOrder(clone, util.asArray(children), filter).then(function () {
  172. return clone;
  173. });
  174. function cloneChildrenInOrder(parent, children, filter) {
  175. var done = Promise.resolve();
  176. children.forEach(function (child) {
  177. done = done
  178. .then(function () {
  179. return cloneNode(child, filter);
  180. })
  181. .then(function (childClone) {
  182. if (childClone) {
  183. parent.appendChild(childClone);
  184. }
  185. });
  186. });
  187. return done;
  188. }
  189. }
  190. function processClone(original, clone) {
  191. if (!(clone instanceof Element)) {
  192. return clone;
  193. }
  194. return Promise.resolve()
  195. .then(cloneStyle)
  196. .then(clonePseudoElements)
  197. .then(copyUserInput)
  198. .then(fixSvg)
  199. .then(function () {
  200. return clone;
  201. });
  202. function cloneStyle() {
  203. copyStyle(window.getComputedStyle(original), clone.style);
  204. function copyStyle(source, target) {
  205. if (source.cssText) {
  206. target.cssText = source.cssText;
  207. } else {
  208. copyProperties(source, target);
  209. }
  210. function copyProperties(source, target) {
  211. util.asArray(source).forEach(function (name) {
  212. target.setProperty(name, source.getPropertyValue(name), source.getPropertyPriority(name));
  213. });
  214. }
  215. }
  216. }
  217. function clonePseudoElements() {
  218. [":before", ":after"].forEach(function (element) {
  219. clonePseudoElement(element);
  220. });
  221. function clonePseudoElement(element) {
  222. var style = window.getComputedStyle(original, element);
  223. var content = style.getPropertyValue("content");
  224. if (content === "" || content === "none") {
  225. return;
  226. }
  227. var className = util.uid();
  228. clone.className = clone.className + " " + className;
  229. var styleElement = document.createElement("style");
  230. styleElement.appendChild(formatPseudoElementStyle(className, element, style));
  231. clone.appendChild(styleElement);
  232. function formatPseudoElementStyle(className, element, style) {
  233. var selector = "." + className + ":" + element;
  234. var cssText = style.cssText ? formatCssText(style) : formatCssProperties(style);
  235. return document.createTextNode(selector + "{" + cssText + "}");
  236. function formatCssText(style) {
  237. var content = style.getPropertyValue("content");
  238. return style.cssText + " content: " + content + ";";
  239. }
  240. function formatCssProperties(style) {
  241. return util.asArray(style).map(formatProperty).join("; ") + ";";
  242. function formatProperty(name) {
  243. return name + ": " + style.getPropertyValue(name) + (style.getPropertyPriority(name) ? " !important" : "");
  244. }
  245. }
  246. }
  247. }
  248. }
  249. function copyUserInput() {
  250. if (original instanceof HTMLTextAreaElement) {
  251. clone.innerHTML = original.value;
  252. }
  253. if (original instanceof HTMLInputElement) {
  254. clone.setAttribute("value", original.value);
  255. }
  256. }
  257. function fixSvg() {
  258. if (!(clone instanceof SVGElement)) {
  259. return;
  260. }
  261. clone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
  262. if (!(clone instanceof SVGRectElement)) {
  263. return;
  264. }
  265. ["width", "height"].forEach(function (attribute) {
  266. var value = clone.getAttribute(attribute);
  267. if (!value) {
  268. return;
  269. }
  270. clone.style.setProperty(attribute, value);
  271. });
  272. }
  273. }
  274. }
  275. function embedFonts(node) {
  276. return fontFaces.resolveAll().then(function (cssText) {
  277. var styleNode = document.createElement("style");
  278. node.appendChild(styleNode);
  279. styleNode.appendChild(document.createTextNode(cssText));
  280. return node;
  281. });
  282. }
  283. function inlineImages(node) {
  284. return images.inlineAll(node).then(function () {
  285. return node;
  286. });
  287. }
  288. function makeSvgDataUri(node, width, height) {
  289. return Promise.resolve(node)
  290. .then(function (node) {
  291. node.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  292. return new XMLSerializer().serializeToString(node);
  293. })
  294. .then(util.escapeXhtml)
  295. .then(function (xhtml) {
  296. return '<foreignObject x="0" y="0" width="100%" height="100%">' + xhtml + "</foreignObject>";
  297. })
  298. .then(function (foreignObject) {
  299. return '<svg xmlns="http://www.w3.org/2000/svg" width="' + width + '" height="' + height + '">' + foreignObject + "</svg>";
  300. })
  301. .then(function (svg) {
  302. return "data:image/svg+xml;charset=utf-8," + svg;
  303. });
  304. }
  305. function newUtil() {
  306. return {
  307. escape: escape,
  308. parseExtension: parseExtension,
  309. mimeType: mimeType,
  310. dataAsUrl: dataAsUrl,
  311. isDataUrl: isDataUrl,
  312. canvasToBlob: canvasToBlob,
  313. resolveUrl: resolveUrl,
  314. getAndEncode: getAndEncode,
  315. uid: uid(),
  316. delay: delay,
  317. asArray: asArray,
  318. escapeXhtml: escapeXhtml,
  319. makeImage: makeImage,
  320. width: width,
  321. height: height,
  322. };
  323. function mimes() {
  324. /*
  325. * Only WOFF and EOT mime types for fonts are 'real'
  326. * see http://www.iana.org/assignments/media-types/media-types.xhtml
  327. */
  328. var WOFF = "application/font-woff";
  329. var JPEG = "image/jpeg";
  330. return {
  331. woff: WOFF,
  332. woff2: WOFF,
  333. ttf: "application/font-truetype",
  334. eot: "application/vnd.ms-fontobject",
  335. png: "image/png",
  336. jpg: JPEG,
  337. jpeg: JPEG,
  338. gif: "image/gif",
  339. tiff: "image/tiff",
  340. svg: "image/svg+xml",
  341. };
  342. }
  343. function parseExtension(url) {
  344. var match = /\.([^\.\/]*?)$/g.exec(url);
  345. if (match) {
  346. return match[1];
  347. } else {
  348. return "";
  349. }
  350. }
  351. function mimeType(url) {
  352. var extension = parseExtension(url).toLowerCase();
  353. return mimes()[extension] || "";
  354. }
  355. function isDataUrl(url) {
  356. return url.search(/^(data:)/) !== -1;
  357. }
  358. function toBlob(canvas) {
  359. return new Promise(function (resolve) {
  360. var binaryString = window.atob(canvas.toDataURL().split(",")[1]);
  361. var length = binaryString.length;
  362. var binaryArray = new Uint8Array(length);
  363. for (var i = 0; i < length; i++) {
  364. binaryArray[i] = binaryString.charCodeAt(i);
  365. }
  366. resolve(
  367. new Blob([binaryArray], {
  368. type: "image/png",
  369. })
  370. );
  371. });
  372. }
  373. function canvasToBlob(canvas) {
  374. if (canvas.toBlob) {
  375. return new Promise(function (resolve) {
  376. canvas.toBlob(resolve);
  377. });
  378. }
  379. return toBlob(canvas);
  380. }
  381. function resolveUrl(url, baseUrl) {
  382. var doc = document.implementation.createHTMLDocument();
  383. var base = doc.createElement("base");
  384. doc.head.appendChild(base);
  385. var a = doc.createElement("a");
  386. doc.body.appendChild(a);
  387. base.href = baseUrl;
  388. a.href = url;
  389. return a.href;
  390. }
  391. function uid() {
  392. var index = 0;
  393. return function () {
  394. return "u" + fourRandomChars() + index++;
  395. function fourRandomChars() {
  396. /* see http://stackoverflow.com/a/6248722/2519373 */
  397. return ("0000" + ((Math.random() * Math.pow(36, 4)) << 0).toString(36)).slice(-4);
  398. }
  399. };
  400. }
  401. function makeImage(uri) {
  402. return new Promise(function (resolve, reject) {
  403. var image = new Image();
  404. image.crossOrigin = "*";
  405. image.onload = function () {
  406. resolve(image);
  407. };
  408. image.onerror = reject;
  409. image.src = uri;
  410. });
  411. }
  412. function getAndEncode(url) {
  413. var TIMEOUT = 100000;
  414. if (domtoimage.impl.options.cacheBust) {
  415. // Cache bypass so we dont have CORS issues with cached images
  416. // Source: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
  417. url += (/\?/.test(url) ? "&" : "?") + new Date().getTime();
  418. }
  419. //走代理,瓦片地图导出需要服务允许跨域
  420. if (url.startsWith("http") && url.indexOf(location.hostname) == -1) {
  421. if (domtoimage.proxy) {
  422. url = domtoimage.proxy(url); //回调方法,在该方法内部处理逻辑
  423. } else if (domtoimage.preUrl) {
  424. url = domtoimage.preUrl + url;
  425. }
  426. }
  427. return new Promise(function (resolve) {
  428. var request = new XMLHttpRequest();
  429. request.onreadystatechange = done;
  430. request.ontimeout = timeout;
  431. request.responseType = "blob";
  432. request.timeout = TIMEOUT;
  433. var getorpost;
  434. if (url.indexOf(location.origin) != -1) {
  435. getorpost = "get";
  436. } else {
  437. getorpost = "post";
  438. }
  439. if (domtoimage.hasGet) {
  440. getorpost = domtoimage.hasGet(url, getorpost);
  441. }
  442. request.open(getorpost, url, true);
  443. //request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  444. request.send();
  445. var placeholder;
  446. if (domtoimage.impl.options.imagePlaceholder) {
  447. var split = domtoimage.impl.options.imagePlaceholder.split(/,/);
  448. if (split && split[1]) {
  449. placeholder = split[1];
  450. }
  451. }
  452. function done() {
  453. if (request.readyState !== 4) {
  454. return;
  455. }
  456. if (request.status !== 200) {
  457. //404错误
  458. if (placeholder) {
  459. resolve(placeholder);
  460. } else {
  461. fail("无法获取资源: " + url + ", 状态: " + request.status);
  462. }
  463. } else {
  464. var encoder = new FileReader();
  465. encoder.onloadend = function () {
  466. var content = encoder.result.split(/,/)[1];
  467. resolve(content);
  468. };
  469. encoder.readAsDataURL(request.response);
  470. }
  471. }
  472. function timeout() {
  473. if (placeholder) {
  474. resolve(placeholder);
  475. } else {
  476. fail("抓取资源时发生超时(" + TIMEOUT + "ms): " + url);
  477. }
  478. }
  479. function fail(message) {
  480. console.log(message);
  481. resolve(
  482. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABlBMVEXMzMwAov9iAAKCAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg=="
  483. );
  484. }
  485. });
  486. }
  487. function dataAsUrl(content, type) {
  488. return "data:" + type + ";base64," + content;
  489. }
  490. function escape(string) {
  491. return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
  492. }
  493. function delay(ms) {
  494. return function (arg) {
  495. return new Promise(function (resolve) {
  496. setTimeout(function () {
  497. resolve(arg);
  498. }, ms);
  499. });
  500. };
  501. }
  502. function asArray(arrayLike) {
  503. var array = [];
  504. var length = arrayLike.length;
  505. for (var i = 0; i < length; i++) {
  506. array.push(arrayLike[i]);
  507. }
  508. return array;
  509. }
  510. function escapeXhtml(string) {
  511. return string.replace(/#/g, "%23").replace(/\n/g, "%0A");
  512. }
  513. function width(node) {
  514. var leftBorder = px(node, "border-left-width");
  515. var rightBorder = px(node, "border-right-width");
  516. return node.scrollWidth + leftBorder + rightBorder;
  517. }
  518. function height(node) {
  519. var topBorder = px(node, "border-top-width");
  520. var bottomBorder = px(node, "border-bottom-width");
  521. return node.scrollHeight + topBorder + bottomBorder;
  522. }
  523. function px(node, styleProperty) {
  524. var value = window.getComputedStyle(node).getPropertyValue(styleProperty);
  525. return parseFloat(value.replace("px", ""));
  526. }
  527. }
  528. function newInliner() {
  529. var URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/g;
  530. return {
  531. inlineAll: inlineAll,
  532. shouldProcess: shouldProcess,
  533. impl: {
  534. readUrls: readUrls,
  535. inline: inline,
  536. },
  537. };
  538. function shouldProcess(string) {
  539. return string.search(URL_REGEX) !== -1;
  540. }
  541. function readUrls(string) {
  542. var result = [];
  543. var match;
  544. while ((match = URL_REGEX.exec(string)) !== null) {
  545. result.push(match[1]);
  546. }
  547. return result.filter(function (url) {
  548. return !util.isDataUrl(url);
  549. });
  550. }
  551. function inline(string, url, baseUrl, get) {
  552. return Promise.resolve(url)
  553. .then(function (url) {
  554. return baseUrl ? util.resolveUrl(url, baseUrl) : url;
  555. })
  556. .then(get || util.getAndEncode)
  557. .then(function (data) {
  558. return util.dataAsUrl(data, util.mimeType(url));
  559. })
  560. .then(function (dataUrl) {
  561. return string.replace(urlAsRegex(url), "$1" + dataUrl + "$3");
  562. });
  563. function urlAsRegex(url) {
  564. return new RegExp("(url\\(['\"]?)(" + util.escape(url) + ")(['\"]?\\))", "g");
  565. }
  566. }
  567. function inlineAll(string, baseUrl, get) {
  568. if (nothingToInline()) {
  569. return Promise.resolve(string);
  570. }
  571. return Promise.resolve(string)
  572. .then(readUrls)
  573. .then(function (urls) {
  574. var done = Promise.resolve(string);
  575. urls.forEach(function (url) {
  576. done = done.then(function (string) {
  577. return inline(string, url, baseUrl, get);
  578. });
  579. });
  580. return done;
  581. });
  582. function nothingToInline() {
  583. return !shouldProcess(string);
  584. }
  585. }
  586. }
  587. function newFontFaces() {
  588. return {
  589. resolveAll: resolveAll,
  590. impl: {
  591. readAll: readAll,
  592. },
  593. };
  594. function resolveAll() {
  595. return readAll(document)
  596. .then(function (webFonts) {
  597. return Promise.all(
  598. webFonts.map(function (webFont) {
  599. return webFont.resolve();
  600. })
  601. );
  602. })
  603. .then(function (cssStrings) {
  604. return cssStrings.join("\n");
  605. });
  606. }
  607. function readAll() {
  608. return Promise.resolve(util.asArray(document.styleSheets))
  609. .then(getCssRules)
  610. .then(selectWebFontRules)
  611. .then(function (rules) {
  612. return rules.map(newWebFont);
  613. });
  614. function selectWebFontRules(cssRules) {
  615. return cssRules
  616. .filter(function (rule) {
  617. return rule.type === CSSRule.FONT_FACE_RULE;
  618. })
  619. .filter(function (rule) {
  620. return inliner.shouldProcess(rule.style.getPropertyValue("src"));
  621. });
  622. }
  623. function getCssRules(styleSheets) {
  624. var cssRules = [];
  625. styleSheets.forEach(function (sheet) {
  626. try {
  627. util.asArray(sheet.cssRules || []).forEach(cssRules.push.bind(cssRules));
  628. } catch (e) {
  629. console.log("Error while reading CSS rules from " + sheet.href, e.toString());
  630. }
  631. });
  632. return cssRules;
  633. }
  634. function newWebFont(webFontRule) {
  635. return {
  636. resolve: function resolve() {
  637. var baseUrl = (webFontRule.parentStyleSheet || {}).href;
  638. return inliner.inlineAll(webFontRule.cssText, baseUrl);
  639. },
  640. src: function () {
  641. return webFontRule.style.getPropertyValue("src");
  642. },
  643. };
  644. }
  645. }
  646. }
  647. function newImages() {
  648. return {
  649. inlineAll: inlineAll,
  650. impl: {
  651. newImage: newImage,
  652. },
  653. };
  654. function newImage(element) {
  655. return {
  656. inline: inline,
  657. };
  658. function inline(get) {
  659. if (util.isDataUrl(element.src)) {
  660. return Promise.resolve();
  661. }
  662. return Promise.resolve(element.src)
  663. .then(get || util.getAndEncode)
  664. .then(function (data) {
  665. return util.dataAsUrl(data, util.mimeType(element.src));
  666. })
  667. .then(function (dataUrl) {
  668. return new Promise(function (resolve, reject) {
  669. element.onload = resolve;
  670. element.onerror = reject;
  671. element.src = dataUrl;
  672. });
  673. });
  674. }
  675. }
  676. function inlineAll(node) {
  677. if (!(node instanceof Element)) {
  678. return Promise.resolve(node);
  679. }
  680. return inlineBackground(node).then(function () {
  681. if (node instanceof HTMLImageElement) {
  682. return newImage(node).inline();
  683. } else {
  684. return Promise.all(
  685. util.asArray(node.childNodes).map(function (child) {
  686. return inlineAll(child);
  687. })
  688. );
  689. }
  690. });
  691. function inlineBackground(node) {
  692. var background = node.style.getPropertyValue("background");
  693. if (!background) {
  694. return Promise.resolve(node);
  695. }
  696. return inliner
  697. .inlineAll(background)
  698. .then(function (inlined) {
  699. node.style.setProperty("background", inlined, node.style.getPropertyPriority("background"));
  700. })
  701. .then(function () {
  702. return node;
  703. });
  704. }
  705. }
  706. }
  707. })(window);