feat:提交
This commit is contained in:
39
tests/dev1/dataHandle/common/drawing/canvasUtil.ts
Normal file
39
tests/dev1/dataHandle/common/drawing/canvasUtil.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { DrawTextOption } from './base'
|
||||
|
||||
// const canvas = new OffscreenCanvas(0, 0);
|
||||
const canvas = document.createElement('canvas')
|
||||
const canvasCtx = canvas.getContext('2d')
|
||||
|
||||
export function blobToDataURL(data: Blob): Promise<string>
|
||||
{
|
||||
return new Promise((resolve) =>
|
||||
{
|
||||
let reader = new FileReader()
|
||||
reader.onload = (response) =>
|
||||
{
|
||||
resolve(response.target.result as string)
|
||||
}
|
||||
reader.readAsDataURL(data)
|
||||
})
|
||||
}
|
||||
|
||||
export function getStringWidth(
|
||||
text: string,
|
||||
width: number,
|
||||
height: number,
|
||||
option: DrawTextOption,
|
||||
): number
|
||||
{
|
||||
canvasCtx.clearRect(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height,
|
||||
)
|
||||
canvasCtx.textAlign = option.textAlign as CanvasTextAlign
|
||||
canvasCtx.textBaseline = option.textBaseline as CanvasTextBaseline
|
||||
canvasCtx.fillStyle = option.fillStyle
|
||||
canvasCtx.font
|
||||
= `${option.fontWeight} ${option.fontSize}px ${option.fontFamily}`
|
||||
return canvasCtx.measureText(text).width
|
||||
}
|
Reference in New Issue
Block a user