Skip to content

Commit c8dd366

Browse files
committed
fix: scale canvas for hidpi
1 parent 4cf2fc9 commit c8dd366

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

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

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ export default defineInstrument({
1616
wrapper.classList.add('canvas-wrapper');
1717

1818
const canvas = document.createElement('canvas');
19-
canvas.height = 320;
20-
canvas.width = 480;
19+
20+
const ratio = window.devicePixelRatio * 3;
21+
const height = 320;
22+
const width = 480;
23+
24+
canvas.width = width * ratio;
25+
canvas.height = height * ratio;
26+
canvas.style.width = width + 'px';
27+
canvas.style.height = height + 'px';
2128

2229
wrapper.appendChild(canvas);
2330
document.body.appendChild(wrapper);
2431

2532
const ctx = canvas.getContext('2d')!;
33+
ctx.scale(ratio, ratio);
2634
const ballRadius = 10;
2735
const brickRowCount = 5;
2836
const brickColumnCount = 3;
@@ -34,11 +42,11 @@ export default defineInstrument({
3442
const paddleHeight = 10;
3543
const paddleWidth = 75;
3644

37-
let x = canvas.width / 2;
38-
let y = canvas.height - 30;
45+
let x = width / 2;
46+
let y = height - 30;
3947
let dx = 2;
4048
let dy = -2;
41-
let paddleX = (canvas.width - paddleWidth) / 2;
49+
let paddleX = (width - paddleWidth) / 2;
4250
let score = 0;
4351
let lives = 3;
4452

@@ -73,7 +81,7 @@ export default defineInstrument({
7381
}
7482
function mouseMoveHandler(e: MouseEvent) {
7583
const relativeX = e.clientX - canvas.offsetLeft;
76-
if (relativeX > 0 && relativeX < canvas.width) {
84+
if (relativeX > 0 && relativeX < width) {
7785
paddleX = relativeX - paddleWidth / 2;
7886
}
7987
}
@@ -109,7 +117,7 @@ export default defineInstrument({
109117
}
110118
function drawPaddle() {
111119
ctx.beginPath();
112-
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
120+
ctx.rect(paddleX, height - paddleHeight, paddleWidth, paddleHeight);
113121
ctx.fillStyle = '#0095DD';
114122
ctx.fill();
115123
ctx.closePath();
@@ -139,24 +147,24 @@ export default defineInstrument({
139147
function drawLives() {
140148
ctx.font = '16px Arial';
141149
ctx.fillStyle = '#0095DD';
142-
ctx.fillText('Lives: ' + lives, canvas.width - 65, 20);
150+
ctx.fillText('Lives: ' + lives, width - 65, 20);
143151
}
144152

145153
function draw() {
146-
ctx.clearRect(0, 0, canvas.width, canvas.height);
154+
ctx.clearRect(0, 0, width, height);
147155
drawBricks();
148156
drawBall();
149157
drawPaddle();
150158
drawScore();
151159
drawLives();
152160
collisionDetection();
153161

154-
if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
162+
if (x + dx > width - ballRadius || x + dx < ballRadius) {
155163
dx = -dx;
156164
}
157165
if (y + dy < ballRadius) {
158166
dy = -dy;
159-
} else if (y + dy > canvas.height - ballRadius) {
167+
} else if (y + dy > height - ballRadius) {
160168
if (x > paddleX && x < paddleX + paddleWidth) {
161169
dy = -dy;
162170
} else {
@@ -167,16 +175,16 @@ export default defineInstrument({
167175
timeElapsed: Date.now() - startTime
168176
});
169177
} else {
170-
x = canvas.width / 2;
171-
y = canvas.height - 30;
178+
x = width / 2;
179+
y = height - 30;
172180
dx = 2;
173181
dy = -2;
174-
paddleX = (canvas.width - paddleWidth) / 2;
182+
paddleX = (width - paddleWidth) / 2;
175183
}
176184
}
177185
}
178186

179-
if (rightPressed && paddleX < canvas.width - paddleWidth) {
187+
if (rightPressed && paddleX < width - paddleWidth) {
180188
paddleX += 7;
181189
} else if (leftPressed && paddleX > 0) {
182190
paddleX -= 7;

0 commit comments

Comments
 (0)