dev
xiaowen 3 years ago
parent bf4d3eb79a
commit 9df968c2cd

@ -22,60 +22,63 @@ Component({
value: [], value: [],
}, },
lvl: { lvl: {
type: [Number, String], type: [Number, String],
value: 18 value: 18
}, },
color: { color: {
type: String, type: String,
value: 'random-dark' value: 'random-dark'
}, },
canvasId: { canvasId: {
type: String, type: String,
value: 'myCanvas' value: 'myCanvas'
} }
}, },
lifetimes: { lifetimes: {
attached: function() { attached: function () {
// 在组件实例进入页面节点树时执行 // 在组件实例进入页面节点树时执行
const query = this.createSelectorQuery() const query = this.createSelectorQuery()
query.select('#' + this.data.canvasId) query.select('#' + this.data.canvasId)
.fields({ node: true, size: true }) .fields({
node: true,
size: true
})
.exec((res) => { .exec((res) => {
this.setData({ this.setData({
canvas: res[0].node canvas: res[0].node
}) })
// this.data.canvas = res[0].node // this.data.canvas = res[0].node
}) })
}, },
}, },
observers: { observers: {
list(newVal) { list(newVal) {
if(!newVal || !Array.isArray(newVal) || newVal.length === 0) return if (!newVal || !Array.isArray(newVal) || newVal.length === 0) return
let { width, height, list, wordData, color } = this.data let {
width,
height,
list,
wordData,
color
} = this.data
list = newVal; list = newVal;
if (!width || isNaN(Number(width))) {
width = 375
}
if (!height || isNaN(Number(height))) {
height = 450
}
const dpr = wx.getSystemInfoSync().pixelRatio const dpr = wx.getSystemInfoSync().pixelRatio
this.setData({ this.setData({
options: { options: {
"list": this.doData(list), "list": this.doData(list),
"gridSize": 10, // size of the grid in pixels "gridSize": 10, // size of the grid in pixels
"weightFactor": 4, // number to multiply for size of each word in the list "weightFactor": 4, // number to multiply for size of each word in the list
"fontWeight": 'normal', // 'normal', 'bold' or a callback "fontWeight": 'normal', // 'normal', 'bold' or a callback
"fontFamily": 'Times, serif', // font to use "fontFamily": 'Times, serif', // font to use
"color": color, // 'random-dark' or 'random-light' "color": color, // 'random-dark' or 'random-light'
"backgroundColor": 'transparent', // the color of canvas "backgroundColor": 'transparent', // the color of canvas
"rotateRatio": 0.2, // probability for the word to rotate. 1 means always "rotateRatio": 0.2, // probability for the word to rotate. 1 means always
"relativeScaling": 0.1, "relativeScaling": 0.1,
"height": height * dpr, "height": height * dpr,
"width": width * dpr "width": 375 * dpr
} }
}) })
setTimeout(()=>{ setTimeout(() => {
if (this.data.canvas) { if (this.data.canvas) {
const wordCloud = new WordCloud(this.data.canvas, this.data.options) const wordCloud = new WordCloud(this.data.canvas, this.data.options)
wordData = wordCloud.start() wordData = wordCloud.start()
@ -95,15 +98,15 @@ Component({
wordData: [], wordData: [],
canvas: null, canvas: null,
options: { options: {
"list": [], "list": [],
"gridSize": 10, // size of the grid in pixels "gridSize": 10, // size of the grid in pixels
"weightFactor": 4, // number to multiply for size of each word in the list "weightFactor": 4, // number to multiply for size of each word in the list
"fontWeight": 'normal', // 'normal', 'bold' or a callback "fontWeight": 'normal', // 'normal', 'bold' or a callback
"fontFamily": 'Times, serif', // font to use "fontFamily": 'Times, serif', // font to use
"color": 'random-dark', // 'random-dark' or 'random-light' "color": 'random-dark', // 'random-dark' or 'random-light'
"backgroundColor": 'transparent', // the color of canvas "backgroundColor": 'transparent', // the color of canvas
"rotateRatio": 0.2, // probability for the word to rotate. 1 means always "rotateRatio": 0.2, // probability for the word to rotate. 1 means always
"relativeScaling": 0.1 "relativeScaling": 0.1
} }
}, },
@ -112,18 +115,23 @@ Component({
*/ */
methods: { methods: {
bindWord(event) { bindWord(event) {
const { detail } = event.currentTarget.dataset const {
detail
} = event.currentTarget.dataset
this.triggerEvent('detail', detail); this.triggerEvent('detail', detail);
}, },
doData(list = []) { doData(list = []) {
if(list.length === 0) { if (list.length === 0) {
return [] return []
} }
let arr = []; let arr = [];
let max = this.data.lvl;
let maxVal = list[0][1]; let maxVal = list[0][1];
let l = list.length;
let x = l / (375 * this.data.height)
let max = x * maxVal;
let y = 23 / max;
list.forEach(ele => { list.forEach(ele => {
let a = [ele[0], ele[1] / maxVal * max]; let a = [ele[0], ele[1] * x * y];
arr.push(a) arr.push(a)
}) })
return arr; return arr;

@ -6,7 +6,7 @@
.wc-canvas { .wc-canvas {
display: none; display: none;
position: absolute; position: absolute;
width: 100%; width: 375px;
height: 100%; height: 100%;
z-index: -1; z-index: -1;
} }

Loading…
Cancel
Save