finished ch 9
This commit is contained in:
9
9/canvas.html
Normal file
9
9/canvas.html
Normal file
@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Canvas</title>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="canvas" width="300" height="300"></canvas>
|
||||
<script src="cs.js"></script>
|
||||
</body
|
42
9/cs.js
Normal file
42
9/cs.js
Normal file
@ -0,0 +1,42 @@
|
||||
let canvas = document.querySelector("#canvas");
|
||||
let ctx = canvas.getContext("2d");
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
let forwards = true;
|
||||
|
||||
function drawCircle(x, y) {
|
||||
ctx.fillStyle = "rgb(0, 128, 255)";
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, 10, 0, Math.PI * 2, false);
|
||||
ctx.fill();
|
||||
}
|
||||
function update() {
|
||||
if (forwards === true) {
|
||||
x += 1;
|
||||
y = 10
|
||||
}
|
||||
else{
|
||||
x -= 1;
|
||||
y = 10
|
||||
}
|
||||
if (x >= 300){
|
||||
forwards = false;
|
||||
}
|
||||
if (x <= 0){
|
||||
forwards = true
|
||||
}
|
||||
// y += 1;
|
||||
// x %= width;
|
||||
// y %= height;
|
||||
}
|
||||
|
||||
function draw() {
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
drawCircle(x, y);
|
||||
}
|
||||
setInterval(() => {
|
||||
update();
|
||||
draw();
|
||||
}, 1);
|
Reference in New Issue
Block a user