翻譯|使用教程|編輯:楊鵬連|2020-10-26 11:40:58.200|閱讀 255 次
概述:對于許多應用程序來說,使用GoJS創建圖的圖像可能很有用,并且此頁面詳細介紹了此任務的一些選項。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
GoJS是一款功能強大,快速且輕量級的流程圖控件,可幫助你在JavaScript 和HTML5 Canvas程序中創建流程圖,且極大地簡化您的JavaScript / Canvas 程序。
對于許多應用程序來說,使用GoJS創建圖的圖像可能很有用,并且此頁面詳細介紹了此任務的一些選項。
Puppeteer
Puppeteer是一個Node庫,它提供了高級API來控制無頭Chrome。我們可以使用它在服務器端創建圖像。如果安裝了Node和npm,則可以使用進行安裝npm install puppeteer。
以下代碼是使用Puppeteer的一個小示例。如果將JavaScript另存為puppet.js,并使用node(node createImage.js)運行它,它將演示如何創建兩個圖像:一個來自圖gojs-screenshot.png,一個來自HTML頁面,一個來自page-screenshot.png。樣本中的圖表代碼與最小樣本中的代碼相同。
// This example loads the GoJS library then adds HTML from scratch and evaluates some JavaScript, // then creates a screenshot of Diagram with makeImageData, plus a screenshot of the page. const puppeteer = require('puppeteer'); const fs = require('fs'); const parseDataUrl = (dataUrl) => { const matches = dataUrl.match(/^data:(.+);base64,(.+)$/); if (matches.length !== 3) { throw new Error('Could not parse data URL.'); } return { mime: matches[1], buffer: Buffer.from(matches[2], 'base64') }; }; (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Point to a version of go.js, either a local file or one on the web at a CDN await page.addScriptTag({ url: '//unpkg.com/gojs' // path: '../../release/go.js' }); // Create HTML for the page: page.setContent('<div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div>'); // Set up a Diagram, and return the result of makeImageData: const imageData = await page.evaluate(() => { var $ = go.GraphObject.make; var myDiagram = $(go.Diagram, "myDiagramDiv", { "animationManager.isEnabled": false, "undoManager.isEnabled": true // enable undo & redo }); // define a simple Node template myDiagram.nodeTemplate = $(go.Node, "Auto", // the Shape will go around the TextBlock $(go.Shape, "RoundedRectangle", { strokeWidth: 0 }, new go.Binding("fill", "color")), $(go.TextBlock, { margin: 8 }, new go.Binding("text", "key")) ); myDiagram.model = new go.GraphLinksModel( [ { key: "Alpha", color: "lightblue" }, { key: "Beta", color: "orange" }, { key: "Gamma", color: "lightgreen" }, { key: "Delta", color: "pink" } ], [ { from: "Alpha", to: "Beta" }, { from: "Alpha", to: "Gamma" }, { from: "Beta", to: "Beta" }, { from: "Gamma", to: "Delta" }, { from: "Delta", to: "Alpha" } ]); return myDiagram.makeImageData(); }); // Output the GoJS makeImageData as a .png: const { buffer } = parseDataUrl(imageData); fs.writeFileSync('diagram-screenshot.png', buffer, 'base64'); // Output a page screenshot await page.screenshot({ path: 'page-screenshot.png' }); await browser.close(); })();您還可以使用Puppeteer來獲取實時HTML頁面并執行相同的操作:
// This example loads a web page with a GoJS diagram, // then creates a screenshot of the Diagram with makeImageData, plus a screenshot of the page. const puppeteer = require('puppeteer'); const fs = require('fs'); const parseDataUrl = (dataUrl) => { const matches = dataUrl.match(/^data:(.+);base64,(.+)$/); if (matches.length !== 3) { throw new Error('Could not parse data URL.'); } return { mime: matches[1], buffer: Buffer.from(matches[2], 'base64') }; }; (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // This does not have to be a page on the web, it can be a localhost page, or file:// await page.goto('//gojs.net/samples/orgChartEditor.html', { waitUntil: 'networkidle2' // ensures images are loaded }); const imageData = await page.evaluate(() => { window.myDiagram.animationManager.stopAnimation(); return window.myDiagram.makeImageData({ background: window.myDiagram.div.style.backgroundColor }); }); // Output the GoJS makeImageData as a .png: const { buffer } = parseDataUrl(imageData); fs.writeFileSync('diagram-screenshot.png', buffer, 'base64'); // Output a page screenshot await page.screenshot({ path: 'page-screenshot.png' }); await browser.close(); })();
下載SVG文件
如果您希望用戶下載SVG文件,則無需涉及Web服務器。請參閱樣本。請注意,該示例僅下載一個SVG文件,但是該文件可以覆蓋整個文檔。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: