Skip to content

Commit

Permalink
feat: vue composable
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Apr 2, 2024
1 parent ce06390 commit 9e067cf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 82 deletions.
30 changes: 28 additions & 2 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type BackmojiOptions, type CreateTextRendererOptions, type Renderer, backmoji, createTextRenderer } from "backmoji";
import { type ComputedRef, type MaybeRefOrGetter, computed, toValue } from "vue";
import { type BackmojiOptions, type CreateImageRendererOptions, type CreateTextRendererOptions, type Renderer, backmoji, createImageRenderer, createTextRenderer } from "backmoji";
import { type ComputedRef, type MaybeRefOrGetter, computed, onMounted, ref, toValue } from "vue";

export type BackmojiResult = ReturnType<typeof backmoji>;

Expand All @@ -25,3 +25,29 @@ export function useTextRenderer(text: MaybeRefOrGetter<string>, options?: MaybeR
return createTextRenderer(_text, _options);
});
}

export function useImageLoader(src: MaybeRefOrGetter<string>) {
const img = ref();

const load = async () => {
const _src = toValue(src);
const _img = new Image();
_img.src = _src;
await new Promise((resolve) => {
_img.onload = resolve;
});
img.value = _img;
};

onMounted(load);

return img;
}

export function useImageRenderer(image: MaybeRefOrGetter<HTMLImageElement | undefined>, options?: MaybeRefOrGetter<CreateImageRendererOptions | undefined>) {
return computed(() => {
const _image = toValue(image);
const _options = toValue(options);
return createImageRenderer(_image, _options);
});
}
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"react-dom": "^18.2.0",
"stats.js": "^0.17.0",
"typescript": "^5.4.3",
"vue": "^3.4.21"
"vue": "3.4.0"
},
"devDependencies": {
"@iconify/json": "^2.2.196",
Expand Down
5 changes: 3 additions & 2 deletions playground/src/demos/WithVue.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup lang="ts">
import { ref, watchEffect } from "vue";
import { useBackmoji, useTextRenderer } from "@backmoji/vue";
import { useBackmoji, useImageLoader, useImageRenderer, useTextRenderer } from "@backmoji/vue";
import VueLogo from "/vue.svg?url";
const canvasRef = ref<HTMLCanvasElement>();
const renderer = useTextRenderer("vue");
const image = useImageLoader(VueLogo);
const renderer = useImageRenderer(image);
const backmojiResult = useBackmoji(renderer, canvasRef, {
rowGap: 30,
columnGap: 30,
Expand Down
103 changes: 26 additions & 77 deletions pnpm-lock.yaml

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

0 comments on commit 9e067cf

Please sign in to comment.