-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Adding objectFit property #51
Open
drijnkels
wants to merge
7
commits into
ValentinH:main
Choose a base branch
from
drijnkels:objectFit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fe2ad54
Fix small JSDoc issue
drijnkels 8d7ac63
Implement objectFit feature, copied from react-easy-crop
drijnkels 9404385
Remove styles from image class & remove cover objectFit option
drijnkels e6ff981
Merge branch 'main' into objectFit
drijnkels a453555
Add the cover option and calculate the best mediaObjectFit value from…
drijnkels ec4bb3d
Fix an issue where the zoom rounding issues would get the zoom to 1.0…
drijnkels 93afa9d
Add a more complete testing page
drijnkels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
import { createEventDispatcher, onDestroy, onMount } from 'svelte' | ||||||
import type { HTMLImgAttributes } from 'svelte/elements' | ||||||
import * as helpers from './helpers' | ||||||
import type { Point, CropShape, Size, DispatchEvents, ImageSize } from './types' | ||||||
import type { Point, CropShape, Size, DispatchEvents, ImageSize, ObjectFit } from './types' | ||||||
|
||||||
export let image: string | ||||||
export let crop: Point = { x: 0, y: 0 } | ||||||
|
@@ -17,6 +17,8 @@ | |||||
export let crossOrigin: HTMLImgAttributes['crossorigin'] = null | ||||||
export let restrictPosition = true | ||||||
export let tabindex: number | undefined = undefined | ||||||
export let objectFit: ObjectFit = 'contain' | ||||||
export let mediaObjectFit: ObjectFit = objectFit | ||||||
|
||||||
let cropperSize: Size | null = null | ||||||
let imageSize: ImageSize = { width: 0, height: 0, naturalWidth: 0, naturalHeight: 0 } | ||||||
|
@@ -29,6 +31,7 @@ | |||||
let rafDragTimeout: number | null = null | ||||||
let rafZoomTimeout: number | null = null | ||||||
|
||||||
const imageObjectFitClass = {"contain": 'image_contain', "cover": "horizontal-cover", "horizontal-cover": "image_horizontal_cover", 'vertical-cover': "image_vertical_cover"}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const dispatch = createEventDispatcher<DispatchEvents>() | ||||||
|
||||||
onMount(() => { | ||||||
|
@@ -63,6 +66,7 @@ | |||||
} | ||||||
|
||||||
const onImgLoad = () => { | ||||||
mediaObjectFit = getObjectFit() | ||||||
computeSizes() | ||||||
emitCropData() | ||||||
} | ||||||
|
@@ -74,6 +78,20 @@ | |||||
return aspect | ||||||
} | ||||||
|
||||||
const getObjectFit = () => { | ||||||
if (objectFit === 'cover') { | ||||||
if (imgEl && containerRect) { | ||||||
const containerAspect = containerRect.width / containerRect.height; | ||||||
const mediaAspect = imgEl.naturalWidth / imgEl.naturalHeight; | ||||||
|
||||||
return mediaAspect < containerAspect ? 'horizontal-cover' : 'vertical-cover' | ||||||
} | ||||||
return 'horizontal-cover' | ||||||
} | ||||||
|
||||||
return objectFit; | ||||||
} | ||||||
|
||||||
const computeSizes = () => { | ||||||
if (imgEl) { | ||||||
imageSize = { | ||||||
|
@@ -180,7 +198,8 @@ | |||||
|
||||||
const onWheel = (e: WheelEvent) => { | ||||||
const point = getMousePoint(e) | ||||||
const newZoom = zoom - (e.deltaY * zoomSpeed) / 200 | ||||||
// const newZoom = zoom - (e.deltaY * zoomSpeed) / 200 // Can give 1.01 due to rounding errors | ||||||
const newZoom = (e.deltaY > 0) ? zoom - (zoomSpeed / 2) : zoom + (zoomSpeed / 2); | ||||||
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that this change is intended as part of this PR. |
||||||
setNewZoom(newZoom, point) | ||||||
} | ||||||
|
||||||
|
@@ -239,10 +258,15 @@ | |||||
//when aspect changes, we reset the cropperSize | ||||||
$: if (imgEl) { | ||||||
cropperSize = cropSize ? cropSize : helpers.getCropSize(imgEl.width, imgEl.height, aspect) | ||||||
const newObjectFit = getObjectFit(); | ||||||
if (newObjectFit !== mediaObjectFit) { | ||||||
mediaObjectFit = newObjectFit; | ||||||
} | ||||||
} | ||||||
|
||||||
// when zoom changes, we recompute the cropped area | ||||||
$: zoom && emitCropData() | ||||||
$: classes = 'image ' + imageObjectFitClass[mediaObjectFit]; | ||||||
</script> | ||||||
|
||||||
<svelte:window on:resize={computeSizes} /> | ||||||
|
@@ -258,7 +282,7 @@ | |||||
> | ||||||
<img | ||||||
bind:this={imgEl} | ||||||
class="image" | ||||||
class={classes} | ||||||
src={image} | ||||||
on:load={onImgLoad} | ||||||
alt="" | ||||||
|
@@ -287,20 +311,34 @@ | |||||
user-select: none; | ||||||
touch-action: none; | ||||||
cursor: move; | ||||||
display: flex; | ||||||
justify-content: center; | ||||||
align-items: center; | ||||||
} | ||||||
|
||||||
.image { | ||||||
max-width: 100%; | ||||||
max-height: 100%; | ||||||
margin: auto; | ||||||
position: absolute; | ||||||
top: 0; | ||||||
bottom: 0; | ||||||
left: 0; | ||||||
right: 0; | ||||||
will-change: transform; | ||||||
} | ||||||
|
||||||
.image_contain { | ||||||
max-width: 100%; | ||||||
max-height: 100%; | ||||||
margin: auto; | ||||||
position: absolute; | ||||||
top: 0; | ||||||
bottom: 0; | ||||||
left: 0; | ||||||
right: 0; | ||||||
} | ||||||
.image_horizontal_cover { | ||||||
width: 100%; | ||||||
height: auto; | ||||||
} | ||||||
.image_vertical_cover { | ||||||
width: auto; | ||||||
height: 100%; | ||||||
} | ||||||
|
||||||
.cropperArea { | ||||||
position: absolute; | ||||||
left: 50%; | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,118 @@ | ||
<script lang="ts"> | ||
import queryString from 'query-string' | ||
import Cropper from '../lib' | ||
import type { ObjectFit } from "$lib/types" | ||
|
||
let crop = { x: 0, y: 0 } | ||
let zoom = 1 | ||
let zoomSpeed = 0.5; | ||
let minZoom = 0.5; | ||
let maxZoom = 3; | ||
let objectFit: ObjectFit = 'contain'; | ||
let mediaObjectFit: ObjectFit; | ||
|
||
let croppedSize = 450; | ||
|
||
const urlArgs = typeof window !== 'undefined' ? queryString.parse(window.location.search) : null | ||
let image = typeof urlArgs?.img === 'string' ? urlArgs.img : '/images/dog.jpeg' // so we can change the image from our tests | ||
</script> | ||
|
||
<Cropper | ||
{image} | ||
bind:crop | ||
bind:zoom | ||
bind:minZoom | ||
bind:maxZoom | ||
on:cropcomplete={e => console.log(e.detail)} | ||
/> | ||
<div class="properties"> | ||
<b>Properties</b><br /> | ||
<ul> | ||
<li>Crop: {crop.x}, {crop.y}</li> | ||
<li>Zoom: {zoom}</li> | ||
<li>Zoomspeed: {zoomSpeed}</li> | ||
<li>objectFit: {mediaObjectFit}</li> | ||
</ul> | ||
</div> | ||
<div class="cropperContainer" style={`width: ${croppedSize}px; height: ${croppedSize}px`}> | ||
<Cropper | ||
{image} | ||
bind:crop | ||
bind:zoom | ||
bind:zoomSpeed | ||
bind:minZoom | ||
bind:maxZoom | ||
bind:objectFit={objectFit} | ||
bind:mediaObjectFit | ||
on:cropcomplete={e => console.log(e.detail)} | ||
/> | ||
</div> | ||
<div class="settingsContainer flex flex-col gap-4"> | ||
<h2>Settings:</h2> | ||
<div class="flex gap-4"> | ||
<div class="setting flex flex-col gap-2"> | ||
<label for="zoomSpeed">zoomSpeed</label> | ||
<input id="zoomSpeed" type="number" bind:value={zoomSpeed} /> | ||
</div> | ||
<div class="setting flex flex-col gap-2"> | ||
<label for="minZoom">minZoom</label> | ||
<input id="minZoom" type="number" bind:value={minZoom} /> | ||
</div> | ||
<div class="setting flex flex-col gap-2"> | ||
<label for="maxZoom">maxZoom</label> | ||
<input id="maxZoom" type="number" bind:value={maxZoom} /> | ||
</div> | ||
</div> | ||
|
||
<div class="flex gap-4"> | ||
<div class="setting flex flex-col gap-2"> | ||
<label for="zoomSpeed">Image</label> | ||
<select bind:value={image}> | ||
<option value="/images/dog.jpeg">horizontal</option> | ||
<option value="/images/cat.jpeg">vertical</option> | ||
</select> | ||
</div> | ||
<div class="setting flex flex-col gap-2"> | ||
<label for="width">Width</label> | ||
<input type="number" bind:value={croppedSize} /> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<style> | ||
.properties{ | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
z-index: 30; | ||
background: rgba(0,0,0,0.3); | ||
padding: 16px; | ||
border-bottom-right-radius: 10px; | ||
color: white; | ||
} | ||
.properties ul{ | ||
padding-left: 20px; | ||
} | ||
.cropperContainer{ | ||
position: relative; | ||
background: lightgray; | ||
margin: 0 auto 16px; | ||
} | ||
.settingsContainer{ | ||
position: relative; | ||
width: 450px; | ||
height: 450px; | ||
margin: auto; | ||
} | ||
.settingsContainer .setting{ | ||
width: 100px; | ||
} | ||
.settingsContainer .setting label{ | ||
|
||
font-weight: bold; | ||
} | ||
.flex{ | ||
display: flex; | ||
} | ||
.flex-col{ | ||
flex-direction: column; | ||
} | ||
.gap-2{ | ||
gap: 8px; | ||
} | ||
.gap-4{ | ||
gap: 16px; | ||
} | ||
</style> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
mediaObjectFit
should be exported as it's an property that is computed internally.