Skip to content
Open
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
14 changes: 12 additions & 2 deletions src/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { memo } from "preact/compat";
import { useEffect, useMemo, useReducer, useRef } from "preact/hooks";
import Pixels from "./pixels";
import Pixels, { flatPoint } from "./pixels";
import {
ArrowUpCircle,
MagnifyingGlassMinus,
Expand Down Expand Up @@ -45,6 +45,16 @@ function Canvas({
const background = useMemo(() => Pixels.fromString(pixels), [pixels]);

async function save() {
const cleanPixels = new Pixels();

for (const { x, y, color } of state.pixels) {
const bgColor = background.storage.get(flatPoint({ x, y }));

if (color !== bgColor) {
cleanPixels.storage.set(flatPoint({ x, y }), color);
}
}

const chainId = await client.getChainId();
if (chainId !== client.chain.id) {
await client.switchChain(client.chain);
Expand Down Expand Up @@ -105,7 +115,7 @@ function Canvas({
]),
functionName: "paint",
address: BASEPAINT_ADDRESS,
args: [BigInt(day), brushId, `0x${state.pixels}`],
args: [BigInt(day), brushId, `0x${cleanPixels.toString()}`],
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/pixels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type FlatPoint = number;
type Point2D = { x: number; y: number };

function flatPoint({ x, y }: Point2D) {
export function flatPoint({ x, y }: Point2D) {
return x + y * 10_000;
}

Expand Down