<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>柱狀圖</title> <style> canvas{ /*下邊框 左邊框 分別表明統計圖的兩個軸線*/ border-left:1px solid #222; border-bottom:1px solid #222; } </style> <script> window.onload = function(){ // 1.獲取canvas元素 var canvas = document.getElementById('canvas'); //2.2d上下文 var context = canvas.getContext('2d'); //3.設置樣式 context.fillStyle = 'skyblue'; //4.繪製 context.fillRect(20,400-300,60,300); context.fillStyle = "pink"; context.fillRect(100,400-140,60,140); context.fillStyle = 'orange'; context.fillRect(180,400-230,80,230); context.fillStyle = 'gold'; context.fillRect(280,400-100,60,100); context.clearRect(180+(80-30)/2,400-((230-30)/2+30),30,30); } </script> </head> <body> <canvas id="canvas" width="400px" height="400px"> 瀏覽器不支持 </canvas> </body> </html>