Skip to content

Commit

Permalink
feat: 完善水印 hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyh2001 committed Sep 22, 2023
1 parent 018815a commit 98aef1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
37 changes: 32 additions & 5 deletions packages/fighting-design/_hooks/use-watermark/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { computed } from 'vue'
import type { WatermarkProps } from '../../watermark'
import type { ComputedRef } from 'vue'

export const useWatermark = (prop) => {
return computed(() => {
export type UseWatermarkReturn = ComputedRef<{
base64: string
size: number
}>

/**
* 生成水印图片
*
* @param { Object } prop prop 参数
* @returns
*/
export const useWatermark = (prop: WatermarkProps): UseWatermarkReturn => {
return computed((): { base64: string; size: number } => {
/**
* 创建一个 canvas
*
Expand Down Expand Up @@ -30,10 +43,24 @@ export const useWatermark = (prop) => {

ctx.font = font

const { width } = ctx.measureText(prop.text)
const { width } = ctx.measureText(prop.content)

const cavasSize = Math.max(100, width) * prop.gap * devicePixeRatio

// const cavasSize = Math.
canvas.width = cavasSize
canvas.height = cavasSize

ctx.translate(canvas.width / 2, canvas.height / 2)
ctx.rotate((Math.PI / 190) * -45)
ctx.fillStyle = '#111'
ctx.font = font
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillText(prop.content, 0, 0)

return {}
return {
base64: canvas.toDataURL(),
size: cavasSize / devicePixeRatio
} as const
})
}
4 changes: 2 additions & 2 deletions packages/fighting-design/watermark/src/props.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
setBooleanProp,
setStringProp,
setStringNumberProp,
setNumberProp
} from '../../_utils'
import type { ExtractPropTypes } from 'vue'
Expand All @@ -13,8 +12,9 @@ export const Props = {
width: setNumberProp(280),
/** 水印的高度 */
height: setNumberProp(200),
gap: setNumberProp(20),
/** 文字大小 */
fontSize: setStringNumberProp('30px'),
fontSize: setNumberProp(24),
/** 文字颜色 */
fontColor: setStringProp<string>('#333'),
/** 自定义图片水印 */
Expand Down

0 comments on commit 98aef1e

Please sign in to comment.