Skip to content

Commit 4e924f8

Browse files
authored
c-eyes@anaximeno: Version 3.0.1 (#6466)
* Check if optimization available before using it * Updated eye drawing process * Optimize the should redraw check function * Refactor clear area process
1 parent 796b5ac commit 4e924f8

File tree

3 files changed

+43
-44
lines changed

3 files changed

+43
-44
lines changed

c-eyes@anaximeno/files/c-eyes@anaximeno/6.2/applet.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Eye extends Applet.Applet {
7878

7979
this.refresh_handler_id = 0;
8080

81+
this.cos_repaint_angle = Math.cos(this.repaint_angle);
82+
8183
this._last_mouse_x = undefined;
8284
this._last_mouse_y = undefined;
8385
this._last_eye_x = undefined;
@@ -91,7 +93,7 @@ class Eye extends Applet.Applet {
9193
get repaint_interval() {
9294
let repaint_interval = this._repaint_interval;
9395

94-
if (this.optimization_mode != "manual") {
96+
if (this.optimization_mode != "manual" && this.optimization_mode in Optimizations) {
9597
let r = Optimizations[this.optimization_mode]["repaint_interval_ms"];
9698
if (r != null || r != undefined) repaint_interval = r;
9799
}
@@ -102,7 +104,7 @@ class Eye extends Applet.Applet {
102104
get repaint_angle() {
103105
let repaint_angle = this._repaint_angle;
104106

105-
if (this.optimization_mode != "manual") {
107+
if (this.optimization_mode != "manual" && this.optimization_mode in Optimizations) {
106108
let r = Optimizations[this.optimization_mode]["repaint_angle_rad"];
107109
if (r != null || r != undefined) repaint_angle = r;
108110
}
@@ -122,7 +124,7 @@ class Eye extends Applet.Applet {
122124
{
123125
key: "repaint-angle",
124126
value: "_repaint_angle",
125-
cb: null,
127+
cb: () => this.cos_repaint_angle = Math.cos(this.repaint_angle),
126128
},
127129
{
128130
key: "mode",
@@ -398,17 +400,18 @@ class Eye extends Applet.Applet {
398400
) {
399401
should_redraw = true;
400402
} else {
401-
const dist = (x, y) => Math.sqrt(x * x + y * y);
403+
const sq_dist = (x, y) => x * x + y * y;
402404
const [last_x, last_y] = [this._last_mouse_x - ox, this._last_mouse_y - oy];
403405
const [current_x, current_y] = [mouse_x - ox, mouse_y - oy];
404-
const dist_prod = dist(last_x, last_y) * dist(current_x, current_y);
406+
const current_sq_dist = sq_dist(current_x, current_y);
407+
const last_sq_dist = sq_dist(last_x, last_y);
405408

406-
if (dist_prod == 0) {
409+
if (last_sq_dist === 0 || current_sq_dist === 0) {
407410
should_redraw = true;
408411
} else {
409412
const dot_prod = current_x * last_x + current_y * last_y;
410-
const angle = Math.acos(dot_prod / dist_prod);
411-
should_redraw = angle >= this.repaint_angle;
413+
const cos_angle = dot_prod / Math.sqrt(last_sq_dist * current_sq_dist);
414+
should_redraw = cos_angle <= this.cos_repaint_angle;
412415
}
413416
}
414417

c-eyes@anaximeno/files/c-eyes@anaximeno/6.2/eyeModes.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class EyeMode {
3636
topAndLatSizes(area_width, area_height, options) {
3737
let top_size, lat_size;
3838

39-
if(options.is_vertical) {
39+
if (options.is_vertical) {
4040
top_size = area_width;
4141
lat_size = area_height;
4242
} else {
@@ -46,41 +46,45 @@ class EyeMode {
4646

4747
return [top_size, lat_size];
4848
}
49+
50+
/** Add a ransparent background to avoid blemishes from previous drawings. */
51+
clearArea(cr, area_width, area_height) {
52+
cr.save();
53+
cr.setSourceRGBA(0, 0, 0, 0);
54+
cr.rectangle(0, 0, area_width, area_height);
55+
cr.fill();
56+
cr.restore();
57+
}
4958
}
5059

5160
class EyelidMode extends EyeMode {
5261
drawEye(area, options) {
53-
let [area_width, area_height] = area.get_surface_size();
54-
let [mouse_x, mouse_y] = [options.mouse_x, options.mouse_y];
55-
let [area_x, area_y] = [options.area_x, options.area_y];
56-
57-
area_x += area_width / 2;
58-
area_y += area_height / 2;
59-
60-
mouse_x -= area_x;
61-
mouse_y -= area_y;
62+
const [area_width, area_height] = area.allocation.get_size();
63+
const mouse_x = options.mouse_x - options.area_x - area_width / 2;
64+
const mouse_y = options.mouse_y - options.area_y - area_height / 2;
6265

6366
const mouse_ang = Math.atan2(mouse_y, mouse_x);
6467
let mouse_rad = Math.sqrt(mouse_x * mouse_x + mouse_y * mouse_y);
6568

66-
let [top_size, lat_size] = this.topAndLatSizes(area_width, area_height, options);
67-
let eye_rad = (top_size - options.padding) / 2;
68-
if (2 * eye_rad > lat_size) eye_rad = lat_size / 2;
69+
const [top_size, lat_size] = this.topAndLatSizes(area_width, area_height, options);
70+
let eye_rad = Math.min(top_size - options.padding, lat_size) / 2;
6971

7072
const iris_rad = eye_rad * 0.5;
7173
const pupil_rad = iris_rad * 0.4;
7274

7375
const max_rad = eye_rad * (Math.pow(Math.cos(mouse_ang), 4) * 0.5 + 0.25);
7476

75-
if (mouse_rad > max_rad)
76-
mouse_rad = max_rad;
77+
mouse_rad = Math.min(mouse_rad, max_rad);
7778

7879
const iris_arc = Math.asin(iris_rad / eye_rad);
7980
const iris_r = eye_rad * Math.cos(iris_arc);
8081

8182
const eye_ang = Math.atan(mouse_rad / iris_r);
8283

8384
let cr = area.get_context();
85+
this.clearArea(cr, area_width, area_height);
86+
87+
cr.save();
8488

8589
// -- Drawing the base of the eye
8690

@@ -91,12 +95,12 @@ class EyelidMode extends EyeMode {
9195

9296
const x_def = iris_rad * Math.cos(mouse_ang) * (Math.sin(eye_ang));
9397
const y_def = iris_rad * Math.sin(mouse_ang) * (Math.sin(eye_ang));
94-
let amp;
9598

9699
const top_lid = 0.8;
97100
const bottom_lid = 0.6;
98101

99-
amp = eye_rad * top_lid;
102+
let amp = eye_rad * top_lid;
103+
100104
cr.moveTo(-eye_rad, 0);
101105
cr.curveTo(x_def - iris_rad, y_def + amp,
102106
x_def + iris_rad, y_def + amp, eye_rad, 0);
@@ -142,45 +146,39 @@ class EyelidMode extends EyeMode {
142146
cr.arc(0, 0, 1.0, 0, 2 * Math.PI);
143147
cr.fill();
144148

145-
cr.save();
146149
cr.restore();
147-
cr.$dispose();
148150
}
149151
}
150152

151153

152154
class BulbMode extends EyeMode {
153155
drawEye(area, options) {
154-
let [area_width, area_height] = area.get_surface_size();
155-
let [mouse_x, mouse_y] = [options.mouse_x, options.mouse_y];
156-
let [area_x, area_y] = [options.area_x, options.area_y];
157-
158-
area_x += area_width / 2;
159-
area_y += area_height / 2;
160-
161-
mouse_x -= area_x;
162-
mouse_y -= area_y;
156+
const [area_width, area_height] = area.allocation.get_size();
157+
const mouse_x = options.mouse_x - options.area_x - area_width / 2;
158+
const mouse_y = options.mouse_y - options.area_y - area_height / 2;
163159

164160
let mouse_rad = Math.sqrt(mouse_x * mouse_x + mouse_y * mouse_y);
165161
const mouse_ang = Math.atan2(mouse_y, mouse_x);
166162

167-
let [top_size, lat_size] = this.topAndLatSizes(area_width, area_height, options);
168-
let eye_rad = (top_size - options.padding) / 2;
169-
if (2 * eye_rad > lat_size) eye_rad = lat_size / 2.1;
163+
const [top_size, lat_size] = this.topAndLatSizes(area_width, area_height, options);
164+
let eye_rad = Math.min(top_size - options.padding, lat_size) / 2;
170165

171166
const iris_rad = eye_rad * 0.6;
172167
const pupil_rad = iris_rad * 0.4;
173168

174169
const max_rad = eye_rad * Math.cos(Math.asin((iris_rad) / eye_rad)) - options.line_width;
175170

176-
if (mouse_rad > max_rad) mouse_rad = max_rad;
171+
mouse_rad = Math.min(mouse_rad, max_rad);
177172

178173
const iris_arc = Math.asin(iris_rad / eye_rad);
179174
const iris_r = eye_rad * Math.cos(iris_arc);
180175

181176
const eye_ang = Math.atan(mouse_rad / iris_r);
182177

183-
const cr = area.get_context();
178+
let cr = area.get_context();
179+
this.clearArea(cr, area_width, area_height);
180+
181+
cr.save();
184182

185183
// -- Drawing the base of the eye
186184

@@ -217,9 +215,7 @@ class BulbMode extends EyeMode {
217215
cr.arc(0, 0, 1.0, 0, 2 * Math.PI);
218216
cr.fill();
219217

220-
cr.save();
221218
cr.restore();
222-
cr.$dispose();
223219
}
224220
}
225221

c-eyes@anaximeno/files/c-eyes@anaximeno/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.0",
2+
"version": "3.0.1",
33
"uuid": "c-eyes@anaximeno",
44
"name": "Cinnamon Eyes",
55
"multiversion": true,

0 commit comments

Comments
 (0)