Skip to content

Commit ef94ee8

Browse files
authored
Merge pull request #6 from comatory/comatory/fix-initial-canvas
fix initial canvas
2 parents d9152f3 + d8fb418 commit ef94ee8

File tree

11 files changed

+50
-26
lines changed

11 files changed

+50
-26
lines changed

index.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ <h3>New version available</h3>
8484
</section>
8585
</main>
8686
<dialog id="info-dialog">
87+
<script type="module">
88+
const info = document.getElementById("version-id");
89+
90+
try {
91+
const pkg = await fetch("/package.json");
92+
const json = await pkg.json();
93+
const { version } = json;
94+
info.textContent = `Version: ${version}`;
95+
} catch (error) {
96+
console.error(error);
97+
info.textContent = "Version: unknown";
98+
}
99+
</script>
87100
<h3>sdraw</h3>
88101
<section class="colophon">
89102
<img
@@ -94,7 +107,7 @@ <h3>sdraw</h3>
94107
/>
95108
<div class="colophon-info">
96109
<pre>MIT License</pre>
97-
<pre>Version: 0.1.2</pre>
110+
<pre id="version-id">Version</pre>
98111
<pre>2023</pre>
99112
</div>
100113
</section>

js/boot.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getCursorCanvas,
44
setCanvasSizeByViewport,
55
setCanvasSizeWithoutPanel,
6+
setCanvasFill,
67
setVideoSizeByCanvasSize,
78
} from "./dom.mjs";
89
import {
@@ -29,6 +30,7 @@ function attachResizeListeners() {
2930
setCanvasSizeByViewport(cursorCanvas);
3031
setCanvasSizeWithoutPanel(canvas);
3132
setVideoSizeByCanvasSize(canvas);
33+
setCanvasFill(canvas);
3234

3335
attachWindowResizeListeners({ canvas, cursorCanvas });
3436
}

js/canvas-utils.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { COLOR } from "./state/constants.mjs";
22
import { getCursorCanvas, getCanvas } from "./dom.mjs";
33
import { getPanel } from "./dom.mjs";
4-
import { isCursorWithinPanelBounds } from "./ui-utils.mjs";
4+
import { isCursorWithinPanelBounds } from "./ui/utils.mjs";
55

66
const ctx = getCursorCanvas().getContext("2d");
77
const canvasCtx = getCanvas().getContext("2d");
@@ -19,8 +19,8 @@ const LINE_WIDTH = 5;
1919
*
2020
* @returns {number[]} - Inverted color in r, g,b order.
2121
*/
22-
function invertColor(r, g, b, a) {
23-
const rgb = [r, g, b, a];
22+
function invertColor(r, g, b) {
23+
const rgb = [r, g, b];
2424
for (var i = 0; i < rgb.length; i++) rgb[i] = (i === 3 ? 1 : 255) - rgb[i];
2525
return rgb;
2626
}
@@ -42,9 +42,9 @@ function getPixelRGBAColor(x, y) {
4242
}
4343

4444
const pixel = canvasCtx.getImageData(x, y, GAP, GAP);
45-
const [r, g, b, a] = invertColor(...pixel.data);
45+
const [r, g, b] = invertColor(...pixel.data);
4646

47-
return `rgb(${r}, ${g}, ${b}, ${Math.abs(a)})`;
47+
return `rgb(${r}, ${g}, ${b})`;
4848
}
4949

5050
export function drawCursor(x, y) {

js/dom.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { COLOR } from "./state/constants.mjs";
2+
13
export function getCanvas() {
24
return document.getElementById("canvas");
35
}
@@ -53,6 +55,12 @@ export function setVideoSizeByCanvasSize(canvas) {
5355
video.style.height = `${canvas.height}px`;
5456
}
5557

58+
export function setCanvasFill(canvas) {
59+
const ctx = canvas.getContext("2d");
60+
ctx.fillStyle = COLOR.WHITE;
61+
ctx.fillRect(0, 0, canvas.width, canvas.height);
62+
}
63+
5664
export function insertCountdown() {
5765
const countdown = document.createElement("div");
5866
countdown.id = "countdown";

js/state/actions/canvas.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { getCanvas } from "../../dom.mjs";
1+
import { getCanvas, setCanvasFill } from "../../dom.mjs";
22
import { storeCanvas } from "../storage.mjs";
33

44
export function resetCanvas() {
55
const canvas = getCanvas();
6-
const ctx = canvas.getContext("2d");
76

8-
ctx.clearRect(0, 0, canvas.width, canvas.height);
7+
setCanvasFill(canvas);
98
storeCanvas(canvas);
109
}
1110

js/ui-utils.mjs

Lines changed: 0 additions & 12 deletions
This file was deleted.

js/ui/panel.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
isPrimaryGamepadButtonPressed,
44
} from "../controls/gamepad.mjs";
55
import { getPanel } from "../dom.mjs";
6-
import { isCursorWithinPanelBounds } from "../ui-utils.mjs";
6+
import { isCursorWithinPanelBounds } from "./utils.mjs";
77

88
function getPanelButtonByCoordinates(x, y, panel) {
99
const buttons = panel.querySelectorAll("button");

js/ui/utils.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ export function updateActivatedButton(buttonContainer, value) {
3838
}
3939
});
4040
}
41+
42+
/**
43+
* Is cursor located on UI panel?
44+
*
45+
* @param {number} x - Cursor x coordinate.
46+
* @param {number} y - Cursor y coordinate.
47+
* @param {DOMRect} rect - Panel bounds.
48+
*
49+
* @returns {boolean} - Is cursor located on UI panel?
50+
*/
51+
export function isCursorWithinPanelBounds(x, y, rect) {
52+
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
53+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sdraw",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Simple drawing application for kids, can be controlled via mouse, keyboard or gamepad.",
55
"private": true,
66
"main": "index.mjs",

0 commit comments

Comments
 (0)