pngtest.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. /* 設置body背景透明 */
  9. body {
  10. background-color: transparent;
  11. margin: 0;
  12. padding: 0;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. height: 100vh;
  17. }
  18. /* 設置容器div背景透明 */
  19. .chart-container {
  20. background-color: transparent;
  21. padding: 20px;
  22. border-radius: 8px;
  23. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="chart-container" id="capture">
  29. <p>透明背景測試</p>
  30. <canvas id="myChart" width="400" height="400"></canvas>
  31. </div>
  32. <br>
  33. <button id="downloadBtn">Download as Transparent PNG</button>
  34. <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
  35. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  36. <script src="https://cdn.jsdelivr.net/npm/chart.js@3.0.0/dist/chart.min.js"></script>
  37. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-3d@1.0.0-beta.2/dist/chartjs-plugin-3d.min.js"></script>
  38. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0"></script>
  39. <script>
  40. // 設定圖表
  41. const ctx = document.getElementById('myChart').getContext('2d');
  42. const myChart = new Chart(ctx, {
  43. type: 'bar',
  44. data: {
  45. labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
  46. datasets: [{
  47. label: '# of Votes',
  48. data: [12, 19, 3, 5, 2, 3],
  49. backgroundColor: [
  50. 'rgba(255, 99, 132, 0.2)',
  51. 'rgba(54, 162, 235, 0.2)',
  52. 'rgba(255, 206, 86, 0.2)',
  53. 'rgba(75, 192, 192, 0.2)',
  54. 'rgba(153, 102, 255, 0.2)',
  55. 'rgba(255, 159, 64, 0.2)'
  56. ],
  57. borderColor: [
  58. 'rgba(255, 99, 132, 1)',
  59. 'rgba(54, 162, 235, 1)',
  60. 'rgba(255, 206, 86, 1)',
  61. 'rgba(75, 192, 192, 1)',
  62. 'rgba(153, 102, 255, 1)',
  63. 'rgba(255, 159, 64, 1)'
  64. ],
  65. borderWidth: 1
  66. }]
  67. },
  68. options: {
  69. scales: {
  70. y: {
  71. beginAtZero: true
  72. }
  73. },
  74. // 確保背景透明
  75. plugins: {
  76. legend: {
  77. labels: {
  78. color: 'rgba(0, 0, 0, 1)' // 修改圖例顏色以適應透明背景
  79. }
  80. }
  81. },
  82. responsive: true,
  83. maintainAspectRatio: true,
  84. backgroundColor: 'transparent'
  85. }
  86. });
  87. // 下載圖片按鈕
  88. document.getElementById('downloadBtn').addEventListener('click', function () {
  89. const captureElement = document.getElementById('capture');
  90. html2canvas(captureElement, {
  91. backgroundColor: null, // 保持背景透明
  92. useCORS: true // 若有跨域圖片,需設置此選項
  93. }).then(function (canvas) {
  94. const link = document.createElement('a');
  95. link.href = canvas.toDataURL('image/png');
  96. link.download = 'chart-container.png';
  97. link.click();
  98. });
  99. });
  100. </script>
  101. </body>
  102. </html>