#1 canvas 線條基本API

canvas是基于狀態(tài)進(jìn)行繪制的。在真正繪制(比如調(diào)用fill, stroke)之前,對(duì)畫布的設(shè)置都是對(duì)畫布狀態(tài)的設(shè)置

// 獲取頁(yè)面中的canvas元素
var canvas = document.getElementById('#myCanvas');

// 設(shè)置畫布大小
canvas.width = 800;
canvas.height = 800;

// 創(chuàng)建一個(gè)2d的context
var ctx = canvas.getContext('2d');

// 設(shè)置線的寬度
ctx.lineWidth = 10;

方法

beginPath()

開始一段全新的路徑,如果沒(méi)有beginPath()則相當(dāng)于連續(xù)的狀態(tài).比如想要給2條折線添加不同的顏色

不使用beginPath的情況:(失敗, 最終結(jié)果均為blue

// 第一段折線
ctx.moveTo(100, 200)
ctx.lineTo(300, 400)
ctx.lineTo(100, 600)
ctx.strokeStyle = 'black'
ctx.stroke()

// 第二段折線
ctx.moveTo(300, 200)
ctx.lineTo(500, 400)
ctx.lineTo(300, 600)
ctx.strokeStyle = "blue"
ctx.stroke()

使用beginPath,相當(dāng)于每次都重新開始一段路勁:(成功

// 第一段折線
ctx.beginPath()  // 開始這個(gè)可以省略
ctx.moveTo(100, 200) // 可以替換成ctx.lineTo(100, 200), 結(jié)果一致
ctx.lineTo(300, 400)
ctx.lineTo(100, 600)
ctx.strokeStyle = 'black'
ctx.stroke()

// 第二段折線
ctx.beginPath()
ctx.moveTo(300, 200)
ctx.lineTo(500, 400)
ctx.lineTo(300, 600)
ctx.strokeStyle = "blue"
ctx.stroke()    
  • moveTo(): 表示不從任何點(diǎn)開始,重新指定一個(gè)新的坐標(biāo)點(diǎn)
  • lineTo(): 表示從上一個(gè)點(diǎn)到另一個(gè)點(diǎn)
  • beginPath() : 會(huì)清空坐標(biāo)點(diǎn),清空不是指還原到(0, 0)坐標(biāo)點(diǎn),而是沒(méi)有,beginPath() + lineTo() 相當(dāng)于一次 moveTo()

closePath()

這個(gè)和上面的 beginPath() 成對(duì)使用, 用于繪制封閉的多邊形

比如繪制一個(gè)箭頭:

ctx.beginPath()
ctx.moveTo(100, 350)
ctx.lineTo(500, 350)
ctx.lineTo(500, 200)
ctx.lineTo(700, 400)
ctx.lineTo(500, 600)
ctx.lineTo(500, 450)
ctx.lineTo(100, 450)
ctx.closePath()

ctx.strokeStyle = '#056'
ctx.stroke()

對(duì)于封閉圖形進(jìn)行填充 fill()

在上面代碼的基礎(chǔ)上,添加填充色:

# 狀態(tài)的描述
ctx.fillStyle = 'orangered'
ctx.strokeStyle = '#056'

# 真正的繪制過(guò)程
ctx.fill()
ctx.stroke()

值得注意的是,上面的 fill() 和 stroke() 執(zhí)行的先后順序,對(duì)繪制的結(jié)果是不一樣的,這需要注意!!!

繪制矩形 fillRect() strokeRect()

通過(guò)上面的學(xué)習(xí),我們可以進(jìn)行以下封裝來(lái)寫個(gè)函數(shù)用來(lái)繪制矩形:

/*
 * @param ctx: canvas context
 * @param x, y: 起點(diǎn)坐標(biāo)
 * @param width, height: 矩形寬高
 * @param borderWidth: 描邊寬度
 * @param borderColor: 描邊顏色
 * @param fillColor: 填充顏色
 */
function drawRect(ctx, x, y, width, height, borderWidth, borderColor, fillColor) {
  ctx.lineWidth = borderWidth();
  
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(x + width, y);
  ctx.lineTo(x + width, y + height);
  ctx.lineTo(x, y + heigth);
  ctx.closePath()

  ctx.fillStyle = fillColor;
  ctx.strokeStyle = borderColor;

  ctx.fill();
  ctx.stroke();
}

// 繪制
drawRect(ctx, 100, 100, 200, 200, 10, 'green', 'red');

但是實(shí)際上,canvas本身就提供了矩形的繪制方法:

fillRect(x, y, width, height)strokeRect(x, y, width, height)

上面的方法可以重構(gòu)為:

function drawRect(ctx, x, y, width, height, borderWidth, borderColor, fillColor) {
  ctx.lineWidth = borderWidth();
  ctx.fillStyle = fillColor;
  ctx.strokeStyle = borderColor;  

  ctx.fillRect(x, y, width, height);
  ctx.strokeRect(x, y, width, height);
}

// 繪制一個(gè)填充為半透明的矩形
drawRect(200, 200, 200, 200, 10, 'green', 'rgba(0, 123, 12, 0.5)');

總結(jié)

學(xué)習(xí)了一些基本的方法和屬性:

  • moveTo()
  • lineTo()
  • beginPath(), closePath()
  • strokeStyle, fillStyle 的基本用法
  • lineWidth: 線條的寬度
  • fill(), stroke()
  • 矩形的基本繪制: fillRect(x, y, width, height), strokeRect(x, y, width, height)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一:canvas簡(jiǎn)介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,882評(píng)論 2 32
  • 一、canvas簡(jiǎn)介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    Looog閱讀 4,052評(píng)論 3 40
  • 熟悉html5的程序員們肯定都知道 元素,該元素是用來(lái)在頁(yè)面中規(guī)定一塊區(qū)域,然后由js在該區(qū)域內(nèi)繪制圖形。canv...
    米幾V閱讀 2,343評(píng)論 1 5
  • 49/100 2017.3.27 1去表姐家聊天,提了辭職的事,她說(shuō)可行,要做好最壞打算。要溝通,要對(duì)得起自己,做...
    aseeya閱讀 227評(píng)論 0 0
  • 網(wǎng)址:圖像峰值信噪比的計(jì)算 - 豆丁網(wǎng) 實(shí)現(xiàn)灰度圖像峰值信噪比計(jì)算 - 松子茶的專欄 - 博客頻道 ...
    horu閱讀 10,655評(píng)論 1 0

友情鏈接更多精彩內(nèi)容