Skip to content

Commit b77a363

Browse files
authored
Smooth color transition animation (#107)
1 parent 54fd161 commit b77a363

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/js/app/components/robot.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,37 @@ export default class Robot {
2626
}
2727

2828
changeColor(id, R, G, B, ambient, callback) {
29-
var r = this.scene.getObjectByName(ROBOT_PREFIX + id);
30-
if (r != undefined) {
31-
r.material.color.setRGB(R / 256, G / 256, B / 265);
32-
//console.log("Color> id:", id, " | R:", R, "G:", G, "B:", B);
33-
34-
if (callback != null) callback('success');
29+
var robot = this.scene.getObjectByName(ROBOT_PREFIX + id);
30+
if (robot != undefined) {
31+
var i = 0;
32+
const startColor = {
33+
r: robot.material.color.r,
34+
g: robot.material.color.g,
35+
b: robot.material.color.b
36+
};
37+
const endColor = {
38+
r: R / 256,
39+
g: G / 256,
40+
b: B / 256
41+
};
42+
const steps = 10;
43+
const interval = 500;
44+
45+
const intervalId = setInterval(function () {
46+
const pos = i / steps;
47+
robot.material.color.setRGB(
48+
(1 - pos) * startColor.r + pos * endColor.r,
49+
(1 - pos) * startColor.g + pos * endColor.g,
50+
(1 - pos) * startColor.b + pos * endColor.b
51+
);
52+
i++;
53+
if (i === steps) {
54+
clearInterval(intervalId);
55+
if (callback != null) callback('success');
56+
}
57+
}, interval / steps);
3558
} else if (callback != null) callback('undefined');
36-
37-
return r;
59+
return robot;
3860
}
3961

4062
create(id, x, y, heading, reality = 'V', callback) {

src/js/app/managers/mqttClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export default class MQTTClient {
177177
}
178178
} else {
179179
// reality not matching; remove
180-
robot.delete(id);
180+
window.robot.delete(id);
181181
}
182182
}
183183
}

0 commit comments

Comments
 (0)