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
2 changes: 1 addition & 1 deletion src/components/ColorWheel/ColorWheel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function ColorWheel(props: ColorWheelProps): ReactNode {
}))
);

const onChange = (color: Color) => changeColor(color.toString());
const onChange = (color: Color) => changeColor(color.toString('hex'));
const wheelColor = parseColor(color).toString("hsl");

const COLOR_WHEEL_OUTER_RADIUS = 80;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Navbar(): ReactNode {

const handleSaveFile = useCallback(async () => {
try {
const { layers, elements } = await prepareForSave();
const { layers, elements } = prepareForSave();

if (layers.length === 0) {
throw new Error("No layers to save. This is a bug.");
Expand Down
21 changes: 10 additions & 11 deletions src/state/slices/canvasSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const createCanvasSlice: StateCreator<
* elements. Therefore, the caller must save the
* layers and elements themselves.
*/
async function prepareForSave(): Promise<SavedCanvasProperties> {
function prepareForSave(): SavedCanvasProperties {
const { layers, elements } = get();

return { layers, elements };
Expand Down Expand Up @@ -270,7 +270,7 @@ export const createCanvasSlice: StateCreator<
return lines;
}

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

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

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

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

if (isPreviewCanvas) {
// If the canvas is a preview canvas, scale it down.
const scale = canvas.width / (canvasWidth * dpi);
const scaleX = canvas.width / (canvasWidth * dpi);
const scaleY = canvas.height / (canvasHeight * dpi);
// Save the current transform state.
ctx.save();

ctx.scale(scale, scale);
ctx.scale(scaleX, scaleY);
}

console.log("drawing...", layers, elements);

for (const element of elements) {
const { x, y, width, height } = element;

Expand Down Expand Up @@ -442,6 +441,9 @@ export const createCanvasSlice: StateCreator<
}
}

if (isPreviewCanvas) {
ctx.restore(); // Restore the previous transform state.
}
// Finally, draw the background behind all elements.
ctx.fillStyle = background;

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

if (isPreviewCanvas) {
ctx.restore(); // Restore the previous transform state.
}
}

return {
width: 400,
height: 400,
mode: "move",
background: "white",
background: "#ffffff",
shape: "rectangle",
shapeMode: "fill",
color: "#000000",
Expand Down
4 changes: 2 additions & 2 deletions src/tests/integration/ColorWheel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("ColorWheel functionality", () => {

// Assert that dispatch was called with the correct action
expect(preloadedState.changeColor).toHaveBeenCalledWith(
mockColor.toString()
mockColor.toString("hex")
);
});

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

// Assert that dispatch was called with the correct action
expect(preloadedState.changeColor).toHaveBeenCalledWith(
mockColor.toString()
mockColor.toString('hex')
);
});

Expand Down
6 changes: 3 additions & 3 deletions src/tests/unit/useStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { CanvasElement, HistoryAction, SliceStores } from "../../types";
import { MODES } from "../../state/store";

const exampleStore: SliceStores = {
background: "white",
background: "#ffffff",
width: 400,
height: 400,
shape: "rectangle",
Expand Down Expand Up @@ -402,7 +402,7 @@ describe("useStore functionality", () => {
result.result.current.setLayers(layers);
result.result.current.setElements(elements);
});
expect(result.result.current.prepareForSave()).resolves.toEqual(expected);
expect(result.result.current.prepareForSave()).toEqual(expected);
});

it("should return a blob for exporting", () => {
Expand Down Expand Up @@ -547,7 +547,7 @@ describe("useStore functionality", () => {
act(() => {
result.result.current.setLayers([]);
});
expect(() => result.result.current.prepareForExport()).rejects.toThrow();
expect(() => result.result.current.prepareForExport()).toThrow();
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/types/Slices.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type CanvasStore = CanvasState & {
changeX: (payload: number) => void;
changeY: (payload: number) => void;
toggleReferenceWindow: () => void;
prepareForSave: () => Promise<SavedCanvasProperties>;
prepareForSave: () => SavedCanvasProperties;
prepareForExport: (quality?: number) => Promise<Blob>;
drawCanvas: (canvas: HTMLCanvasElement, layerId?: string) => void;
};
Expand Down
Loading