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
17 changes: 16 additions & 1 deletion src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compact, map } from 'lodash';

import visualCenter from './visualCenter.js';
import demoImage from './assets/demo.js';
// import { downloadCenteredImage } from './lib/imglib';
import { downloadCenteredImage } from './lib/imglib.js';

import {
MainHeader,
Expand Down Expand Up @@ -242,6 +242,21 @@ function App() {
</div>
</div>

<div className="txt-center">
<Button
onClick={() =>
downloadCenteredImage(imgSrc || demoImage, {
x: resultLeft,
y: resultTop,
})
}
>
Download
</Button>
</div>

<Space h={1} />

<Inline style={{ gap: 16 }}>
<Checkbox
checked={showGuides}
Expand Down
14 changes: 2 additions & 12 deletions src/lib/imglib.ts → src/lib/imglib.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export function downloadCenteredImage(
src: string,
center: { x: number; y: number }
) {
export function downloadCenteredImage(src, center) {
const { x, y } = center;

const img = new Image();
Expand All @@ -17,22 +14,15 @@ export function downloadCenteredImage(
return;
}

// ctx.drawImage(img, 0, 0);
ctx.drawImage(
img,
x > 0.5 ? 0 : canvas.width - img.width,
y > 0.5 ? 0 : canvas.height - img.height
);

// // draw in the dom
// const img2 = new Image();
// img2.src = canvas.toDataURL('image/png');
// img2.style.border = '1px solid red';
// document.body.appendChild(img2);

const a = document.createElement('a');
a.href = canvas.toDataURL('image/png');
a.download = 'image.png';
a.download = 'image-centered.png';
a.click();
};
}