Skip to content

Commit 7710442

Browse files
HTML canvas
1 parent 3496c26 commit 7710442

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

apps/scratch-clone/pages/index.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,37 @@ export function Index() {
88
*/
99
return (
1010
<div className={styles.page}>
11-
SCRATCH CLONE
11+
12+
<canvas id="myCanvas" width="300" height="300" style={{ border: "1px solid #000000" }}>
13+
</canvas>
14+
15+
<img src='/apple.png' alt="apple" height={50} />
16+
17+
<img src='/cat.png' alt="apple" height={50} />
18+
19+
<button onClick={drawHouse}>DRAW HOUSE</button>
1220
</div>
1321
);
1422
}
1523

24+
function drawHouse() {
25+
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
26+
const ctx = canvas.getContext('2d');
27+
ctx.lineWidth = 10;
28+
29+
// Wall
30+
ctx.strokeRect(75, 140, 150, 110);
31+
32+
// Door
33+
ctx.fillRect(130, 190, 40, 60);
34+
35+
// Roof
36+
ctx.beginPath();
37+
ctx.moveTo(50, 140);
38+
ctx.lineTo(150, 60);
39+
ctx.lineTo(250, 140);
40+
ctx.closePath();
41+
ctx.stroke();
42+
}
43+
1644
export default Index;
77.1 KB
Loading

apps/scratch-clone/public/cat.png

160 KB
Loading

apps/scratch-clone/sprite/Scratch.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ export default class Scratch {
1010
changeXBy(value) {
1111
this.x = this.x + value
1212
}
13+
changeyBy(value) {
14+
this.y = this.y + value
15+
}
1316
whenGreenFlagClicked() {
14-
this.setXTo(100)
15-
this.setYTo(50)
17+
this.setXTo(-215)
18+
this.setYTo(163)
19+
// eslint-disable-next-line no-constant-condition
20+
while(true) {
21+
this.changeXBy(1)
22+
this.changeyBy(-1)
23+
}
1624
}
1725
}

0 commit comments

Comments
 (0)