Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ module.exports = {
rules: {
quotes: ["error", "double"],
"no-shadow": ["error", { hoist: "functions", allow: [] }],
"no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
},
};
22 changes: 13 additions & 9 deletions css/panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@
justify-content: space-between;
}

.panel-row button {
.panel-row ui-button,
.panel-row color-button,
.panel-row tool-button,
.panel-row variant-button,
.panel-row variant-stamp-button {
margin-right: var(--button-space);
width: var(--button-size);
height: var(--button-size);
border: 2px solid black;
border-radius: 6px;
}

.panel-row button:last-child {
.panel-row ui-button:last-child,
.panel-row color-button:last-child,
.panel-row tool-button:last-child,
.panel-row variant-button:last-child,
.panel-row variant-stamp-button:last-child {
margin-right: 0px;
}

.panel-row button.active {
border: 2px solid white;
box-shadow: 0px 0px 0px 4px orange;
#tools {
display: flex;
flex: 1;
}

#actions {
Expand Down
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ <h3>New version available</h3>
</div>
<div id="right-panel-column">
<section id="global-actions" class="panel-row">
<button id="clear"><img src="img/buttons/clear.svg" /></button>
<button id="save"><img src="img/buttons/save.svg" /></button>
<button id="info"><img src="img/buttons/info.svg" /></button>
<ui-button id="clear" icon-url="img/buttons/clear.svg"></ui-button>
<ui-button id="save" icon-url="img/buttons/save.svg"></ui-button>
<ui-button id="info" icon-url="img/buttons/info.svg"></ui-button>
</section>
</div>
</section>
Expand Down
14 changes: 14 additions & 0 deletions js/boot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ import { attachKeyboardListeners } from "./controls/keyboard.mjs";
import { attachGamepadBlockListeners } from "./controls/general.mjs";
import { attachGamepadListeners } from "./controls/gamepad.mjs";
import { initializeCursor } from "./cursor.mjs";
import { ColorButton } from "./ui/color.mjs";
import { ToolButton } from "./ui/tool.mjs";
import { VariantButton } from "./ui/variant.mjs";
import { VariantStampButton } from "./ui/variant-stamp.mjs";
import { UiButton } from "./ui/button.mjs";

function registerComponents() {
customElements.define("ui-button", UiButton);
customElements.define("color-button", ColorButton);
customElements.define("tool-button", ToolButton);
customElements.define("variant-button", VariantButton);
customElements.define("variant-stamp-button", VariantStampButton);
}

function attachResizeListeners() {
const canvas = getCanvas();
Expand Down Expand Up @@ -59,6 +72,7 @@ export function boot() {
const state = createState();
const canvas = getCanvas();

registerComponents();
restorePreviousCanvas(canvas);
attachCanvasSaveListener(canvas);

Expand Down
12 changes: 0 additions & 12 deletions js/controls/keyboard.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ export function attachKeyboardListeners(state) {
let accelerationTimer = null;
let keysPressed = NO_KEYS_PRESSED;

function shouldBlockInteractions() {
return state.get((prevState) => prevState.blockedInteractions);
}

window.addEventListener("keyup", (event) => {
if (shouldBlockInteractions()) {
return;
}

switch (event.key) {
case "ArrowUp":
keysPressed = produceMovementKeysPressed(keysPressed, "up", false);
Expand All @@ -67,10 +59,6 @@ export function attachKeyboardListeners(state) {
});

window.addEventListener("keydown", (event) => {
if (shouldBlockInteractions()) {
return;
}

if (event.key === "p") {
setTool(TOOLS.PEN, { state });
} else if (event.key === "f") {
Expand Down
8 changes: 0 additions & 8 deletions js/cursor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,11 @@ export function initializeCursor({ state }) {
const y = cursor.y;
let setCursorTimer = null;

function shouldBlockInteractions() {
return state.get((prevState) => prevState.blockedInteractions);
}

function drawCursorOnMouseMove(event) {
if (setCursorTimer) {
window.clearTimeout(setCursorTimer);
}

if (shouldBlockInteractions()) {
return;
}

const rect = canvas.getBoundingClientRect();
const { clientX, clientY } = event;

Expand Down
26 changes: 26 additions & 0 deletions js/dom.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ export function getPanelTools() {
return document.getElementById("tools");
}

export function getToolButtons() {
return getPanelTools().querySelectorAll("tool-button");
}

export function getToolButtonById(id) {
return Array.from(getToolButtons()).find((button) => button.id === id);
}

export function getPanelToolVariants() {
return document.getElementById("variants");
}

export function getVariantButtons() {
return getPanelToolVariants().querySelectorAll(
"variant-button,variant-stamp-button",
);
}

export function getVariantButtonById(id) {
return Array.from(getVariantButtons()).find((button) => button.id === id);
}

export function getPanelToolActions() {
return document.getElementById("actions");
}
Expand All @@ -24,6 +42,14 @@ export function getPanelColors() {
return document.getElementById("colors");
}

export function getColorButtons() {
return getPanelColors().querySelectorAll("color-button");
}

export function getColorButtonByColor(color) {
return Array.from(getColorButtons()).find((button) => button.color === color);
}

export function getPanel() {
return document.getElementById("panel");
}
Expand Down
3 changes: 0 additions & 3 deletions js/state/actions/cam.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getCountdownAnimationLengthInSeconds,
getFlashAnimationLengthInSeconds,
} from "../../dom.mjs";
import { blockInteractions, unblockInteractions } from "../actions/ui.mjs";

function memorizePhoto({ state }) {
state.set(() => ({
Expand All @@ -29,7 +28,6 @@ export function takePhoto({ state }) {
const flashAnimationLength = getFlashAnimationLengthInSeconds() * 1000;

insertCountdown();
blockInteractions({ state });

const videoSettings = cam.srcObject.getVideoTracks()[0]?.getSettings();
const height = canvas.height;
Expand All @@ -39,7 +37,6 @@ export function takePhoto({ state }) {
ctx.drawImage(cam, canvas.width / 2 - width / 2, 0, width, height);
memorizePhoto({ state });
removeCountdown();
unblockInteractions({ state });
}, countdownAnimationLength + flashAnimationLength);
}

Expand Down
12 changes: 0 additions & 12 deletions js/state/actions/ui.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { getInfoDialog } from "../../dom.mjs";

export function blockInteractions({ state }) {
state.set(() => ({
blockedInteractions: true,
}));
}

export function unblockInteractions({ state }) {
state.set(() => ({
blockedInteractions: false,
}));
}

export function showInfo() {
const dialog = getInfoDialog();
const closeButton = dialog.querySelector("#close-info");
Expand Down
1 change: 0 additions & 1 deletion js/state/state.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function createState() {
color: loadColor(),
gamepad: null,
photoMemorized: false,
blockedInteractions: false,
gamepadBlocked: false,
};

Expand Down
33 changes: 2 additions & 31 deletions js/tools/fill.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import {
isPrimaryGamepadButtonPressed,
} from "../controls/gamepad.mjs";
import { isWithinCanvasBounds } from "../canvas.mjs";
import {
blockInteractions,
unblockInteractions,
} from "../state/actions/ui.mjs";

function hexToRGB(h) {
let r = 0,
Expand Down Expand Up @@ -53,9 +49,8 @@ function colorsMatch(a, b) {
}

// taken from SO: https://stackoverflow.com/a/56221940/3056783
function floodFill(ctx, x, y, fillColor, { state }) {
function floodFill(ctx, x, y, fillColor) {
showLoader();
blockInteractions({ state });
// read the pixels in the canvas
const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);

Expand Down Expand Up @@ -83,7 +78,6 @@ function floodFill(ctx, x, y, fillColor, { state }) {
ctx.putImageData(imageData, 0, 0);
}

unblockInteractions({ state });
hideLoader();
}

Expand Down Expand Up @@ -131,18 +125,6 @@ export function activateFill({ state }) {
window.removeEventListener("keydown", keyDown);
}

function onBlockInteractionsChange(nextState, prevState) {
if (nextState.blockedInteractions === prevState.blockedInteractions) {
return;
}

if (nextState.blockedInteractions) {
deactivateListeners();
} else {
activateListeners();
}
}

let wasPressed = false;
function activateFillOnGamepadButtonPress() {
const gamepad = getGamepad(state);
Expand Down Expand Up @@ -187,20 +169,9 @@ export function activateFill({ state }) {
frame = null;
}

const blockedInteractions = state.get(
(prevState) => prevState.blockedInteractions,
);

if (blockedInteractions) {
deactivateListeners();
} else {
activateListeners();
}

state.addListener(onBlockInteractionsChange);
activateListeners();

return function dispose() {
deactivateListeners();
state.removeListener(onBlockInteractionsChange);
};
}
24 changes: 1 addition & 23 deletions js/tools/pen.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,7 @@ export function activatePen({ state, variant }) {
cancelGamepadAnimationFrame();
}

function onBlockInteractionsChange(nextState, prevState) {
if (nextState.blockedInteractions === prevState.blockedInteractions) {
return;
}

if (nextState.blockedInteractions) {
deactivateListeners();
} else {
activateListeners();
}
}

const blockInteractions = state.get(
(prevState) => prevState.blockedInteractions,
);

if (blockInteractions) {
deactivateListeners();
} else {
activateListeners();
}
activateListeners();

function updateColor(nextState, prevState) {
if (nextState.color === prevState.color) {
Expand All @@ -243,13 +223,11 @@ export function activatePen({ state, variant }) {
draw(nextState.cursor.x, nextState.cursor.y);
}

state.addListener(onBlockInteractionsChange);
state.addListener(updateColor);
state.addListener(updatePath);

return function dispose() {
deactivateListeners();
state.removeListener(onBlockInteractionsChange);
state.removeListener(updateColor);
state.removeListener(updatePath);
};
Expand Down
Loading