Skip to content

Commit 4d11872

Browse files
authored
Merge pull request #46 from MarkE16/small-refactor
refactor: fix drawing background and store values
2 parents e934b4b + 0a65a6c commit 4d11872

6 files changed

Lines changed: 18 additions & 19 deletions

File tree

src/components/ColorWheel/ColorWheel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function ColorWheel(props: ColorWheelProps): ReactNode {
2929
}))
3030
);
3131

32-
const onChange = (color: Color) => changeColor(color.toString());
32+
const onChange = (color: Color) => changeColor(color.toString('hex'));
3333
const wheelColor = parseColor(color).toString("hsl");
3434

3535
const COLOR_WHEEL_OUTER_RADIUS = 80;

src/components/Navbar/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Navbar(): ReactNode {
4141

4242
const handleSaveFile = useCallback(async () => {
4343
try {
44-
const { layers, elements } = await prepareForSave();
44+
const { layers, elements } = prepareForSave();
4545

4646
if (layers.length === 0) {
4747
throw new Error("No layers to save. This is a bug.");

src/state/slices/canvasSlice.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export const createCanvasSlice: StateCreator<
215215
* elements. Therefore, the caller must save the
216216
* layers and elements themselves.
217217
*/
218-
async function prepareForSave(): Promise<SavedCanvasProperties> {
218+
function prepareForSave(): SavedCanvasProperties {
219219
const { layers, elements } = get();
220220

221221
return { layers, elements };
@@ -270,7 +270,7 @@ export const createCanvasSlice: StateCreator<
270270
return lines;
271271
}
272272

273-
async function prepareForExport(quality: number = 1): Promise<Blob> {
273+
function prepareForExport(quality: number = 1): Promise<Blob> {
274274
const accountForDPI = true; // Consider DPI for better quality exports
275275

276276
const substituteCanvas = document.createElement("canvas");
@@ -317,7 +317,7 @@ export const createCanvasSlice: StateCreator<
317317

318318
function drawCanvas(canvas: HTMLCanvasElement, layerId?: string) {
319319
let elements = get().elements;
320-
const { background, layers, dpi, width: canvasWidth } = get();
320+
const { background, layers, dpi, width: canvasWidth, height: canvasHeight } = get();
321321

322322
if (layers.length === 0) {
323323
throw new Error("No layers available to draw on the canvas.");
@@ -357,15 +357,14 @@ export const createCanvasSlice: StateCreator<
357357

358358
if (isPreviewCanvas) {
359359
// If the canvas is a preview canvas, scale it down.
360-
const scale = canvas.width / (canvasWidth * dpi);
360+
const scaleX = canvas.width / (canvasWidth * dpi);
361+
const scaleY = canvas.height / (canvasHeight * dpi);
361362
// Save the current transform state.
362363
ctx.save();
363364

364-
ctx.scale(scale, scale);
365+
ctx.scale(scaleX, scaleY);
365366
}
366367

367-
console.log("drawing...", layers, elements);
368-
369368
for (const element of elements) {
370369
const { x, y, width, height } = element;
371370

@@ -442,6 +441,9 @@ export const createCanvasSlice: StateCreator<
442441
}
443442
}
444443

444+
if (isPreviewCanvas) {
445+
ctx.restore(); // Restore the previous transform state.
446+
}
445447
// Finally, draw the background behind all elements.
446448
ctx.fillStyle = background;
447449

@@ -450,16 +452,13 @@ export const createCanvasSlice: StateCreator<
450452
ctx.globalCompositeOperation = "destination-over";
451453
ctx.fillRect(canvasX, canvasY, canvas.width, canvas.height);
452454

453-
if (isPreviewCanvas) {
454-
ctx.restore(); // Restore the previous transform state.
455-
}
456455
}
457456

458457
return {
459458
width: 400,
460459
height: 400,
461460
mode: "move",
462-
background: "white",
461+
background: "#ffffff",
463462
shape: "rectangle",
464463
shapeMode: "fill",
465464
color: "#000000",

src/tests/integration/ColorWheel.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe("ColorWheel functionality", () => {
128128

129129
// Assert that dispatch was called with the correct action
130130
expect(preloadedState.changeColor).toHaveBeenCalledWith(
131-
mockColor.toString()
131+
mockColor.toString("hex")
132132
);
133133
});
134134

@@ -139,7 +139,7 @@ describe("ColorWheel functionality", () => {
139139

140140
// Assert that dispatch was called with the correct action
141141
expect(preloadedState.changeColor).toHaveBeenCalledWith(
142-
mockColor.toString()
142+
mockColor.toString('hex')
143143
);
144144
});
145145

src/tests/unit/useStore.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { CanvasElement, HistoryAction, SliceStores } from "../../types";
88
import { MODES } from "../../state/store";
99

1010
const exampleStore: SliceStores = {
11-
background: "white",
11+
background: "#ffffff",
1212
width: 400,
1313
height: 400,
1414
shape: "rectangle",
@@ -402,7 +402,7 @@ describe("useStore functionality", () => {
402402
result.result.current.setLayers(layers);
403403
result.result.current.setElements(elements);
404404
});
405-
expect(result.result.current.prepareForSave()).resolves.toEqual(expected);
405+
expect(result.result.current.prepareForSave()).toEqual(expected);
406406
});
407407

408408
it("should return a blob for exporting", () => {
@@ -547,7 +547,7 @@ describe("useStore functionality", () => {
547547
act(() => {
548548
result.result.current.setLayers([]);
549549
});
550-
expect(() => result.result.current.prepareForExport()).rejects.toThrow();
550+
expect(() => result.result.current.prepareForExport()).toThrow();
551551
});
552552
});
553553

src/types/Slices.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type CanvasStore = CanvasState & {
5656
changeX: (payload: number) => void;
5757
changeY: (payload: number) => void;
5858
toggleReferenceWindow: () => void;
59-
prepareForSave: () => Promise<SavedCanvasProperties>;
59+
prepareForSave: () => SavedCanvasProperties;
6060
prepareForExport: (quality?: number) => Promise<Blob>;
6161
drawCanvas: (canvas: HTMLCanvasElement, layerId?: string) => void;
6262
};

0 commit comments

Comments
 (0)