Skip to content

Commit e212113

Browse files
committed
fix: add scaling
1 parent c8dd366 commit e212113

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

  • packages/instrument-library/src/interactive/breakout-task

packages/instrument-library/src/interactive/breakout-task/index.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import { z } from '/runtime/v1/zod@3.23.x';
33

44
import './styles.css';
55

6+
const baseHeight = 320;
7+
const baseWidth = 480;
8+
9+
function getScale() {
10+
const ratios = [1, 1.25, 1.5, 2];
11+
for (let i = 1; i < ratios.length; i++) {
12+
const multiplier = ratios[i]! + 0.25;
13+
const [heightThreshold, widthThreshold] = [baseHeight * multiplier, baseWidth * multiplier];
14+
if (window.innerHeight < heightThreshold || window.innerWidth < widthThreshold) {
15+
return ratios[i - 1]!;
16+
}
17+
}
18+
return ratios.at(-1)!;
19+
}
20+
621
export default defineInstrument({
722
clientDetails: {
823
estimatedDuration: 1,
@@ -11,15 +26,16 @@ export default defineInstrument({
1126
content: {
1227
render(done) {
1328
const startTime = Date.now();
29+
const ratio = window.devicePixelRatio * 3;
30+
const scale = getScale();
1431

1532
const wrapper = document.createElement('div');
1633
wrapper.classList.add('canvas-wrapper');
1734

1835
const canvas = document.createElement('canvas');
1936

20-
const ratio = window.devicePixelRatio * 3;
21-
const height = 320;
22-
const width = 480;
37+
const height = baseHeight * scale;
38+
const width = baseWidth * scale;
2339

2440
canvas.width = width * ratio;
2541
canvas.height = height * ratio;
@@ -31,21 +47,22 @@ export default defineInstrument({
3147

3248
const ctx = canvas.getContext('2d')!;
3349
ctx.scale(ratio, ratio);
34-
const ballRadius = 10;
50+
51+
const ballRadius = 10 * scale;
3552
const brickRowCount = 5;
3653
const brickColumnCount = 3;
37-
const brickWidth = 75;
38-
const brickHeight = 20;
39-
const brickPadding = 10;
40-
const brickOffsetTop = 30;
41-
const brickOffsetLeft = 30;
42-
const paddleHeight = 10;
43-
const paddleWidth = 75;
54+
const brickWidth = 75 * scale;
55+
const brickHeight = 20 * scale;
56+
const brickPadding = 10 * scale;
57+
const brickOffsetTop = 30 * scale;
58+
const brickOffsetLeft = 30 * scale;
59+
const paddleHeight = 10 * scale;
60+
const paddleWidth = 75 * scale;
4461

4562
let x = width / 2;
4663
let y = height - 30;
47-
let dx = 2;
48-
let dy = -2;
64+
let dx = 2 * scale;
65+
let dy = -2 * scale;
4966
let paddleX = (width - paddleWidth) / 2;
5067
let score = 0;
5168
let lives = 3;
@@ -177,8 +194,8 @@ export default defineInstrument({
177194
} else {
178195
x = width / 2;
179196
y = height - 30;
180-
dx = 2;
181-
dy = -2;
197+
dx = 2 * scale;
198+
dy = -2 * scale;
182199
paddleX = (width - paddleWidth) / 2;
183200
}
184201
}

0 commit comments

Comments
 (0)