move: fix canvas size and bad dt unit
This commit is contained in:
@@ -48,7 +48,7 @@ function updateFollow(ctx: CanvasRenderingContext2D, dt: number) {
|
|||||||
function updateBounce(ctx: CanvasRenderingContext2D, dt: number) {
|
function updateBounce(ctx: CanvasRenderingContext2D, dt: number) {
|
||||||
// update head pos
|
// update head pos
|
||||||
// P_t+1 = P_t + V * t
|
// P_t+1 = P_t + V * t
|
||||||
const newPos = trail[0].add(velocity.scale(0.001*dt));
|
const newPos = trail[0].add(velocity.scale(dt));
|
||||||
if (newPos.x > ctx.canvas.width - 100) { velocity.x *= -1; newPos.x = ctx.canvas.width - 100; }
|
if (newPos.x > ctx.canvas.width - 100) { velocity.x *= -1; newPos.x = ctx.canvas.width - 100; }
|
||||||
if (newPos.y > ctx.canvas.height - 100) { velocity.y *= -1; newPos.y = ctx.canvas.height - 100; }
|
if (newPos.y > ctx.canvas.height - 100) { velocity.y *= -1; newPos.y = ctx.canvas.height - 100; }
|
||||||
if (newPos.x < 100) { velocity.x *= -1; newPos.x = 100; }
|
if (newPos.x < 100) { velocity.x *= -1; newPos.x = 100; }
|
||||||
@@ -82,8 +82,8 @@ function init() {
|
|||||||
if (!canvas) throw new Error("unable to get canvas HTML element");
|
if (!canvas) throw new Error("unable to get canvas HTML element");
|
||||||
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D | null;
|
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D | null;
|
||||||
if (!ctx) throw new Error("unable to get canvas 2D context");
|
if (!ctx) throw new Error("unable to get canvas 2D context");
|
||||||
ctx.canvas.width = ctx.canvas.clientWidth;
|
ctx.canvas.width = window.innerWidth
|
||||||
ctx.canvas.height = ctx.canvas.clientHeight;
|
ctx.canvas.height = window.innerHeight
|
||||||
|
|
||||||
canvas.onmousemove = (evt) => {
|
canvas.onmousemove = (evt) => {
|
||||||
const {clientX, clientY} = evt;
|
const {clientX, clientY} = evt;
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="status-line"></div>
|
<div id="status-line"></div>
|
||||||
<div class="canvas-wrapper">
|
<div class="canvas-wrapper">
|
||||||
<canvas id="game-canvas"></canvas>
|
<canvas id="game-canvas" tabindex="0"></canvas>
|
||||||
</div>
|
</div>
|
||||||
<script type="module" src="snake.js"></script>
|
<script type="module" src="snake.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user