用户上传头像后可以加入文字,并且可以调整文字位置,大小,颜色,选择字体生成头像的微信小程序代码

<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)
    })
  }
})

代码实现了用户上传头像后可以加入文字,并且可以调整文字位置,大小,颜色,选择字体生成头像的功能。用户可以在小程序中输入文字、选择字体、选择颜色、调整字体大小,然后点击生成头像按钮,即可生成带有文字的头像。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

果果科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值