Skip to content

Commit

Permalink
Merge pull request #1 from VSirrr/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
VSirrr authored Jul 8, 2022
2 parents 06dc0b0 + a2c32bf commit 5c92adc
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ package-lock.json
*.njsproj
*.sln
*.sw?

dist
11 changes: 11 additions & 0 deletions CHANGLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# CHANGELOG

- v0.1.4

-**name** 字段增加类型判断
- 去除 **name** 字段中的空格
- **Personl****name** 字段中 **·** 字符宽度的适配

- v0.1.3

- 发布初版
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>canvas-seal</title>
<title>使用 canvas 绘制印章</title>
</head>

<body>
Expand Down Expand Up @@ -35,7 +35,7 @@

const person = new Seal.Personal('#person', {
// 姓名
name: '张三',
name: '尼古·拉斯·赵四',
// 是否有边框,默认为 true
border: true
})
Expand Down
5 changes: 0 additions & 5 deletions dist/index.js

This file was deleted.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"name": "@vsirrr/seal",
"version": "0.1.3",
"version": "0.1.4",
"description": "使用 canvas 绘制公司公章、个人印章",
"main": "dist/index.js",
"module": "dist/index.js",
"unpkg": "dist/index.js",
"files": [
"dist/*",
"src/*"
"dist/*"
],
"scripts": {
"prepare": "husky install",
"dev": "rollup -w -c --environment MODE:dev",
"build": "rollup -c --environment MODE:prod",
"publish": "npm run build && npm publish --access public"
"publish": "npm run build && npm publish"
},
"keywords": [
"canvas",
Expand Down Expand Up @@ -48,14 +47,16 @@
"arrowParens": "avoid"
},
"lint-staged": {
"!dist/**.{js,json,html,md}": [
"prettier --write",
"git add"
"*.{js,json,html,md}": [
"prettier --write"
]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"publishConfig": {
"access": "public"
}
}
19 changes: 16 additions & 3 deletions src/Official.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Seal from './Seal.js'
import { normalize } from './_util'

export default class Official extends Seal {
static defaultOptions = {
Expand All @@ -20,6 +21,7 @@ export default class Official extends Seal {
constructor(id, options) {
super(id)
this.options = Object.assign({}, Official.defaultOptions, options || {})

this._init()
}

Expand All @@ -29,6 +31,7 @@ export default class Official extends Seal {
canvas,
options: { code, type, radius, innerLine }
} = this

canvas.width = canvas.height = radius * 2
ctx.clearRect(0, 0, canvas.width, canvas.height)

Expand Down Expand Up @@ -110,16 +113,22 @@ export default class Official extends Seal {
options: { name, color, radius, fontSize, fontFamily }
} = this

if ('string' !== typeof name) {
throw new TypeError('name 字段必须是字符串类型')
}

const companyName = normalize(name)

ctx.fillStyle = color
ctx.font = `normal normal normal ${fontSize}px ${fontFamily}`
ctx.textAlign = 'left'
ctx.textBaseline = 'middle'
ctx.translate(radius, radius)

for (let i = 0, length = name.length; i < length; i++) {
for (let i = 0, length = companyName.length; i < length; i++) {
ctx.save()
ctx.rotate(-0.17 * (length - 1) + i * 0.34)
ctx.fillText(name[i], -fontSize / 2, -radius * 0.73)
ctx.fillText(companyName[i], -fontSize / 2, -radius * 0.73)
ctx.restore()
}
}
Expand All @@ -130,9 +139,13 @@ export default class Official extends Seal {
options: { type, radius, typeFontSize, fontFamily }
} = this

if ('string' !== typeof type) {
throw new TypeError('type 字段必须是字符串类型')
}

ctx.font = `normal normal normal ${typeFontSize}px ${fontFamily}`
ctx.textAlign = 'center'
ctx.fillText(type, 0, radius * 0.5)
ctx.fillText(normalize(type), 0, radius * 0.5)
ctx.restore()
}

Expand Down
22 changes: 19 additions & 3 deletions src/Personal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Seal from './Seal.js'
import { normalize } from './_util'

export default class Personal extends Seal {
static defaultOptions = {
Expand All @@ -24,13 +25,28 @@ export default class Personal extends Seal {
options: { name, border, lineWidth, fontSize }
} = this

if ('string' !== typeof name) {
throw new TypeError('name 字段必须是字符串类型')
}

const personName = normalize(name)

if (border) {
const pad = lineWidth * 2
canvas.width = name.length * fontSize + pad
const dotNum = personName.replace(/[^·]/g, '').length

if (dotNum) {
canvas.width =
(personName.length - dotNum) * fontSize +
dotNum * (fontSize / 2) +
pad
} else {
canvas.width = personName.length * fontSize + pad
}
canvas.height = fontSize + pad
this._drawBorder()
} else {
canvas.width = name.length * fontSize
canvas.width = personName.length * fontSize
canvas.height = fontSize
}

Expand Down Expand Up @@ -65,7 +81,7 @@ export default class Personal extends Seal {
ctx.font = `normal normal normal ${fontSize}px ${fontFamily}`

ctx.fillText(
name,
normalize(name),
border ? lineWidth : 0,
fontSize / 2 + (border ? lineWidth : 0)
)
Expand Down
3 changes: 3 additions & 0 deletions src/_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function normalize(name) {
return name.replace(/\s/g, '')
}

0 comments on commit 5c92adc

Please sign in to comment.