|
| 1 | +--- |
| 2 | +permalink: /docs/org_chart.html |
| 3 | +layout: default |
| 4 | +title: Generate an Org Chart image |
| 5 | +--- |
| 6 | + |
| 7 | +{% include new_org_chart.html %} |
| 8 | + |
| 9 | +<!-- 2) Add a button near the chart --> |
| 10 | +<button id="savePng"> |
| 11 | + Download PNG |
| 12 | +</button> |
| 13 | + |
| 14 | +<!-- 3) Add this script block (after your existing <script> that renders the chart) --> |
| 15 | +<script> |
| 16 | + // If your photos might be cross-origin, ensure images don't taint the capture: |
| 17 | + // (You already create <img> elements in addPhotos—this sets crossOrigin there.) |
| 18 | + const originalAddPhotos = addPhotos; |
| 19 | + addPhotos = function(key, div, names){ |
| 20 | + names.forEach(p => { if (p && p.photo) { /* hint for CORS */ } }); |
| 21 | + originalAddPhotos(key, div, names); |
| 22 | + // Set crossOrigin on any images we just appended |
| 23 | + div.querySelectorAll('img').forEach(img => { |
| 24 | + if (!img.crossOrigin) img.crossOrigin = 'anonymous'; |
| 25 | + // If images are on a CDN you control, make sure it sends: Access-Control-Allow-Origin: * |
| 26 | + }); |
| 27 | + }; |
| 28 | + |
| 29 | + document.getElementById('savePng').addEventListener('click', async () => { |
| 30 | + const chartEl = document.getElementById('chart'); |
| 31 | + |
| 32 | + // Ensure the connector canvas is up to date (your code already draws on load) |
| 33 | + // Optionally re-run your layout routine here if you draw on resize. |
| 34 | + |
| 35 | + // 2x scale = sharper export; increase to 3 for print quality |
| 36 | + const scale = 3; |
| 37 | + |
| 38 | + const canvas = await html2canvas(chartEl, { |
| 39 | + backgroundColor: '#fff', |
| 40 | + scale, |
| 41 | + useCORS: true, |
| 42 | + logging: false, |
| 43 | + windowWidth: chartEl.scrollWidth, |
| 44 | + windowHeight: chartEl.scrollHeight |
| 45 | + }); |
| 46 | + |
| 47 | + const link = document.createElement('a'); |
| 48 | + link.download = 'uscms-sc-org-chart.png'; |
| 49 | + link.href = canvas.toDataURL('image/png'); |
| 50 | + link.click(); |
| 51 | + |
| 52 | + |
| 53 | + }); |
| 54 | +</script> |
0 commit comments