forked from w3c/css-houdini-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
100 lines (87 loc) · 2.47 KB
/
chat.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
registerPaint('chat', class {
static get inputProperties() { return ['background-image', '--chat-images-num']; }
constructor() {
this.radii = [
0.5,
1 / (2 + 2/Math.SQRT2),
0.25,
0.25,
1 / Math.SQRT2 - 0.5,
];
const fudge = 0.03349;
this.positions = [
[
{x: 0.5, y: 0.5},
],
[
{x: this.radii[1], y: this.radii[1]},
{x: 1-this.radii[1], y: 1-this.radii[1]},
],
[
{x: 0.5, y: 0.25 + fudge},
{x: 0.75, y: 0.75 - fudge},
{x: 0.25, y: 0.75 - fudge},
],
[
{x: 0.25, y: 0.25},
{x: 0.75, y: 0.25},
{x: 0.25, y: 0.75},
{x: 0.75, y: 0.75},
],
[
{x: this.radii[4], y: this.radii[4]},
{x: 1-this.radii[4], y: this.radii[4]},
{x: 0.5, y: 0.5},
{x: 1-this.radii[4], y: 1-this.radii[4]},
{x: this.radii[4], y: 1-this.radii[4]},
],
];
this.colors = [
'#E91E63',
'#9C27B0',
'#2196F3',
'#8BC34A',
'#FF9800',
];
}
paint(ctx, geom, styleMap) {
ctx.fillStyle = '#FFF';
ctx.fillRect(0, 0, geom.width, geom.height);
// Chrome can only use background-images as source images at the moment.
const images = styleMap.getAll('background-image');
const num = styleMap.get('--chat-images-num').value;
const num_low = Math.floor(num);
const num_high = Math.ceil(num);
const dist = (num - num_low);
const size = Math.min(geom.width, geom.height);
const r_low = this.radii[num_low-1];
const r_high = this.radii[num_high-1];
const pos_low = this.positions[num_low-1];
const pos_high = this.positions[num_high-1];
for (let i = 0; i < num_high; i++) {
if (!pos_high) break;
const low = pos_low && pos_low[i];
const high = pos_high[i];
let x, y, r;
if (num_low != num_high && i == num_high - 1) {
x = size * high.x;
y = size * high.y;
r = size * dist * r_high;
} else {
x = size * ((1-dist) * low.x + dist * high.x);
y = size * ((1-dist) * low.y + dist * high.y);
r = size * ((1-dist) * r_low + dist * r_high);
}
ctx.fillStyle = this.colors[i];
ctx.beginPath();
ctx.arc(x, y, 0.95 * r, 0, Math.PI*2);
ctx.fill();
ctx.save();
ctx.beginPath();
ctx.arc(x, y, 0.9 * r, 0, Math.PI * 2);
ctx.clip();
ctx.drawImage(images[i], x - r, y - r, 2*r, 2*r);
ctx.restore();
}
}
});