Browse Source

finalize the photo-taking code, adding comments and removing logging

photo-ratio
Trent Larson 8 months ago
parent
commit
313cd79e60
  1. 1
      project.task.yaml
  2. 70
      src/components/GiftedPhotoDialog.vue

1
project.task.yaml

@ -6,6 +6,7 @@ tasks :
- add to readme - check version, close tabs & restart phone if necessary - add to readme - check version, close tabs & restart phone if necessary
- bug maybe - a new give remembers the previous project - bug maybe - a new give remembers the previous project
- alert & stop if give amount < 0 - alert & stop if give amount < 0
- add warning that all data (except ID) is public
- onboarding video - onboarding video
- .1 on feed, don't show "to someone anonymous" if it's to a project - .1 on feed, don't show "to someone anonymous" if it's to a project

70
src/components/GiftedPhotoDialog.vue

@ -32,8 +32,6 @@
> >
<span>Upload</span> <span>Upload</span>
</button> </button>
<!-- TODO remove, this and console.logs -->
<!--{{ imageHeight }} {{ imageWidth }} {{ imageWarning }}-->
<button <button
@click="retryImage" @click="retryImage"
class="bg-slate-500 hover:bg-slate-700 text-white font-bold py-2 px-4 rounded-full" class="bg-slate-500 hover:bg-slate-700 text-white font-bold py-2 px-4 rounded-full"
@ -133,62 +131,45 @@ export default class GiftedPhotoDialog extends Vue {
async takeImage(/* payload: MouseEvent */) { async takeImage(/* payload: MouseEvent */) {
const cameraComponent = this.$refs.camera as InstanceType<typeof Camera>; const cameraComponent = this.$refs.camera as InstanceType<typeof Camera>;
console.log(
"cameraComponent on snapshot", /**
cameraComponent, * This logic to set the image height & width correctly.
cameraComponent.resolution, * Without it, the portrait orientation ends up with an image that is stretched horizontally.
); * Note that it's the same with raw browser Javascript; see the "drawImage" example below.
this.imageHeight = cameraComponent?.resolution?.height; * Now that I've done it, I can't explain why it works.
this.imageWidth = cameraComponent?.resolution?.width; */
const initialImageRatio = this.imageWidth / this.imageHeight; let imageHeight = cameraComponent?.resolution?.height;
console.log( let imageWidth = cameraComponent?.resolution?.width;
"initialImageRatio", const initialImageRatio = imageWidth / imageHeight;
initialImageRatio,
this.imageWidth,
this.imageHeight,
);
const windowRatio = window.innerWidth / window.innerHeight; const windowRatio = window.innerWidth / window.innerHeight;
console.log(
"windowRatio",
windowRatio,
window.innerWidth,
window.innerHeight,
);
if (initialImageRatio > 1 && windowRatio < 1) { if (initialImageRatio > 1 && windowRatio < 1) {
// the image is wider than it is tall, and the window is taller than it is wide // the image is wider than it is tall, and the window is taller than it is wide
this.imageWarning = "?";
// For some reason, mobile in portrait orientation renders a horizontally-stretched image. // For some reason, mobile in portrait orientation renders a horizontally-stretched image.
// We're gonna force it opposite. // We're gonna force it opposite.
this.imageHeight = cameraComponent?.resolution?.width; imageHeight = cameraComponent?.resolution?.width;
this.imageWidth = cameraComponent?.resolution?.height; imageWidth = cameraComponent?.resolution?.height;
} else if (initialImageRatio < 1 && windowRatio > 1) { } else if (initialImageRatio < 1 && windowRatio > 1) {
// the image is taller than it is wide, and the window is wider than it is tall // the image is taller than it is wide, and the window is wider than it is tall
// Haven't seen this happen, but just in case. // Haven't seen this happen, but we'll do it just in case.
this.imageWarning = "??"; imageHeight = cameraComponent?.resolution?.width;
this.imageHeight = cameraComponent?.resolution?.width; imageWidth = cameraComponent?.resolution?.height;
this.imageWidth = cameraComponent?.resolution?.height;
} }
const newImageRatio = this.imageWidth / this.imageHeight; const newImageRatio = imageWidth / imageHeight;
console.log(
"newImageRatio",
newImageRatio,
this.imageWidth,
this.imageHeight,
);
if (newImageRatio < windowRatio) { if (newImageRatio < windowRatio) {
// the image is a taller ratio than the window, so fit the height first // the image is a taller ratio than the window, so fit the height first
this.imageHeight = window.innerHeight / 2; imageHeight = window.innerHeight / 2;
this.imageWidth = this.imageHeight * newImageRatio; imageWidth = imageHeight * newImageRatio;
} else { } else {
// the image is a wider ratio than the window, so fit the width first // the image is a wider ratio than the window, so fit the width first
this.imageWidth = window.innerWidth / 2; imageWidth = window.innerWidth / 2;
this.imageHeight = this.imageWidth / newImageRatio; imageHeight = imageWidth / newImageRatio;
} }
// The resolution is only necessary because of that mobile portrait-orientation case. // The resolution is only necessary because of that mobile portrait-orientation case.
// But setting this screws up desktop the mobile emulation. // The mobile emulation in a browser shows something stretched vertically, but real devices work fine.
this.blob = await cameraComponent?.snapshot({ this.blob = await cameraComponent?.snapshot({
height: this.imageHeight, height: imageHeight,
width: this.imageWidth, width: imageWidth,
}); // png is default; if that changes, change extension in formData.append }); // png is default; if that changes, change extension in formData.append
if (!this.blob) { if (!this.blob) {
this.$notify( this.$notify(
@ -210,7 +191,8 @@ export default class GiftedPhotoDialog extends Vue {
/**** /****
Here's an approach without a library, which works similarly and looks ugly. Here's an approach to photo capture without a library. It has similar quirks.
Now that we've fixed styling for simple-vue-camera, it's not critical to refactor. Maybe someday.
<button id="start-camera" @click="cameraClicked">Start Camera</button> <button id="start-camera" @click="cameraClicked">Start Camera</button>
<video id="video" width="320" height="240" autoplay></video> <video id="video" width="320" height="240" autoplay></video>

Loading…
Cancel
Save