Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: adds support for patterns to export #133

Merged
merged 3 commits into from
Dec 2, 2024
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
58 changes: 29 additions & 29 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ name: "CodeQL"

on:
push:
branches: [ main ]
branches: [main]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [main]
schedule:
- cron: '35 12 * * 2'
- cron: "35 12 * * 2"

jobs:
analyze:
Expand All @@ -28,40 +28,40 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
language: ["javascript"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/preact-stickerbook",
"version": "5.0.0",
"version": "5.1.0",
"description": "Easily create collage apps that are accessible by default.",
"files": [
"dist/*"
Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Playground

Files in the root of this directory are not shipped as part of the library, use it as development environment to run and test the package.
Files in the root of this directory are not shipped as part of the library, use it as development environment to run and test the package.
6 changes: 3 additions & 3 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Sticker, Stickerbook } from "./lib"
import { addSticker, exportStickerbook } from "./lib/helpers"
import { useStickerbook } from "./lib/use-stickerbook"

import backgroundImage from "./background.jpg"
import backgroundImage2 from "./background-2.png"
import frameImage from "./frame.png"
import foregroundImage from "./foreground.png"
import foregroundImage2 from "./foreground-2.png"
import stickerImage from "./sticker.png"
import patternImage from "./sweet-potato-pattern.jpg"

const CANVAS_SIZE = {
width: 500,
Expand Down Expand Up @@ -50,8 +50,8 @@ export function App() {
],
initialBackgrounds: [
{
image: backgroundImage,
type: "scene",
image: patternImage,
type: "pattern",
},
{
image: backgroundImage2,
Expand Down
Binary file removed src/background.jpg
Binary file not shown.
54 changes: 38 additions & 16 deletions src/lib/helpers/coverCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OverlayType } from "../types"
import { loadUrlAsImage } from "./loadUrlAsImage"

interface CoverCanvasOptions {
Expand All @@ -7,6 +8,7 @@ interface CoverCanvasOptions {
height: number
offsetX?: number
offsetY?: number
type?: OverlayType
}

export async function coverCanvas({
Expand All @@ -16,6 +18,7 @@ export async function coverCanvas({
height,
offsetX = 0.5,
offsetY = 0.5,
type = "scene",
}: CoverCanvasOptions): Promise<void> {
if (!ctx || !(ctx instanceof CanvasRenderingContext2D))
throw Error("`ctx` is required and must be a valid canvas context")
Expand All @@ -34,29 +37,48 @@ export async function coverCanvas({
let outputWidth = width
let outputHeight = height

if (imageRatio > outputRatio) outputWidth = outputHeight * imageRatio
else if (imageRatio < outputRatio)
outputHeight = outputWidth * (imageHeight / imageWidth)

// first we our image/source onto a canvas resized
// to the output size we want and with the correct ratio
const resizedCanvas = document.createElement("canvas")
const resizedCanvasCtx = resizedCanvas.getContext("2d")!

if (type === "scene") {
if (imageRatio > outputRatio) outputWidth = outputHeight * imageRatio
else if (imageRatio < outputRatio)
outputHeight = outputWidth * (imageHeight / imageWidth)
} else {
// backgrounds should base themselves on the size of the stickerbook
// meaning, they should look good with the output size in mind
// so we don't alter their dimensions here, just like we don't
// on the stickerbook preview itself, see size matching there
// https://github.com/wethegit/preact-stickerbook/blob/4f33afc7ac1fb25090d2f799798b104797758229/src/lib/stickerbook.tsx
outputWidth = imageWidth
outputHeight = imageHeight
}

resizedCanvas.width = outputWidth
resizedCanvas.height = outputHeight
resizedCanvasCtx.drawImage(loadedImage, 0, 0, outputWidth, outputHeight)

// now we draw the scaled canvas onto the original context passed
ctx.drawImage(
resizedCanvas,
(outputWidth - width) * offsetX,
(outputHeight - height) * offsetY,
width,
height,
0,
0,
width,
height
)
if (type === "scene") {
// now we draw the scaled canvas onto the original context passed
ctx.drawImage(
resizedCanvas,
(outputWidth - width) * offsetX,
(outputHeight - height) * offsetY,
width,
height,
0,
0,
width,
height
)
} else {
// create a pattern
const pattern = ctx.createPattern(resizedCanvas, "repeat")!

// fill canvas
ctx.fillStyle = pattern
ctx.fillRect(0, 0, width, height)
}
}
1 change: 1 addition & 0 deletions src/lib/helpers/exportStickerbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function exportStickerbook<T extends ExportFormat>({
img: background.image,
width: outputWidth,
height: outputHeight,
type: background.type,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/helpers/reorderSticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export function reorderSticker({
? max
: Math.min(max, currentOrder + 1)
: extreme
? min
: Math.max(min, currentOrder - 1)
? min
: Math.max(min, currentOrder - 1)

// if the order didn't change, than we just return
if (newOrder === currentOrder) return stickers
Expand Down
1 change: 1 addition & 0 deletions src/lib/stickerbook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function Stickerbook({
const bgSize =
(backgroundDetails[i].width * dimensions.width) / outputWidth
return {
backgroundPosition: "left top",
backgroundRepeat: "repeat",
backgroundSize: `${bgSize}px auto`,
}
Expand Down
Binary file added src/sweet-potato-pattern.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading