微信小程序输出几个关键字生成云词代码,需要漂亮的UI

代码使用了WordCloud库来生成云词,需要先在小程序中引入该库。同时,需要将漂流瓶图片放在/images/bottle.png路径下。在漂流瓶页面点击按钮后,跳转到云词页面,并将关键字作为参数传递过去。在云词页面中,根据传递过来的关键字生成云词,并将生成的云词图片保存在wordcloudUrl变量中,用于在页面中显示。

<!-- 云词页面 -->
<view class="container">
  <view class="bottle">
    <image src="/images/bottle.png" class="bottle-img"></image>
    <view class="bottle-text">点击输入关键字生成云词</view>
  </view>
  <button class="btn" bindtap="onTapBottle">开始生成</button>
</view>

<!-- 云词页面 -->
<view class="container">
  <view class="wordcloud">
    <image src="{{wordcloudUrl}}" class="wordcloud-img"></image>
  </view>
</view>
复制// 漂流瓶页面逻辑
Page({
  onTapBottle: function() {
    wx.navigateTo({
      url: '/pages/wordcloud/wordcloud',
    })
  }
})

// 云词页面逻辑
Page({
  data: {
    wordcloudUrl: ''
  },
  onLoad: function(options) {
    // 获取关键字
    var keywords = options.keywords.split(',')
    // 生成云词
    var wordcloud = new window.WordCloud(document.querySelector('.wordcloud-img'), {
      list: keywords,
      fontFamily: 'Microsoft YaHei',
      fontWeight: 'bold',
      color: '#333',
      backgroundColor: '#fff',
      gridSize: Math.round(16 * document.querySelector('.wordcloud-img').width / 1024),
      weightFactor: function (size) {
        return Math.pow(size, 2.3) * document.querySelector('.wordcloud-img').width / 1024
      }
    })
    // 保存云词图片
    this.setData({
      wordcloudUrl: wordcloud.toDataURL()
    })
  }
})
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.bottle {
  position: relative;
  width: 200px;
  height: 200px;
  margin-bottom: 50px;
}

.bottle-img {
  width: 100%;
  height: 100%;
}

.bottle-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
  color: #fff;
  text-align: center;
}

.btn {
  width: 200px;
  height: 40px;
  background-color: #007aff;
  color: #fff;
  border-radius: 20px;
}

.wordcloud {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.wordcloud-img {
  max-width: 100%;
  max-height: 100%;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

果果科技

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

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

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

打赏作者

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

抵扣说明:

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

余额充值