Skip to content

Commit

Permalink
feat: add download button
Browse files Browse the repository at this point in the history
  • Loading branch information
isqua committed Jun 1, 2024
1 parent b7949de commit 3df9201
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<textarea id="title" rows="8">Hello world</textarea>
<label for="font">Font size:</label>
<input id="font" type="number" value="72" />
<button type="button">Download</button>
</form>
<div class="preview">
<svg
Expand Down
9 changes: 9 additions & 0 deletions src/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function download(name: string, content: string) {
const downloadLink = document.createElement("a");
downloadLink.href = content;
downloadLink.download = name;

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
29 changes: 20 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { STRIPE_COLORS } from "./constants";
import { getFontsRules } from "./fonts";
import { SettingsManager } from "./settings";
import { SvgBuilder } from "./svg";
import { download } from "./download";

import "./style.css";

Expand All @@ -19,6 +20,15 @@ if (!form) {

const settings = new SettingsManager(form);

const cover = new SvgBuilder(svg);

cover
.setTheme(settings.theme)
.addStripes(STRIPE_COLORS)
.setYear(new Date().getUTCFullYear())
.setLocation("Пермь")
.setTitle({ text: settings.title, fontSize: settings.fontSize });

settings.onSettingsChange((data) => {
cover.setTitle({
text: data.title,
Expand All @@ -28,14 +38,15 @@ settings.onSettingsChange((data) => {
cover.setTheme(data.theme);
});

const cover = new SvgBuilder(svg);
settings.onDownload(async () => {
const imageData = await cover.createDataUri();

cover
.setTheme(settings.theme)
.addStripes(STRIPE_COLORS)
.setYear(new Date().getUTCFullYear())
.setLocation("Пермь")
.setTitle({ text: settings.title, fontSize: settings.fontSize });
download("sicamp.png", imageData);
});

async function initAsync() {
const rules = await getFontsRules();
cover.injectFonts(rules);
}

const rules = await getFontsRules();
cover.injectFonts(rules);
initAsync();
14 changes: 14 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class SettingsManager {
private readonly themeSelect: HTMLSelectElement;
private readonly textarea: HTMLTextAreaElement;
private readonly fontSizeInput: HTMLInputElement;
private readonly downloadButton: HTMLButtonElement;

constructor(private readonly form: HTMLFormElement) {
const themeSelect =
Expand Down Expand Up @@ -42,6 +43,15 @@ export class SettingsManager {
}

this.fontSizeInput = fontSizeInput;

const downloadButton =
this.form.querySelector<HTMLButtonElement>("button");

if (!downloadButton) {
throw new Error("Cannot find download button");
}

this.downloadButton = downloadButton;
}

onSettingsChange(callback: SettingsChangeHandler) {
Expand All @@ -58,6 +68,10 @@ export class SettingsManager {
this.fontSizeInput.addEventListener("change", handler);
}

onDownload(callback: () => void) {
this.downloadButton.addEventListener("click", callback);
}

get theme() {
return this.themeSelect.value;
}
Expand Down
34 changes: 33 additions & 1 deletion src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const minWidth = 30;
const maxWidth = 90;
const marginLeft = 12;
const marginBottom = 220;
const viewPortHeight = 1280;
const viewPortWidth = 1280;
const viewPortHeight = 720;
const stripesEnd = 180;
const minVisibleWidth = 20;

Expand Down Expand Up @@ -146,6 +147,37 @@ export class SvgBuilder {
return this;
}

async createDataUri() {
return new Promise<string>((resolve, reject) => {
const img = new Image();

img.addEventListener("load", () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d")!;

// Устанавливаем размеры canvas
canvas.width = viewPortWidth;
canvas.height = viewPortHeight;

// Рисуем SVG на canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);

ctx.drawImage(img, 0, 0);

// Преобразуем canvas в PNG-данные
const pngData = canvas.toDataURL("image/png");

resolve(pngData);
});

img.addEventListener("error", reject);

img.src =
"data:image/svg+xml;utf8," +
encodeURIComponent(this.element.outerHTML);
});
}

addStripes(classNames: string[]) {
for (
let y = marginBottom;
Expand Down

0 comments on commit 3df9201

Please sign in to comment.