-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathavatar-path-renderer.js
47 lines (38 loc) · 1.27 KB
/
avatar-path-renderer.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
// Renders the avatar's path through the maze
function AvatarPathRenderer(
avatarPathContext,
coordinates
) {
var positions = [];
return {
clearAvatarPath: clearAvatarPath,
addToAvatarPath: addToAvatarPath,
redraw: redraw
};
// redraw the whole path.
// this happens when display size changes or viewport scrolls
function redraw() {
CanvasUtils.resetCanvas(avatarPathContext, "rgba(0,0,0,0)");
for(var i = 0; i < positions.length - 1; i++) {
renderSegment(positions[i], positions[i + 1]);
}
}
function clearAvatarPath() {
positions = [];
CanvasUtils.resetCanvas(avatarPathContext, "rgba(0,0,0,0)");
}
// add a new segment. We don't redraw the whole path, just add to the avatar path canvas.
function addToAvatarPath(from, to) {
if (positions.length === 0) positions = [from];
positions.push(to);
renderSegment(from, to);
}
function renderSegment(from, to) {
var f = coordinates.canvasPosition(from);
var t = coordinates.canvasPosition(to);
// only if we can see it!
if (f.offscreen && t.offscreen) return;
var dashBasis = Math.ceil(f.size / 20);
CanvasUtils.drawPath(avatarPathContext, f.size / 8, Styles.avatarPath, [f.center, t.center], { dash: [dashBasis * 5, dashBasis * 5] });
}
}