Skip to content

Commit b64b4b1

Browse files
committed
feat: キャンバスの上に表示するペンの枠の幅と高さを修正
1 parent b4a40fa commit b64b4b1

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/components/CanvasView.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import {
1313
editingState,
1414
nibSizeState,
1515
paletteNibState,
16-
paletteSelectionState,
1716
preloadNibState,
1817
sceneScreenState,
1918
sceneState
2019
} from '../recoils';
2120
import { useDropper } from '../recoils/useDropper';
2221
import Cursor, { cursorClasses } from '../utils/cursor';
23-
import { getMatrix } from '../utils/selection';
2422
import { editWithCursor } from '../utils/updateScene';
2523
import { Paper } from './Paper';
2624

@@ -93,24 +91,9 @@ export function CanvasView() {
9391
const canvas = nibCanvasRef.current;
9492
const ctx = canvas?.getContext('2d');
9593
if (!canvas || !ctx) return;
96-
const selectionLoadable = getLoadable(paletteSelectionState);
97-
let rows = 0;
98-
let cols = 0;
99-
if (selectionLoadable.state === 'hasValue') {
100-
const selection = selectionLoadable.contents;
101-
if (selection) {
102-
const matrix = getMatrix(selection);
103-
rows = matrix.length;
104-
cols = matrix[0]?.length || 0;
105-
}
106-
}
107-
const nibSizeLoadable = getLoadable(nibSizeState);
108-
if (
109-
nibSizeLoadable.state === 'hasValue' &&
110-
nibSizeLoadable.contents > 1
111-
) {
112-
rows = cols = nibSizeLoadable.contents;
113-
}
94+
const loadable = getLoadable(nibSizeState);
95+
if (loadable.state !== 'hasValue') return;
96+
const { cols, rows } = loadable.contents;
11497
ctx.clearRect(0, 0, canvas.width, canvas.height);
11598
ctx.strokeStyle = '#333';
11699
ctx.strokeRect(x * 32, y * 32, 32 * cols, 32 * rows);

src/recoils/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,29 @@ export const baseSelectionState = selector<Selection>({
193193
set(editingState, curr => updateBase(curr, tile));
194194
}
195195
});
196+
197+
/**
198+
* キャンバスの上に表示するペンの枠の幅と高さ
199+
*/
200+
export const nibSizeState = selector<{ rows: number; cols: number }>({
201+
key: 'nibSizeState',
202+
get: ({ get }) => {
203+
// ペン以外の場合は問答無用で 1
204+
const cursorMode = get(cursorModeState);
205+
if (cursorMode !== 'pen') {
206+
return { cols: 1, rows: 1 };
207+
}
208+
// ペン幅を変えている場合は、その大きさに
209+
const nibWidth = get(nibWidthState);
210+
if (nibWidth > 1) {
211+
return { cols: nibWidth, rows: nibWidth };
212+
}
213+
// パレットを選択しているなら、その大きさに
214+
const selection = get(paletteSelectionState);
215+
if (selection) {
216+
const matrix = getMatrix(selection);
217+
return { rows: matrix.length, cols: matrix[0]?.length || 0 };
218+
}
219+
return { cols: 1, rows: 1 };
220+
}
221+
});

0 commit comments

Comments
 (0)