Add gallery button
This commit is contained in:
parent
15f6a517e7
commit
5ce7625f6b
2 changed files with 47 additions and 21 deletions
|
|
@ -51,27 +51,9 @@ async function waitAtLeast(duration: number, since: number) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onRecord() {
|
async function postImage(image: Blob | File) {
|
||||||
assert(video.value !== null);
|
|
||||||
|
|
||||||
const canvas = document.createElement("canvas");
|
|
||||||
|
|
||||||
const scale = 384 / video.value.videoWidth;
|
|
||||||
canvas.width = video.value.videoWidth * scale;
|
|
||||||
canvas.height = video.value.videoHeight * scale;
|
|
||||||
|
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
assert(ctx !== null);
|
|
||||||
|
|
||||||
ctx.drawImage(video.value, 0, 0, canvas.width, canvas.height);
|
|
||||||
|
|
||||||
const blob = await new Promise<Blob | null>((resolve) => {
|
|
||||||
canvas.toBlob(resolve);
|
|
||||||
});
|
|
||||||
assert(blob !== null);
|
|
||||||
|
|
||||||
const form = new FormData();
|
const form = new FormData();
|
||||||
form.append("image", blob);
|
form.append("image", image);
|
||||||
form.append("caption", new Date().toLocaleString());
|
form.append("caption", new Date().toLocaleString());
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
@ -85,6 +67,31 @@ async function onRecord() {
|
||||||
covered.value = false;
|
covered.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onGallery(file: File) {
|
||||||
|
await postImage(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onRecord() {
|
||||||
|
assert(video.value !== null);
|
||||||
|
const video_ = video.value;
|
||||||
|
|
||||||
|
const canvas = document.createElement("canvas");
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
assert(ctx !== null);
|
||||||
|
|
||||||
|
const scale = 384 / video_.videoWidth;
|
||||||
|
canvas.width = video_.videoWidth * scale; // Yes, slightly redundant
|
||||||
|
canvas.height = video_.videoHeight * scale;
|
||||||
|
ctx.drawImage(video_, 0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
|
const blob = await new Promise<Blob | null>((resolve) => {
|
||||||
|
canvas.toBlob(resolve);
|
||||||
|
});
|
||||||
|
assert(blob !== null);
|
||||||
|
|
||||||
|
await postImage(blob);
|
||||||
|
}
|
||||||
|
|
||||||
async function onFlip() {
|
async function onFlip() {
|
||||||
const facingOpposite = facing.value === "user" ? "environment" : "user";
|
const facingOpposite = facing.value === "user" ? "environment" : "user";
|
||||||
await initStream(facingOpposite);
|
await initStream(facingOpposite);
|
||||||
|
|
@ -98,7 +105,7 @@ onMounted(async () => {
|
||||||
<template>
|
<template>
|
||||||
<video ref="video" :class="{ mirrored }" autoplay playsinline></video>
|
<video ref="video" :class="{ mirrored }" autoplay playsinline></video>
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<CPhotoButtonGallery style="visibility: hidden" />
|
<CPhotoButtonGallery @click="onGallery" />
|
||||||
<CPhotoButtonRecord :disabled="stream === undefined" @click="onRecord" />
|
<CPhotoButtonRecord :disabled="stream === undefined" @click="onRecord" />
|
||||||
<CPhotoButtonFlip
|
<CPhotoButtonFlip
|
||||||
:disabled="stream === undefined || facing === undefined"
|
:disabled="stream === undefined || facing === undefined"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,25 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { RiMultiImageFill } from "@remixicon/vue";
|
import { RiMultiImageFill } from "@remixicon/vue";
|
||||||
|
import { useTemplateRef } from "vue";
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
click: [file: File];
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const image = useTemplateRef<HTMLInputElement>("image");
|
||||||
|
|
||||||
|
function onImageChange() {
|
||||||
|
const theFile = image.value?.files?.[0];
|
||||||
|
if (theFile === undefined) return;
|
||||||
|
if (!theFile.type.startsWith("image/")) return;
|
||||||
|
emit("click", theFile);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<label>
|
<label>
|
||||||
<RiMultiImageFill size="48px" />
|
<RiMultiImageFill size="48px" />
|
||||||
|
<input ref="image" type="file" accept="image/*" @change="onImageChange" />
|
||||||
</label>
|
</label>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
@ -27,4 +42,8 @@ label:active {
|
||||||
svg {
|
svg {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue