Skip to content

Commit

Permalink
change background
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanfrans committed Apr 12, 2024
1 parent 00f924a commit 9db9f40
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 52 deletions.
File renamed without changes.
File renamed without changes
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<html>

<head>
<base href="">
<base href="/">

<title>Hard Bulls - Game Day</title>
<link rel="icon" href="./public/favicon.ico"/>
<link rel="icon" href="/favicon.ico"/>

<script type="module" src="/src/main.ts"></script>
</head>
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hardbulls/gameday",
"version": "0.0.1",
"version": "1.0.0",
"private": false,
"description": "Generate shareable gameday templates.",
"main": "dist/main.mjs",
Expand All @@ -16,9 +16,9 @@
"url": "git+https://github.com/hardbulls/gameday.git"
},
"keywords": [
"baseball",
"scoreboard",
"hard bulls",
"bulls",
"game day",
"baseball"
],
"author": "Arjan Frans",
Expand Down
41 changes: 41 additions & 0 deletions src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,44 @@ export function drawRoundedRightSquare(
ctx.closePath();
ctx.stroke();
}

export async function drawImage(
ctx: CanvasRenderingContext2D,
src: string,
dx: number,
dy: number,
width: number,
height: number,
): Promise<void> {
const image = new Image();

image.src = src;

return new Promise((resolve, reject) => {
image.onload = () => {
// Specify the target width and height
const targetWidth = width; // Change this to your desired width
const targetHeight = height; // Change this to your desired height

// Calculate the scaling factors to cover the specified width and height
const scaleX = targetWidth / image.width;
const scaleY = targetHeight / image.height;

// Choose the larger scaling factor to cover both width and height
const scale = Math.max(scaleX, scaleY);

// Calculate the destination width and height based on the scale
const dWidth = image.width * scale;
const dHeight = image.height * scale;

// Draw the image with the calculated parameters
ctx.drawImage(image, dx, dy, dWidth, dHeight);

resolve();
};

image.onerror = () => {
reject();
};
});
}
2 changes: 1 addition & 1 deletion src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ body {
margin: 0;
width: 100%;
font-family: sans-serif;
background-color: rgb(196, 196, 196);
background-color: #dcdcdc;
display: flex;
}

Expand Down
55 changes: 9 additions & 46 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import Font_Steelfish from "./assets/steelfish.woff2";
import { GamesRepository } from "./games-repository.ts";
import { Game } from "./model/game.ts";
import { convertFileToBase64 } from "./file-to-base64.ts";
import { drawRoundedLeftSquare, drawRoundedRightSquare } from "./draw.ts";
import {
drawImage,
drawRoundedLeftSquare,
drawRoundedRightSquare,
} from "./draw.ts";
import { imageToBlob } from "./image-to-blob.ts";

const BULLS_COLOR = "#e20613";
Expand Down Expand Up @@ -39,7 +43,7 @@ function getImageUrl(name: string) {
}

async function controls(
handleSelect: (url: string) => void,
handleSelect: (url: string) => Promise<void>,
): Promise<HTMLSelectElement> {
const selectBackground = document.createElement("select");

Expand Down Expand Up @@ -104,10 +108,10 @@ async function controls(
selectBackground.add(new Option(name, url));
}

selectBackground.addEventListener("change", (event) => {
selectBackground.addEventListener("change", async (event) => {
const select = event.target as HTMLSelectElement;

handleSelect(select.value ? getImageUrl(select.value) : "");
await handleSelect(select.value ? getImageUrl(select.value) : "");
});

return selectBackground;
Expand All @@ -118,7 +122,7 @@ function download(handleDownload: () => void): HTMLButtonElement {

downloadButton.textContent = "Download Image";

downloadButton.addEventListener("click", (event) => {
downloadButton.addEventListener("click", async (event) => {
event.preventDefault();

handleDownload();
Expand All @@ -127,47 +131,6 @@ function download(handleDownload: () => void): HTMLButtonElement {
return downloadButton;
}

async function drawImage(
ctx: CanvasRenderingContext2D,
src: string,
dx: number,
dy: number,
width: number,
height: number,
): Promise<void> {
const image = new Image();

image.src = src;

return new Promise((resolve, reject) => {
image.onload = () => {
// Specify the target width and height
const targetWidth = width; // Change this to your desired width
const targetHeight = height; // Change this to your desired height

// Calculate the scaling factors to cover the specified width and height
const scaleX = targetWidth / image.width;
const scaleY = targetHeight / image.height;

// Choose the larger scaling factor to cover both width and height
const scale = Math.max(scaleX, scaleY);

// Calculate the destination width and height based on the scale
const dWidth = image.width * scale;
const dHeight = image.height * scale;

// Draw the image with the calculated parameters
ctx.drawImage(image, dx, dy, dWidth, dHeight);

resolve();
};

image.onerror = () => {
reject();
};
});
}

async function renderCanvas(
outputImage: HTMLImageElement,
canvas: HTMLCanvasElement,
Expand Down

0 comments on commit 9db9f40

Please sign in to comment.