parent
42af666487
commit
cf1690ce2a
File diff suppressed because it is too large
Load Diff
@ -1,50 +1,98 @@
|
||||
// word-cloud/index.js
|
||||
const {
|
||||
WordCloud
|
||||
} = require("./WordCloud.js")
|
||||
// components/word-cloud/index.js
|
||||
import WordCloud from './wordcloud'
|
||||
const options = {
|
||||
"list": [],
|
||||
"gridSize": 6, // size of the grid in pixels
|
||||
"weightFactor": 1.5, // number to multiply for size of each word in the list
|
||||
"fontWeight": 'normal', // 'normal', 'bold' or a callback
|
||||
"fontFamily": 'Times, serif', // font to use
|
||||
"color": 'red', // 'random-dark' or 'random-light'
|
||||
"backgroundColor": '#fff', // the color of canvas
|
||||
"rotateRatio": 0, // probability for the word to rotate. 1 means always
|
||||
"minFontSize": 12, //最小字号
|
||||
"maxFontSize": 60, //最大字号
|
||||
"fontSizeFactor": 5,
|
||||
"ellipticity": 1,
|
||||
"shuffle": false,
|
||||
"figPath": '../../images/carSide.png'
|
||||
}
|
||||
|
||||
Component({
|
||||
/**
|
||||
* 组件的属性列表
|
||||
*/
|
||||
properties: {
|
||||
list: {
|
||||
type: Array,
|
||||
value: [],
|
||||
width: {
|
||||
type: Number,
|
||||
value: 375
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
value: "200"
|
||||
type: Number,
|
||||
value: 450
|
||||
},
|
||||
width: {
|
||||
bgColor: {
|
||||
type: String,
|
||||
value: ""
|
||||
value: '#fff',
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
value: "random-light"
|
||||
list: {
|
||||
type: Array,
|
||||
value: [],
|
||||
}
|
||||
},
|
||||
lifetimes: {
|
||||
attached: function() {
|
||||
// 在组件实例进入页面节点树时执行
|
||||
const query = this.createSelectorQuery()
|
||||
query.select('#myCanvas')
|
||||
.fields({ node: true, size: true })
|
||||
.exec((res) => {
|
||||
this.data.canvas = res[0].node
|
||||
})
|
||||
},
|
||||
},
|
||||
|
||||
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()
|
||||
this.setData({
|
||||
wordData,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 组件的初始数据
|
||||
*/
|
||||
data: {
|
||||
spans: []
|
||||
wordData: [],
|
||||
canvas: null,
|
||||
},
|
||||
|
||||
ready() {
|
||||
let that = this;
|
||||
this.ctx = wx.createCanvasContext('canvas', this);
|
||||
let query = this.createSelectorQuery();
|
||||
query.select("#canvas").boundingClientRect(function (res) {
|
||||
WordCloud(that.ctx, res, {
|
||||
minSize: 8,
|
||||
list: that.data.list,
|
||||
color: that.data.color
|
||||
}, that, function (val) {
|
||||
that.data.spans.push(val)
|
||||
// w.to_file("../../images/carSide.png")
|
||||
that.setData({
|
||||
spans: that.data.spans
|
||||
})
|
||||
})
|
||||
}).exec();
|
||||
/**
|
||||
* 组件的方法列表
|
||||
*/
|
||||
methods: {
|
||||
bindWord(event) {
|
||||
const { detail } = event.currentTarget.dataset
|
||||
this.triggerEvent('detail', detail);
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
@ -1,8 +1,6 @@
|
||||
<view style="position:relative;width:100%;">
|
||||
<view style="position:absolute;width:100%;visibility:hidden;">
|
||||
<canvas style="width:100%;height: {{height}}px;" id="canvas" canvas-id="canvas"></canvas>
|
||||
</view>
|
||||
<view style="position:relative;">
|
||||
<view wx:for="{{spans}}" wx:key="{{index}}" style="position:absolute;left:{{((item.gx + item.info.gw / 2) * item.g + item.info.fillTextOffsetX)}}px;top:{{((item.gy + item.info.gh / 2) * item.g + item.info.fillTextOffsetY)}}px;width:{{item.info.fillTextWidth * 2}}px;height:{{item.info.fillTextHeight}}px;line-height:{{item.fontSize}}px;font-size:{{(item.fontSize * item.info.mu)}}px; transform:{{item.transformRule}};transformOrigin:50% 40%;color:{{item.color}}">{{item.word}}</view>
|
||||
</view>
|
||||
<canvas type="2d" id="myCanvas" class="wc-canvas"></canvas>
|
||||
<view class="wc-main" style="width: {{ width ? width + 'px' : '100%' }};height: {{ height }}px; background-color: {{bgColor || '#fff'}};">
|
||||
<block wx:for="{{wordData}}" wx:key="index">
|
||||
<text class="wc-item" style="top:{{item.top}}rpx;left:{{ item.left }}rpx; transform:{{item.transform}};lineHeight:{{item.fontSize}}rpx;font:{{item.font}};color:{{item.color}};" catchtap="bindWord" data-detail="{{item}}" >{{ item.word }} </text>
|
||||
</block>
|
||||
</view>
|
@ -1 +1,16 @@
|
||||
/* word-cloud/index.wxss */
|
||||
/* components/word-cloud/index.wxss */
|
||||
|
||||
.wc-canvas {
|
||||
display: none;
|
||||
position: absolute;
|
||||
z-index: -1;
|
||||
}
|
||||
.wc-main {
|
||||
position: relative;
|
||||
}
|
||||
.wc-item {
|
||||
white-space: nowrap;
|
||||
position: absolute;
|
||||
display: block;
|
||||
transform-origin: 50% 40%;
|
||||
}
|
@ -0,0 +1,247 @@
|
||||
function WordCloud(wdata, laySize) {
|
||||
|
||||
var cloudRadians = Math.PI / 180,
|
||||
cw = 1 << 11 >> 5,
|
||||
ch = 1 << 11;
|
||||
let data = wdata
|
||||
let words = wdata
|
||||
let canvas = cloudCanvas
|
||||
let random = Math.random
|
||||
let size = laySize
|
||||
let spiral = archimedeanSpiral
|
||||
|
||||
|
||||
step()
|
||||
console.log('===', words)
|
||||
|
||||
function zeroArray(n) {
|
||||
var a = [],
|
||||
i = -1;
|
||||
while (++i < n) a[i] = 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
function step() {
|
||||
var contextAndRatio = getContext(canvas()),
|
||||
board = zeroArray((size[0] >> 5) * size[1]),
|
||||
bounds = null,
|
||||
n = words.length,
|
||||
i = 0,
|
||||
tags = []
|
||||
for (i; i < n; i++) {
|
||||
var d = data[i];
|
||||
d.x = (size[0] * (random() + .5)) >> 1;
|
||||
d.y = (size[1] * (random() + .5)) >> 1;
|
||||
cloudSprite(contextAndRatio, d, data, i);
|
||||
if (d.hasText && place(board, d, bounds)) {
|
||||
tags.push(d);
|
||||
|
||||
if (bounds) cloudBounds(bounds, d);
|
||||
else bounds = [{ x: d.x + d.x0, y: d.y + d.y0 }, { x: d.x + d.x1, y: d.y + d.y1 }];
|
||||
// Temporary hack
|
||||
d.x -= size[0] >> 1;
|
||||
d.y -= size[1] >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cloudCanvas() {
|
||||
return document.createElement("canvas");
|
||||
}
|
||||
|
||||
function getContext(canvas) {
|
||||
canvas.width = canvas.height = 1;
|
||||
var ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2);
|
||||
canvas.width = (cw << 5) / ratio;
|
||||
canvas.height = ch / ratio;
|
||||
|
||||
var context = canvas.getContext("2d");
|
||||
context.fillStyle = context.strokeStyle = "red";
|
||||
context.textAlign = "center";
|
||||
|
||||
return { context: context, ratio: ratio };
|
||||
}
|
||||
|
||||
|
||||
function cloudSprite(contextAndRatio, d, data, di) {
|
||||
if (d.sprite) return;
|
||||
var c = contextAndRatio.context,
|
||||
ratio = contextAndRatio.ratio;
|
||||
|
||||
c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
|
||||
var x = 0,
|
||||
y = 0,
|
||||
maxh = 0,
|
||||
n = data.length;
|
||||
--di;
|
||||
while (++di < n) {
|
||||
d = data[di];
|
||||
c.save();
|
||||
|
||||
// c.font = ~~((d.size + 1) / ratio) + "px " + (d.font || "Arial");
|
||||
c.font = `${(((d.size) / ratio))}px ${(d.font || "Arial")}`
|
||||
var w = c.measureText(d.text + "m").width * ratio,
|
||||
h = d.size << 1;
|
||||
if (d.rotate) {
|
||||
var sr = Math.sin(d.rotate * cloudRadians),
|
||||
cr = Math.cos(d.rotate * cloudRadians),
|
||||
wcr = w * cr,
|
||||
wsr = w * sr,
|
||||
hcr = h * cr,
|
||||
hsr = h * sr;
|
||||
w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5;
|
||||
h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
|
||||
} else {
|
||||
w = (w + 0x1f) >> 5 << 5;
|
||||
}
|
||||
if (h > maxh) maxh = h;
|
||||
if (x + w >= (cw << 5)) {
|
||||
x = 0;
|
||||
y += maxh;
|
||||
maxh = 0;
|
||||
}
|
||||
if (y + h >= ch) break;
|
||||
c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
|
||||
if (d.rotate) c.rotate(d.rotate * cloudRadians);
|
||||
c.fillText(d.text, 0, 0);
|
||||
if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, 0, 0);
|
||||
c.restore();
|
||||
d.width = w;
|
||||
d.height = h;
|
||||
d.xoff = x;
|
||||
d.yoff = y;
|
||||
d.x1 = w >> 1;
|
||||
d.y1 = h >> 1;
|
||||
d.x0 = -d.x1;
|
||||
d.y0 = -d.y1;
|
||||
d.hasText = true;
|
||||
x += w;
|
||||
}
|
||||
var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data,
|
||||
sprite = [];
|
||||
while (--di >= 0) {
|
||||
d = data[di];
|
||||
if (!d.hasText) continue;
|
||||
var w = d.width,
|
||||
w32 = w >> 5,
|
||||
h = d.y1 - d.y0;
|
||||
// Zero the buffer
|
||||
for (var i = 0; i < h * w32; i++) sprite[i] = 0;
|
||||
x = d.xoff;
|
||||
if (x == null) return;
|
||||
y = d.yoff;
|
||||
var seen = 0,
|
||||
seenRow = -1;
|
||||
for (var j = 0; j < h; j++) {
|
||||
for (var i = 0; i < w; i++) {
|
||||
var k = w32 * j + (i >> 5),
|
||||
m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0;
|
||||
sprite[k] |= m;
|
||||
seen |= m;
|
||||
}
|
||||
if (seen) seenRow = j;
|
||||
else {
|
||||
d.y0++;
|
||||
h--;
|
||||
j--;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
d.y1 = d.y0 + seenRow;
|
||||
d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
|
||||
}
|
||||
}
|
||||
|
||||
function place(board, tag, bounds) {
|
||||
var perimeter = [{ x: 0, y: 0 }, { x: size[0], y: size[1] }],
|
||||
startX = tag.x,
|
||||
startY = tag.y,
|
||||
maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
|
||||
s = spiral(size),
|
||||
dt = random() < .5 ? 1 : -1,
|
||||
t = -dt,
|
||||
dxdy,
|
||||
dx,
|
||||
dy;
|
||||
|
||||
while (dxdy = s(t += dt)) {
|
||||
dx = ~~dxdy[0];
|
||||
dy = ~~dxdy[1];
|
||||
|
||||
if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break;
|
||||
|
||||
tag.x = startX + dx;
|
||||
tag.y = startY + dy;
|
||||
|
||||
if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
|
||||
tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
|
||||
// TODO only check for collisions within current bounds.
|
||||
if (!bounds || !cloudCollide(tag, board, size[0])) {
|
||||
if (!bounds || collideRects(tag, bounds)) {
|
||||
var sprite = tag.sprite,
|
||||
w = tag.width >> 5,
|
||||
sw = size[0] >> 5,
|
||||
lx = tag.x - (w << 4),
|
||||
sx = lx & 0x7f,
|
||||
msx = 32 - sx,
|
||||
h = tag.y1 - tag.y0,
|
||||
x = (tag.y + tag.y0) * sw + (lx >> 5),
|
||||
last;
|
||||
for (var j = 0; j < h; j++) {
|
||||
last = 0;
|
||||
for (var i = 0; i <= w; i++) {
|
||||
board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
|
||||
}
|
||||
x += sw;
|
||||
}
|
||||
delete tag.sprite;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function cloudCollide(tag, board, sw) {
|
||||
sw >>= 5;
|
||||
var sprite = tag.sprite,
|
||||
w = tag.width >> 5,
|
||||
lx = tag.x - (w << 4),
|
||||
sx = lx & 0x7f,
|
||||
msx = 32 - sx,
|
||||
h = tag.y1 - tag.y0,
|
||||
x = (tag.y + tag.y0) * sw + (lx >> 5),
|
||||
last;
|
||||
for (var j = 0; j < h; j++) {
|
||||
last = 0;
|
||||
for (var i = 0; i <= w; i++) {
|
||||
if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0))
|
||||
& board[x + i]) return true;
|
||||
}
|
||||
x += sw;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function collideRects(a, b) {
|
||||
return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y;
|
||||
}
|
||||
|
||||
function cloudBounds(bounds, d) {
|
||||
var b0 = bounds[0],
|
||||
b1 = bounds[1];
|
||||
if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0;
|
||||
if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0;
|
||||
if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1;
|
||||
if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1;
|
||||
}
|
||||
|
||||
function archimedeanSpiral(size) {
|
||||
var e = size[0] / size[1];
|
||||
return function (t) {
|
||||
return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)];
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue