上一節咱們學習瞭如何用路徑繪製各類形狀,但咱們只能用默認的顏色和線條。這節就來學習設置不一樣的顏色和線條樣式。javascript
設置顏色主要有兩個屬性:java
fillStyle = color
設置填充顏色
strokeStyle = color
設置描邊顏色canvas
顏色值能夠用十六進制也能夠用一些內置的顏色字符,還能夠用rgb和rgba格式。數組
// these all set the fillStyle to 'orange' ctx.fillStyle = 'orange'; ctx.fillStyle = '#FFA500'; ctx.fillStyle = 'rgb(255, 165, 0)'; ctx.fillStyle = 'rgba(255, 165, 0, 1)';
下面來看看一個填充顏色的例子和一個描邊顏色的例子:ide
在下面這個例子中,咱們建立了6X6的方塊,每一個方塊都填充了不一樣的顏色。根據i、j的值,生成R通道和G通道的值,而B通道的值爲固定值0。學習
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); for (var i = 0; i < 6; i++) { for (var j = 0; j < 6; j++) { ctx.fillStyle = 'rgb(' + Math.floor(255 - 42.5 * i) + ', ' + Math.floor(255 - 42.5 * j) + ', 0)'; ctx.fillRect(j * 25, i * 25, 25, 25); } } }
這個例子和上一個例子相似。在這個例子中,R通道的值固定,G和B通道的值根據i、j的值變化。動畫
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); for (var i = 0; i < 6; i++) { for (var j = 0; j < 6; j++) { ctx.strokeStyle = 'rgb(0, ' + Math.floor(255 - 42.5 * i) + ', ' + Math.floor(255 - 42.5 * j) + ')'; ctx.beginPath(); ctx.arc(12.5 + j * 25, 12.5 + i * 25, 10, 0, Math.PI * 2, true); ctx.stroke(); } } }
若是沒有設置fillStyle或strokeStyle,則默認的fillStyle或strokeStyle是黑色,若是設置了fillStyle或strokeStyle,則默認的顏色就變成設置的顏色。spa
咱們能夠直接經過rgba的方式設置顏色從而實現透明的效果,以下:3d
// Assigning transparent colors to stroke and fill style ctx.strokeStyle = 'rgba(255, 0, 0, 0.5)'; ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
咱們還能夠設置全局的透明度,設置了全局透明度,以後繪製的圖形都會是這個透明度。全局透明度的值是0~1。code
globalAlpha = transparencyValue
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); // draw background ctx.fillStyle = '#FD0'; ctx.fillRect(0, 0, 75, 75); ctx.fillStyle = '#6C0'; ctx.fillRect(75, 0, 75, 75); ctx.fillStyle = '#09F'; ctx.fillRect(0, 75, 75, 75); ctx.fillStyle = '#F30'; ctx.fillRect(75, 75, 75, 75); ctx.fillStyle = '#FFF'; // set global transparency value ctx.globalAlpha = 0.2; // Draw semi transparent circles for (i = 0; i < 7; i++) { ctx.beginPath(); ctx.arc(75, 75, 10 + 10 * i, 0, Math.PI * 2, true); ctx.fill(); } }
有不少屬性和API能夠設置線條的樣式。
lineWidth = value
設置線條的寬度。
lineCap = type
設置線條端點的樣式。
lineJoin = type
設置線條鏈接處的樣式。
miterLimit = value
設置或返回最大斜接長度。
getLineDash()
獲取當前虛線的樣式,返回設置虛線的線寬數組。
setLineDash(segments)
設置當前虛線樣式。
lineDashOffset = value
肯定一條線從哪裏開始是虛線。
線寬這個屬性就是設置線的粗細。它的值不能是負數,單位是像素。默認值是1像素。
下面咱們先來看一個例子:
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); for (var i = 0; i < 10; i++) { ctx.lineWidth = 1 + i; ctx.beginPath(); ctx.moveTo(5 + i * 14, 5); ctx.lineTo(5 + i * 14, 140); ctx.stroke(); } }
效果:
注意看的話會發現,奇數位置的線條邊緣有些模糊,這是由於位置不對,這就須要瞭解線生成的機制。
如圖,圖中一個方格表示一個像素。若是要在座標(3,1)到座標(3,5)之間畫一條1像素寬的直線,那麼線的寬度將會如圖中第一張圖的深藍部分所示,左右的寬度只佔半個像素。半個像素是沒法繪製的,因此實際繪製的線條是第二章圖所示的內容。它實際的位置並不正確。
lineCap屬性設置了線段端點的樣式。它的值有三種:
butt (默認值)
端點是方的。
round
端點是圓的。
square
端點多出一個寬度和線寬同樣,長度是線寬通常的方塊。
三種樣式從左到右如圖:
這個屬性設置了線段鏈接處的樣式。
它的值有三種:
round
鏈接處是圓的。
bevel
鏈接處是一個三角形。
miter(默認值)
鏈接處是一個菱形。
從上到下效果如圖:
miterlimit屬性就是對上文miter做控制的一個屬性。簡單的說,miterlimit屬性就是控制miter的大小的。
下面來簡單說明一下它的效果:
上面三張圖分別是miterlimit屬性值爲一、五、10時的效果。miterlimit屬性實際上久時hi限制了鏈接處菱形的大小。
利用setLineDash(segments)方法和lineDashOffset屬性就能夠本身設置虛線的樣式。
setLineDash(segments)接受一個數組做參數,數組的第一個元素規定了虛線中每一小段虛線的長度,第二個參數規定了虛線中每一小段虛線之間的間隔距離。
lineDashOffset設置了虛線樣式是從哪裏開始的。
下面用一個螞蟻線的動畫例子來講明一下它們的用法:
var ctx = document.getElementById('canvas').getContext('2d'); var offset = 0; function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.setLineDash([4, 2]); ctx.lineDashOffset = -offset; ctx.strokeRect(10, 10, 100, 100); } function march() { offset++; if (offset > 16) { offset = 0; } draw(); setTimeout(march, 20); } march();
經過間隔一段時間增長lineDashOffset的方法達到虛線移動的效果。這個效果常常用來表示選中。
canvas能夠建立漸變對象,將漸變對象賦值給strokeStyle或fillStyle,就能夠畫出漸變的顏色。
有兩種漸變對象,一種是線性漸變,一種是徑向漸變:
createLinearGradient(x1, y1, x2, y2)
建立線性漸變對象,從點(x1, y1)開始,至點(x2, y2)結束。
createRadialGradient(x1, y1, r1, x2, y2, r2)
建立徑向漸變對象,參數是兩個圓,一個圓圓心是(x1, y1),半徑是r1,另外一個圓圓心是(x2, y2),半徑是r2。
例子:
var lineargradient = ctx.createLinearGradient(0, 0, 150, 150); var radialgradient = ctx.createRadialGradient(75, 75, 0, 75, 75, 100);
這樣就建立了漸變對象。而後用addColorStop方法添加顏色:
gradient.addColorStop(position, color)
position的值是0~1,這決定了顏色相對於漸變對象的位置,color是表示顏色的字符串,只要CSS中用來表示的顏色的方法均可以,好比十六進制、rgb或rgba。
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); // Create gradients var lingrad = ctx.createLinearGradient(0, 0, 0, 150); lingrad.addColorStop(0, '#00ABEB'); lingrad.addColorStop(0.5, '#fff'); lingrad.addColorStop(0.5, '#26C000'); lingrad.addColorStop(1, '#fff'); var lingrad2 = ctx.createLinearGradient(0, 50, 0, 95); lingrad2.addColorStop(0.5, '#000'); lingrad2.addColorStop(1, 'rgba(0, 0, 0, 0)'); // assign gradients to fill and stroke styles ctx.fillStyle = lingrad; ctx.strokeStyle = lingrad2; // draw shapes ctx.fillRect(10, 10, 130, 130); ctx.strokeRect(50, 50, 50, 50); }
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); // Create gradients var radgrad = ctx.createRadialGradient(45, 45, 10, 52, 50, 30); radgrad.addColorStop(0, '#A7D30C'); radgrad.addColorStop(0.9, '#019F62'); radgrad.addColorStop(1, 'rgba(1, 159, 98, 0)'); var radgrad2 = ctx.createRadialGradient(105, 105, 20, 112, 120, 50); radgrad2.addColorStop(0, '#FF5F98'); radgrad2.addColorStop(0.75, '#FF0188'); radgrad2.addColorStop(1, 'rgba(255, 1, 136, 0)'); var radgrad3 = ctx.createRadialGradient(95, 15, 15, 102, 20, 40); radgrad3.addColorStop(0, '#00C9FF'); radgrad3.addColorStop(0.8, '#00B5E2'); radgrad3.addColorStop(1, 'rgba(0, 201, 255, 0)'); var radgrad4 = ctx.createRadialGradient(0, 150, 50, 0, 140, 90); radgrad4.addColorStop(0, '#F4F201'); radgrad4.addColorStop(0.8, '#E4C700'); radgrad4.addColorStop(1, 'rgba(228, 199, 0, 0)'); // draw shapes ctx.fillStyle = radgrad4; ctx.fillRect(0, 0, 150, 150); ctx.fillStyle = radgrad3; ctx.fillRect(0, 0, 150, 150); ctx.fillStyle = radgrad2; ctx.fillRect(0, 0, 150, 150); ctx.fillStyle = radgrad; ctx.fillRect(0, 0, 150, 150); }
如今介紹的這個方法能夠用圖片去填充圖形。
createPattern(image, type)
這個方法建立並返回了一個pattern對象,image參數是CanvasImageSource,HTML圖片元素、canvas或
type參數有以下幾種值:
repeat
在垂直和水平方向上重複平鋪圖片。
repeat-x
水平平鋪圖片。
repeat-y
垂直平鋪圖片。
no-repeat
不平鋪重複圖片。
pattern對象的建立方法和漸變對象相似:
var img = new Image(); img.src = 'someimage.png'; var ptrn = ctx.createPattern(img, 'repeat');
這個方法和drawImage相似,要確保圖片加載完,不然圖片不能顯示。
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); // create new image object to use as pattern var img = new Image(); img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png'; img.onload = function() { // create pattern var ptrn = ctx.createPattern(img, 'repeat'); ctx.fillStyle = ptrn; ctx.fillRect(0, 0, 150, 150); } }
陰影涉及四個屬性:
shadowOffsetX = float
陰影水平方向距離。默認值爲0。不受transform變換矩陣影響。
shadowOffsetY = float
陰影垂直方向距離。默認值爲0。不受transform變換矩陣影響。
shadowBlur = float
陰影模糊大小。默認值爲0。模糊數值並非模糊的像素的大小,是模糊的程度。不受transform變換矩陣影響。
shadowColor = color
陰影顏色。默認值是全透明黑色。
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 2; ctx.shadowBlur = 2; ctx.shadowColor = 'rgba(0, 0, 0, 0.5)'; ctx.font = '20px Times New Roman'; ctx.fillStyle = 'Black'; ctx.fillText('Sample String', 5, 30); // 後面兩個參數是x, y座標 }
若是兩個路徑交叉或重疊,咱們能夠設置填充的方式。
參數有兩種:
"nonzero": 默認值,按照non-zero winding rule規則填充。
"evenodd": 按照even-odd winding rule規則填充。
function draw() { var ctx = document.getElementById('canvas').getContext('2d'); ctx.beginPath(); ctx.arc(50, 50, 30, 0, Math.PI * 2, true); ctx.arc(50, 50, 15, 0, Math.PI * 2, true); ctx.fill('evenodd'); }