This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrawing.js
138 lines (129 loc) · 3.15 KB
/
drawing.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
138
const { Clutter, GObject, GLib, PangoCairo, Pango } = imports.gi;
const Cairo = imports.cairo;
function draw_rotated_line(ctx, color, width, angle, len, offset) {
offset = offset || 0;
ctx.save();
ctx.rotate(angle);
set_color(ctx, color, 1);
ctx.setLineWidth(width);
ctx.moveTo(0, offset);
ctx.lineTo(0, len);
ctx.stroke();
ctx.restore();
}
function draw_line(ctx, color, width, x, y, x2, y2) {
ctx.save();
set_color(ctx, color, 1);
ctx.setLineWidth(width);
ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.restore();
}
function draw_circle(ctx, color, x, y, diameter, line_width) {
ctx.save();
set_color(ctx, color, 1);
ctx.arc(x, y, diameter / 2 - diameter / 20, 0, 2 * Math.PI);
ctx.setLineWidth(line_width || 0);
if (line_width > 0) {
ctx.stroke();
} else {
ctx.fill();
}
ctx.restore();
}
function draw_rounded_rect(
ctx,
color,
x,
y,
h_size,
v_size,
line_width,
border_radius
) {
ctx.save();
set_color(ctx, color, 1);
ctx.translate(x, y);
ctx.setLineWidth(line_width || 0);
ctx.moveTo(border_radius, 0);
ctx.lineTo(h_size - border_radius, 0);
// ctx.lineTo(h_size, border_radius);
ctx.curveTo(h_size - border_radius, 0, h_size, 0, h_size, border_radius);
ctx.lineTo(h_size, v_size - border_radius);
// ctx.lineTo(h_size - border_radius, h_size);
ctx.curveTo(
h_size,
v_size - border_radius,
h_size,
v_size,
h_size - border_radius,
v_size
);
ctx.lineTo(border_radius, v_size);
// ctx.lineTo(0, h_size - border_radius);
ctx.curveTo(border_radius, v_size, 0, v_size, 0, v_size - border_radius);
ctx.lineTo(0, border_radius);
ctx.curveTo(0, border_radius, 0, 0, border_radius, 0);
if (line_width == 0) {
ctx.fill();
} else {
ctx.stroke();
}
ctx.restore();
}
function draw_rect(ctx, color, x, y, h_size, v_size, line_width) {
ctx.save();
set_color(ctx, color, 1);
ctx.translate(x, y);
ctx.setLineWidth(line_width || 0);
ctx.moveTo(0, 0);
ctx.lineTo(h_size, 0);
ctx.lineTo(h_size, v_size);
ctx.lineTo(0, v_size);
ctx.lineTo(0, 0);
if (line_width == 0) {
ctx.fill();
} else {
ctx.stroke();
}
ctx.restore();
}
function draw_text(ctx, showtext, font = 'DejaVuSans 42') {
ctx.save();
let pl = PangoCairo.create_layout(ctx);
pl.set_text(showtext, -1);
pl.set_font_description(Pango.FontDescription.from_string(font));
PangoCairo.update_layout(ctx, pl);
let [w, h] = pl.get_pixel_size();
ctx.relMoveTo(-w / 2, -h / 2);
PangoCairo.show_layout(ctx, pl);
ctx.relMoveTo(w / 2, 0);
ctx.restore();
return [w, h];
}
function set_color(ctx, clr, alpha) {
if (typeof clr === 'string') {
const [, cc] = Clutter.Color.from_string(clr);
ctx.setSourceRGBA(cc.red, cc.green, cc.blue, alpha);
} else {
if (clr.red) {
ctx.setSourceRGBA(clr.red, clr.green, clr.blue, alpha);
} else {
ctx.setSourceRGBA(clr[0], clr[1], clr[2], alpha);
}
}
}
function set_color_rgba(ctx, red, green, blue, alpha) {
ctx.setSourceRGBA(red, green, blue, alpha);
}
var Drawing = {
set_color,
set_color_rgba,
draw_rotated_line,
draw_line,
draw_circle,
draw_rect,
draw_rounded_rect,
draw_text,
};