<view class="container">
<image class="avatar" src="{{avatarUrl}}" mode="aspectFill"></image>
<view class="text-container">
<input class="input-text" placeholder="请输入文字" bindinput="onInput"></input>
<picker class="picker-font" mode="selector" range="{{fontList}}" bindchange="onFontChange">
<view class="picker-text">{{fontList[fontIndex]}}</view>
</picker>
<picker class="picker-color" mode="selector" range="{{colorList}}" bindchange="onColorChange">
<view class="picker-text" style="background-color: {{colorList[colorIndex]}};"></view>
</picker>
<slider class="slider-size" value="{{fontSize}}" min="10" max="50" step="1" bindchange="onSizeChange"></slider>
</view>
<button class="btn-generate" bindtap="onGenerate">生成头像</button>
</view>
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.avatar {
width: 200rpx;
height: 200rpx;
border-radius: 50%;
margin-bottom: 20rpx;
}
.text-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-bottom: 20rpx;
}
.input-text {
width: 80%;
height: 40rpx;
border: 1rpx solid #ccc;
border-radius: 20rpx;
padding: 0 20rpx;
margin-bottom: 10rpx;
}
.picker-font,
.picker-color {
width: 80%;
height: 40rpx;
margin-bottom: 10rpx;
}
.picker-text {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
padding: 0 10rpx;
color: #fff;
}
.slider-size {
width: 80%;
margin-bottom: 10rpx;
}
.btn-generate {
width: 80%;
height: 40rpx;
background-color: #007aff;
color: #fff;
border-radius: 20rpx;
border: none;
}
Page({
data: {
avatarUrl: '',
text: '',
fontList: ['宋体', '微软雅黑', '黑体', '楷体'],
fontIndex: 0,
colorList: ['#000000', '#ffffff', '#ff0000', '#00ff00', '#0000ff'],
colorIndex: 0,
fontSize: 20,
canvasWidth: 200,
canvasHeight: 200
},
onLoad: function () {
// 获取用户头像
wx.getUserInfo({
success: res => {
this.setData({
avatarUrl: res.userInfo.avatarUrl
})
}
})
},
onInput: function (e) {
// 监听输入框输入
this.setData({
text: e.detail.value
})
},
onFontChange: function (e) {
// 监听字体选择器变化
this.setData({
fontIndex: e.detail.value
})
},
onColorChange: function (e) {
// 监听颜色选择器变化
this.setData({
colorIndex: e.detail.value
})
},
onSizeChange: function (e) {
// 监听字体大小滑块变化
this.setData({
fontSize: e.detail.value
})
},
onGenerate: function () {
// 生成头像
const ctx = wx.createCanvasContext('avatar-canvas', this)
const avatarUrl = this.data.avatarUrl
const text = this.data.text
const font = this.data.fontList[this.data.fontIndex]
const color = this.data.colorList[this.data.colorIndex]
const fontSize = this.data.fontSize
const canvasWidth = this.data.canvasWidth
const canvasHeight = this.data.canvasHeight
// 绘制头像
ctx.drawImage(avatarUrl, 0, 0, canvasWidth, canvasHeight)
// 绘制文字
ctx.setFontSize(fontSize)
ctx.setFillStyle(color)
ctx.setTextAlign('center')
ctx.setTextBaseline('middle')
ctx.fillText(text, canvasWidth / 2, canvasHeight / 2)
// 保存图片
ctx.draw(false, () => {
wx.canvasToTempFilePath({
canvasId: 'avatar-canvas',
success: res => {
wx.previewImage({
urls: [res.tempFilePath]
})
}
}, this)
})
}
})
代码实现了用户上传头像后可以加入文字,并且可以调整文字位置,大小,颜色,选择字体生成头像的功能。用户可以在小程序中输入文字、选择字体、选择颜色、调整字体大小,然后点击生成头像按钮,即可生成带有文字的头像。