-
Notifications
You must be signed in to change notification settings - Fork 107
/
hover.js
137 lines (121 loc) · 4.48 KB
/
hover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// create element
function newlabel(el){
var newDiv = document.createElement("div");
var newSpan = document.createElement("span");
var id = el.id
newDiv.id = id + "wcLabel";
newDiv.className += "wcLabel";
newSpan.id = id + "wcSpan";
newSpan.className += "wcSpan";
el.appendChild(newDiv);
document.getElementById(id + "wcLabel").appendChild(newSpan);
}
// hover function
function cv_handleHover(item, dimension, evt) {
var path = evt.path || (evt.composedPath && evt.composedPath());
var id = path[1].id
var el = document.getElementById(id + "wcLabel");
var target = evt.target || evt.srcElement;
if (!item) {
el.setAttribute('hidden', true);
return;
}
el.removeAttribute('hidden');
el.style.left = dimension.x + target.offsetLeft + 'px';
el.style.top = dimension.y + target.offsetTop + 'px';
el.style.width = dimension.w + 'px';
el.style.height = dimension.h + 'px';
this.hoverDimension = dimension;
document.getElementById(id + "wcSpan").setAttribute(
'data-l10n-args', JSON.stringify({ word: item[0], count: item[1] }));
document.getElementById(id + "wcSpan").innerHTML =item[0]+":" + item[1];
}
//mask function
function maskInit(el,x){
str = x.figBase64;
var newImg = new Image();
newImg.src = str;
newImg.style.position = 'absolute';
newImg.style.left = 0;
newImg.width = el.clientWidth;
newImg.height = el.clientHeight;
// maskCanvas = init(el, x, newImg);
vvalue = 128
var maskCanvas = document.createElement('canvas');
maskCanvas.width = newImg.width;
maskCanvas.height = newImg.height;
var ctx = maskCanvas.getContext('2d');
ctx.drawImage(newImg, 0, 0, newImg.width, newImg.height);
var imageData = ctx.getImageData(0, 0, maskCanvas.width, maskCanvas.height);
var newImageData = ctx.createImageData(imageData);
// M = 0
for (var i = 0; i < imageData.data.length; i += 4) {
var tone = imageData.data[i] +
imageData.data[i + 1] +
imageData.data[i + 2];
var alpha = imageData.data[i + 3];
if (alpha < vvalue || tone > vvalue * 3) {
// Area not to draw
newImageData.data[i] =
newImageData.data[i + 1] =
newImageData.data[i + 2] = 255;
newImageData.data[i + 3] = 0;
} else {
// Area to draw
newImageData.data[i] =
newImageData.data[i + 1] =
newImageData.data[i + 2] = 0;
newImageData.data[i + 3] = 255;
}
}
ctx.putImageData(newImageData, 0, 0);
//mask(el, x, maskCanvas);
var bctx = document.createElement('canvas').getContext('2d');
bctx.fillStyle = x.backgroundColor || '#fff';
bctx.fillRect(0, 0, 1, 1);
var bgPixel = bctx.getImageData(0, 0, 1, 1).data;
var maskCanvasScaled = document.createElement('canvas');
maskCanvasScaled.width = el.clientWidth;
maskCanvasScaled.height = el.clientHeight;
ctx = maskCanvasScaled.getContext('2d');
ctx.drawImage(maskCanvas,
0, 0, maskCanvas.width, maskCanvas.height);
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
newImageData = ctx.createImageData(imageData);
for (var j = 0; j < imageData.data.length; j += 4) {
if (imageData.data[j + 3] > vvalue) {
newImageData.data[j] = bgPixel[0];
newImageData.data[j + 1] = bgPixel[1];
newImageData.data[j + 2] = bgPixel[2];
newImageData.data[j + 3] = bgPixel[3];
} else {
// This color must not be the same w/ the bgPixel.
newImageData.data[j] = bgPixel[0];
newImageData.data[j + 1] = bgPixel[1];
newImageData.data[j + 2] = bgPixel[2];
newImageData.data[j + 3] = bgPixel[3] ? (bgPixel[3] - 1) : 0;
}
}
ctx.putImageData(newImageData, 0, 0);
ctx = el.firstChild.getContext('2d');
ctx.drawImage(maskCanvasScaled, 0, 0);
maskCanvasScaled = ctx = imageData = newImageData = bctx = bgPixel = undefined;
WordCloud(el.firstChild, { list: listData,
fontFamily: x.fontFamily,
fontWeight: x.fontWeight,
color: x.color,
minSize: x.minSize,
weightFactor: x.weightFactor,
backgroundColor: x.backgroundColor,
gridSize: x.gridSize,
minRotation: x.minRotation,
maxRotation: x.maxRotation,
shuffle: x.shuffle,
shape: x.shape,
rotateRatio: x.rotateRatio,
ellipticity: x.ellipticity,
clearCanvas: false,
hover: x.hover || cv_handleHover,
abortThreshold: 3000
});
}