Skip to content

Commit 8f561fc

Browse files
committed
optimize scaling factor of canvas animation
1 parent 3103b87 commit 8f561fc

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/view/menu/UpdatePace.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
canvas {
3535
border-radius: 50%;
3636
// downscaled
37-
width: 1.5rem;
38-
height: 1.5rem;
37+
width: 20px;
38+
height: 20px;
3939
}
4040
}
4141
</style>

src/view/menu/UpdatePaceTimeMap.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deg2rad, Point, Vector } from '../../api/canvas.ts';
1+
import { deg2rad, PI2, Point, Vector } from '../../api/canvas.ts';
22

33
interface IMemo {
44
when: number;
@@ -8,8 +8,10 @@ interface IMemo {
88

99
let raf = 0;
1010
let ctx: CanvasRenderingContext2D;
11-
const R = 40;
11+
const R = 20;
1212
const D = 2 * R;
13+
const LINE_WIDTH = 4;
14+
const SHADOW_WIDTH = 4;
1315
const pRotationAxis = new Point(R, R);
1416
const WHITE = 'rgb(100% 100% 100%)';
1517
const BLACK = 'rgb(0% 0% 0%)';
@@ -47,8 +49,8 @@ function initContext(_ctx: CanvasRenderingContext2D) {
4749
ctx.canvas.width = D;
4850
ctx.canvas.height = D;
4951
ctx.lineCap = 'round';
50-
ctx.lineWidth = 4;
51-
ctx.shadowBlur = 4;
52+
ctx.lineWidth = LINE_WIDTH;
53+
ctx.shadowBlur = SHADOW_WIDTH;
5254

5355
onColourSchemeChange((scheme) => {
5456
primaryColour = scheme === 'dark' ? WHITE : BLACK;
@@ -62,24 +64,37 @@ function draw() {
6264
const drawTime = Date.now();
6365
ctx.clearRect(0, 0, D, D);
6466

65-
for (let n = 0, N = memory.length; n < N; n++) {
66-
const memo = memory[n];
67-
memo.age = drawTime - memo.when;
68-
drawLine(memo);
69-
}
67+
if (memory.length) {
68+
for (let n = 0, N = memory.length; n < N; n++) {
69+
const memo = memory[n];
70+
memo.age = drawTime - memo.when;
71+
drawLine(memo);
72+
}
7073

71-
let n = memory.length;
72-
while (n--) {
73-
if (memory[n].age >= ANIMATION_DURATION) {
74-
memory.pop();
75-
} else {
76-
break;
74+
let n = memory.length;
75+
while (n--) {
76+
if (memory[n].age >= ANIMATION_DURATION) {
77+
memory.pop();
78+
} else {
79+
break;
80+
}
7781
}
82+
} else {
83+
drawCenter();
7884
}
7985

8086
raf = requestAnimationFrame(draw);
8187
}
8288

89+
function drawCenter() {
90+
ctx.save();
91+
ctx.beginPath();
92+
ctx.arc(pRotationAxis.x, pRotationAxis.y, 0.5, 0, PI2);
93+
ctx.stroke();
94+
ctx.closePath();
95+
ctx.restore();
96+
}
97+
8398
function drawLine(memo: IMemo) {
8499
const length = R - memo.age * R / ANIMATION_DURATION;
85100
const p = memo.vector.setLength(length).toPoint(pRotationAxis);
@@ -95,9 +110,6 @@ function drawLine(memo: IMemo) {
95110

96111
type TColourScheme = 'light' | 'dark';
97112

98-
/**
99-
* NOTE: if OS is dark but devtools is default - then scheme is dark
100-
*/
101113
export function onColourSchemeChange(
102114
callback: (scheme: TColourScheme) => void,
103115
) {

0 commit comments

Comments
 (0)