在微信小程序繪圖API中創(chuàng)建二次方貝塞爾曲線

2022-05-09 16:47 更新

微信小程序 canvas接口和方法繪圖接口和方法

canvasContext.quadraticCurveTo


定義

創(chuàng)建二次貝塞爾曲線路徑。

Tip: 曲線的起始點為路徑中前一個點。

參數(shù)

參數(shù)類型說明
cpxNumber貝塞爾控制點的x坐標
cpyNumber貝塞爾控制點的y坐標
xNumber結束點的x坐標
yNumber結束點的y坐標

例子

const ctx = wx.createCanvasContext('myCanvas')

// Draw points
ctx.beginPath()
ctx.arc(20, 20, 2, 0, 2 * Math.PI)
ctx.setFillStyle('red')
ctx.fill()

ctx.beginPath()
ctx.arc(200, 20, 2, 0, 2 * Math.PI)
ctx.setFillStyle('lightgreen')
ctx.fill()

ctx.beginPath()
ctx.arc(20, 100, 2, 0, 2 * Math.PI)
ctx.setFillStyle('blue')
ctx.fill()

ctx.setFillStyle('black')
ctx.setFontSize(12)

// Draw guides
ctx.beginPath()
ctx.moveTo(20, 20)
ctx.lineTo(20, 100)
ctx.lineTo(200, 20)
ctx.setStrokeStyle('#AAAAAA')
ctx.stroke()

// Draw quadratic curve
ctx.beginPath()
ctx.moveTo(20, 20)
ctx.quadraticCurveTo(20, 100, 200, 20)
ctx.setStrokeStyle('black')
ctx.stroke()

ctx.draw()


針對 moveTo(20, 20) quadraticCurveTo(20, 100, 200, 20)的三個關鍵坐標如下:

  • 紅色:起始點(20, 20)
  • 藍色:控制點(20, 100)
  • 綠色:終止點(200, 20)

微信小程序 canvas接口和方法繪圖接口和方法

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號