const subStringToImage = (params) => {
const { str = '',color = '#FFF',background = '#4F54FF',size = 60,fontSize =
20 } = params
if(str.length < 0) return console.error('字符不能为空!')
const text = str.slice(0, 1)
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = size
canvas.height = size
const x = canvas.width / 2;
const y = canvas.height / 2;
const radius = size/2;
ctx.fillStyle = background;
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fill();
ctx.font = `${fontSize}px Arial`;
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillStyle = color
ctx.fillText(text,size/2,size/2);
const url = canvas.toDataURL('iamge/png')
const image = new Image()
image.src = url
document.body.appendChild(image)
}
subStringToImage({ str:'仙古梦回' })
示例: