HTML5之Canvas畫正方形,html5canvas正方形
HTML5之Canvas畫正方形
1、設計源碼
<!DOCTYPE html><head><meta charset="utf-8" /><title>HTML5之Canvas畫正方形</title><script type="text/javascript"> function drawFour(id){//擷取canvas元素var canvas = document.getElementById("canvas");if(canvas == null) return false;//擷取上下文var context = canvas.getContext('2d');//設定填充圖形的樣式context.fillStyle = "#EEEEFF";//繪製圖形context.fillRect(0,0,800,800);context.fillStyle = "yellow";//設定圖形邊框的樣式context.strokeStyle = "blue";//指定線寬context.lineWidth = 150;context.fillRect(50,50,500,500);context.strokeRect(50,50,500,500);}</script></head><body onLoad="drawFour('canvas')"> <canvas id="canvas" width="1200" height="560"/></body>
2、設計結果
3、分析說明
(1)擷取Canvas元素
var canvas = document.getElementById("canvas");
(2)取到上下文
var context = canvas.getContext('2d');
(3)填充繪製邊框
context.fillStyle = "#EEEEFF";//填充的樣式
(4)設定繪圖樣式
strokeStyle:圖形邊框的樣式
(5)指定線寬
context.lineWidth = 150;
(6)指定顏色值
(7)繪製正方形
context.fillRect(50,50,500,500);
context.strokeRect(50,50,500,500);