You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2 KiB

3 years ago
// components/word-cloud/index.js
3 years ago
import WordCloud from './wordcloud'
3 years ago
const options = {
"list": [],
3 years ago
"gridSize": 2, // size of the grid in pixels
"weightFactor": 8, // number to multiply for size of each word in the list
3 years ago
"fontWeight": 'normal', // 'normal', 'bold' or a callback
"fontFamily": 'Times, serif', // font to use
3 years ago
"color": '#FF1C20', // 'random-dark' or 'random-light'
"backgroundColor": 'transparent', // the color of canvas
"rotateRatio": 0.2, // probability for the word to rotate. 1 means always
3 years ago
}
3 years ago
Component({
/**
* 组件的属性列表
*/
properties: {
3 years ago
width: {
type: Number,
value: 375
3 years ago
},
height: {
3 years ago
type: Number,
value: 450
3 years ago
},
3 years ago
bgColor: {
3 years ago
type: String,
3 years ago
value: 'transparent',
3 years ago
},
3 years ago
list: {
type: Array,
value: [],
}
},
lifetimes: {
attached: function() {
// 在组件实例进入页面节点树时执行
const query = this.createSelectorQuery()
3 years ago
query.select('.wc-canvas')
3 years ago
.fields({ node: true, size: true })
.exec((res) => {
this.data.canvas = res[0].node
})
3 years ago
},
},
3 years ago
observers: {
list(newVal) {
if(!newVal || !Array.isArray(newVal) || newVal.length === 0) return
let { width, height, list, wordData } = this.data
list = newVal;
if (!width || isNaN(Number(width))) {
width = 375
}
if (!height || isNaN(Number(height))) {
height = 450
}
const dpr = wx.getSystemInfoSync().pixelRatio
options.height = height * dpr
options.width = width * dpr
options.list = list
setTimeout(()=>{
if (this.data.canvas) {
const wordCloud = new WordCloud(this.data.canvas, options)
wordData = wordCloud.start()
3 years ago
console.log(wordData)
3 years ago
this.setData({
wordData,
width,
height,
})
}
}, 500)
}
},
/**
* 组件的初始数据
*/
3 years ago
data: {
3 years ago
wordData: [],
canvas: null,
3 years ago
},
3 years ago
/**
* 组件的方法列表
*/
methods: {
bindWord(event) {
const { detail } = event.currentTarget.dataset
this.triggerEvent('detail', detail);
},
3 years ago
}
3 years ago
})