canvas 在線制作圓形、方形、印章

<template>
  <div>
       <canvas id="canvas" width="166" height="166"></canvas>
  </div>
<template>

export default {
 data () {
    return {
       isColor: 'red',//印章顏色
       checked: '', // 橫向文
       checked1: '', // 下弦文
       shape: 1, // 印章形狀(1.方形 2.圓形 )
       sealType: '3', // 印章類型(1.公章 2.法人章 3.財(cái)務(wù)專屬章)
  },
  methods: {
      //圓形印章
    createSeal (id, company, name) {
      let canvas = document.getElementById(id)
      let context = canvas.getContext('2d')
     // 繪畫邊框
      let width = canvas.width / 2
      let height = canvas.height / 2
      context.lineWidth = 7
      context.strokeStyle = this.isColor
      context.beginPath()
      context.arc(width, height, width - 7, 0, Math.PI * 2)
      context.stroke()
      // 畫五角星
      create5star(context, width, height, 10, this.isColor, 0)
      // 繪制印章名稱
      context.font = '10px Helvetica'
      context.textBaseline = 'middle'// 設(shè)置文本的垂直對(duì)齊方式
      context.textAlign = 'center' // 設(shè)置文本的水平對(duì)對(duì)齊方式
      context.lineWidth = 1
      context.fillStyle = this.isColor
      context.fillText(this.transverseWord, width, height + 40) // 繪畫文字(橫行文)
      context.font = '8px Helvetica'
      context.fillText(this.downWord, width, height + 60) // 繪畫文字(下弦文)
     // 繪制印章單位
      context.translate(width, height)// 平移到此位置,
      context.font = '14px Helvetica'
      let count = company.length// 字?jǐn)?shù)
      let angle = 4 * Math.PI / (3 * (count - 1))// 字間角度
      let chars = company.split('')
      let c
      for (let i = 0; i < count; i++) {
        c = chars[i]// 需要繪制的字符
        if (i == 0) { context.rotate(5 * Math.PI / 6) } else { context.rotate(angle) }
        context.save()
        context.translate(60, 0)// 平移到此位置,此時(shí)字和x軸垂直
        context.rotate(Math.PI / 2)// 旋轉(zhuǎn)90度,讓字平行于x軸
        context.fillText(c, 0, 5)// 此點(diǎn)為字的中心點(diǎn)
        context.restore()
      }
        // 繪制五角星
        /**
     * 創(chuàng)建一個(gè)五角星形狀. 該五角星的中心坐標(biāo)為(sx,sy),中心到頂點(diǎn)的距離為radius,rotate=0時(shí)一個(gè)頂點(diǎn)在對(duì)          稱軸上
   * rotate:繞對(duì)稱軸旋轉(zhuǎn)rotate弧度
   */
      function create5star (context, sx, sy, radius, color, rotato) {
        context.save()
        context.fillStyle = color
        context.translate(sx, sy)// 移動(dòng)坐標(biāo)原點(diǎn)
        context.rotate(Math.PI + rotato)// 旋轉(zhuǎn)
        context.beginPath()// 創(chuàng)建路徑
        let x = Math.sin(0)
        let y = Math.cos(0)
        let dig = Math.PI / 5 * 4
        for (let i = 0; i < 5; i++) { // 畫五角星的五條邊
          let x = Math.sin(i * dig)
          let y = Math.cos(i * dig)
          context.lineTo(x * radius, y * radius)
          // context.lineTo()
        }
        context.closePath()
        context.stroke()
        context.fill()
        context.restore()
      }
  },
//方形印章
  createSealSquare(){
        // // 獲取畫布
      let canvas = document.getElementById('canvas')
      let context = canvas.getContext('2d')

      let centerPoint = canvas.width / 2
      // 名稱
      let personName = sealData.sealCompany || '李四'
      // 字體
      let fontFamily = 'Helvetica'
      // 顏色
      let color = this.isColor
      // 邊框線寬度
      let personLineWidth = 4
      // 字體大小
      let personNameFontSize = 65
      // 字與邊框的距離
      let lineTextGap = 0
      let leftTopPointX = 2.4 * (personNameFontSize + lineTextGap)
      let leftTopPointY = (personNameFontSize + lineTextGap)
      // 矩形寬高
      let wide = 2 * (personNameFontSize + lineTextGap)
      let high = 2 * (personNameFontSize + lineTextGap)
      // 字體加粗
      let fontWeight = 'bold'
      let textPoint = personNameFontSize / 2
      let length = personName.length
      if (length < 2 || length > 4) {
        alert('名稱只能為2~4個(gè)字符!')
        throw new RangeError('名稱只能為2~4個(gè)字符!')
      }
      if (typeof personName !== 'string') {
        alert('只能是字符串!')
        throw new TypeError('只能是字符串!')
      }
      switch (length) {
        case 2:
          personName += '之印'
          break
        case 3:
          personName += '印'
          break
      }
      context.save()
      context.fillStyle = color
      context.textBaseline = 'middle'
      context.textAlign = 'center'
      context.font = 'normal normal bold ' + personNameFontSize + 'px ' + fontFamily
      context.translate(canvas.width / 2, canvas.width / 2)
      context.fillText(personName.charAt(0), textPoint, -textPoint)
      context.fillText(personName.charAt(1), textPoint, textPoint)
      context.fillText(personName.charAt(2), -textPoint, -textPoint)
      context.fillText(personName.charAt(3), -textPoint, textPoint)
      context.restore()

      // 繪制正方形
      context.fillStyle = color
      context.strokeStyle = this.isColor
      context.lineWidth = 15
      context.rect(0.5 * (canvas.width - 166), 0.5 * (canvas.height - 166), 166, 166)
      context.stroke()
  },
    //  (方形印)
    createSealSquareFinance (name) {
      let canvas = document.getElementById('canvas')
      let context = canvas.getContext('2d')

      // 繪制正方形
      context.lineWidth = 15
      context.strokeStyle = this.isColor
      context.strokeRect(0.5 * (canvas.width - 166), 0.5 * (canvas.height - 166), 166, 166)

      // 繪制文字
      context.fillStyle = this.isColor
      context.textBaseline = 'middle'
      context.textAlign = 'left'
      context.font = 'normal normal bold 25px Helvetica'
      context.translate(canvas.width / 2, canvas.width / 2)
      context.fillText('財(cái)', -60, -45)
      context.fillText('務(wù)', -60, -15)
      context.fillText('專', -60, 15)
      context.fillText('用', -60, 45)
      context.font = 'normal normal bold 16px Helvetica'
      context.translate(canvas.width / 2, canvas.width / 2)

      let count = name.length// 字?jǐn)?shù)
      let y = -120
      let x = -90
      for (let i = 0; i < count; i++) {
        if (x === -90) {
          y = -120 + (i * 20)
        } else if (x === -70) {
          y = -120 + ((i - 5) * 20)
        } else if (x === -50) {
          y = -120 + ((i - 10) * 20)
        } else if (x === -30) {
          y = -120 + ((i - 15) * 20)
        }

        if (y === -20 && x === -90) { // 第一列的坐標(biāo)
          y = -120
          x = -70
        } else if (y === -20 && x === -70) { // 第二列的坐標(biāo)
          y = -120
          x = -50
        } else if (y === -20 && x === -50) { // 第三列的坐標(biāo)
          y = -120
          x = -30
        }

        context.fillText(name[i], x, y)
      }
    },

}
?著作權(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)容

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