/
+page.svelte
541 lines (489 loc) · 16.9 KB
/
+page.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<script lang="ts">
import { base } from '$app/paths';
import { Highlight, HighlightSvelte } from 'svelte-highlight';
import { atomOneDark } from 'svelte-highlight/styles';
import typescript from 'svelte-highlight/languages/typescript';
import {
CropWindow,
defaultValue,
defaultOptions,
type Point,
type CropValue,
type Media,
type Options,
type OverlayOptions
} from '../lib';
const VIDEO: Media = {
content_type: 'video',
url: `${base}/Mountain%20-%208837.mp4`
};
const IMAGE: Media = {
content_type: 'image',
url: `${base}/hintersee-3601004.jpg`
};
let video: boolean = false;
let media = IMAGE;
let options: Options<OverlayOptions> = {
...defaultOptions,
shape: 'round',
crop_window_margin: 30,
overlay_options: {
overlay_color: '#167676AA',
line_color: '#f3f5e866',
show_third_lines: true
}
};
let background_color = '#164646';
let value: CropValue = { ...defaultValue };
let value2: CropValue = { ...defaultValue };
let position: Point = { x: 0, y: 0 };
let crop_window_el: CropWindow<OverlayOptions>;
$: {
position = value.position;
video = video;
media = video ? VIDEO : IMAGE;
}
</script>
<svelte:head>
{@html atomOneDark}
</svelte:head>
<div class="container">
<div class="box">
<h1>
<a
href="https://github.com/sabine/svelte-crop-window"
style="display:flex;align-items:center"
>
<img
src="{base}/logo.svg"
alt="svelte media crop"
style="height:5rem;margin-right:1rem"
/>
svelte-crop-window</a
>
</h1>
<p class="subtitle">
A crop window with touch and mouse gestures to zoom, pan and rotate an image or video.
</p>
<p>Media snaps back to cover the crop area.</p>
</div>
<div class="box">
<p>
Looking for contributions, code review and feedback! Feel free to open an issue on <a
href="https://github.com/sabine/svelte-crop-window">the GitHub repository</a
> or connect with me via the Svelte discord (sabine#8815).
</p>
<p>
This is still in an experimental stage. Changes to the API are to be expected. After
v1.0.0, this package will strictly follow semantic versioning.
</p>
<p>
The code of this demo page is <a
href="https://github.com/sabine/svelte-crop-window/blob/main/src/routes/%2Bpage.svelte"
>here</a
>.
</p>
</div>
<div class="box">
<h2 id="with-controls"><a href="#with-controls">Example with controls</a></h2>
<div style="height:20em; background: {background_color}">
<CropWindow bind:this={crop_window_el} bind:value {media} {options} />
</div>
<button on:click={() => (video = !video)}>{video ? 'video' : 'image'}</button>
<h3>Result</h3>
<div
style="position:relative;height:100px;width:{value.aspect *
100}px; overflow:hidden; border-radius: {options.shape == 'round' ? '50%' : '0'}"
>
{#if video}
<video
style={`transform: translateX(-50%) translateY(-50%) rotate(${
value.rotation || 0
}deg);` +
`height: ${(value.scale || 0.0) * 100}px;` +
`margin-left: ${
(100 * value.aspect) / 2 + (value.position.x || 0) * 100
}px;` +
`margin-top: ${100 / 2 + (value.position.y || 0) * 100}px;
max-width:none;`}
src={media.url}
autoPlay
loop
muted={true}
/>
{:else}
<img
style={`transform: translateX(-50%) translateY(-50%) rotate(${
value.rotation || 0
}deg);` +
`height: ${(value.scale || 0.0) * 100}px;` +
`margin-left: ${
(100 * value.aspect) / 2 + (value.position.x || 0) * 100
}px;` +
`margin-top: ${100 / 2 + (value.position.y || 0) * 100}px;
max-width:none;`}
src={media.url}
alt="result"
/>
{/if}
</div>
<h3>Aspect ratio</h3>
<div>
<button
on:click={() => {
value.aspect = 8;
}}>8:1</button
>
<button
on:click={() => {
value.aspect = 16 / 9;
}}>16:9</button
>
<button
on:click={() => {
value.aspect = 4 / 3;
}}>4:3</button
>
<button
on:click={() => {
value.aspect = 1;
}}>1:1</button
>
<button
on:click={() => {
value.aspect = 3 / 4;
}}>3:4</button
>
</div>
<h3>Scale / Rotation / Position</h3>
<div style="display:grid; grid-template-columns: auto 1fr;column-gap:2rem">
<div>scale</div>
<div>
<input
type="range"
min={0.001}
max="6"
step={0.01}
on:input={(e) => crop_window_el.set_zoom(parseFloat(e.currentTarget.value))}
on:change={(e) => {
crop_window_el.commit();
e.currentTarget.value = value.scale.toString();
}}
value={value.scale}
/>
<!-- Note: For scale, it is necessary to update the value after the component scales
the image to fit. Otherwise, the value of the input element can get out of sync. -->
{Math.round(value.scale * 100) / 100}
</div>
<div>rotation</div>
<div>
<input
type="range"
min={-180}
max="180"
step={1}
on:input={(e) => crop_window_el.set_rotation(parseFloat(e.currentTarget.value))}
on:change={crop_window_el.commit}
value={value.rotation}
/>
{Math.round(value.rotation * 100) / 100}
</div>
<div>x</div>
<input
type="number"
step={0.01}
on:input={() => crop_window_el.set_pan(position)}
on:change={() => {
crop_window_el.commit();
position.x = value.position.x;
}}
bind:value={position.x}
/>
<div>y</div>
<input
type="number"
step={0.01}
on:input={() => crop_window_el.set_pan(position)}
on:change={() => {
crop_window_el.commit();
position.y = value.position.y;
}}
bind:value={position.y}
/>
</div>
<h3>Colors</h3>
<div
style="display:grid; grid-template-columns: auto 1fr; column-gap:2rem; align-items:center;"
>
<div>overlay color</div>
<input type="color" bind:value={options.overlay_options.overlay_color} />
<div>line color</div>
<input type="color" bind:value={options.overlay_options.line_color} />
<div>background color</div>
<input type="color" bind:value={background_color} />
</div>
<h3>Shape</h3>
<div>
<select bind:value={options.shape}>
<option value="rect">rectangle</option>
<option value="round">round</option>
</select>
<select bind:value={options.overlay_options.show_third_lines}>
<option value={true}>show third lines</option>
<option value={false}>don't show third lines</option>
</select>
</div>
</div>
<div class="box">
<h2 id="minimal-code-example"><a href="#minimal-code-example">Minimal code example</a></h2>
<div style="height:20em; background: #222">
<CropWindow bind:value={value2} {media} />
</div>
Note: You must wrap the component with an element that has a determined height (and width), as
the component will always take up 100% of the available width and height.
<div>
<Highlight
language={typescript}
code={`// in script tag
import { CropWindow, defaultValue } from 'svelte-crop-window';
let media = {
content_type: '${video ? 'video' : 'image'}',
url: '${video ? VIDEO.url : IMAGE.url}'
};
let value = { ...defaultValue };
// in the template
<div style="height:20em; background: #222">
<CropWindow bind:value {media} />
</div>
`}
/>
</div>
</div>
<div class="box">
<h2>Props and defaults</h2>
<a href="https://github.com/sabine/svelte-crop-window#props">See here</a>.
<HighlightSvelte
code={`<div style="height:20em">
<CropWindow
bind:value
{media}
{options}
/>
</div>
`}
/>
<Highlight
language={typescript}
code={`const defaultOverlayOptions: OverlayOptions = {
overlay_color: 'rgb(11, 11, 11, 0.7)',
line_color: 'rgba(167, 167, 167, 0.5)',
show_third_lines: true
};
};
const defaultOptions: Options<OverlayOptions> = {
shape: 'rect',
crop_window_margin: 10,
overlay: Overlay,
overlay_options: defaultOverlayOptions,
}
const defaultValue: CropValue = {
position: { x: 0, y: 0 },
aspect: 1.0,
rotation: 0,
scale: 0
};
`}
/>
</div>
<div class="box">
<h2 id="how-to-crop"><a href="#how-to-crop">How to Crop</a></h2>
<div
style="position:relative;height:100px;width:{value.aspect *
100}px; overflow:hidden; border-radius: {options.shape == 'round' ? '50%' : '0'}"
>
{#if video}
<video
style={`transform: translateX(-50%) translateY(-50%) rotate(${
value.rotation || 0
}deg);` +
`height: ${(value.scale || 0.0) * 100}px;` +
`margin-left: ${
(100 * value.aspect) / 2 + (value.position.x || 0) * 100
}px;` +
`margin-top: ${100 / 2 + (value.position.y || 0) * 100}px;
max-width:none;`}
src={media.url}
autoPlay
loop
muted={true}
/>
{:else}
<img
style={`transform: translateX(-50%) translateY(-50%) rotate(${
value.rotation || 0
}deg);` +
`height: ${(value.scale || 0.0) * 100}px;` +
`margin-left: ${
(100 * value.aspect) / 2 + (value.position.x || 0) * 100
}px;` +
`margin-top: ${100 / 2 + (value.position.y || 0) * 100}px;
max-width:none;`}
src={media.url}
alt="result"
/>
{/if}
</div>
<h3>Display in HTML without actually cropping:</h3>
<HighlightSvelte
code={`<div style={\`position:relative; overflow:hidden;
height:{HEIGHT}px; width:{value.aspect * HEIGHT}px;
border-radius: { options.shape == 'round' ? '50%' : '0' }\`}>
<video
style={\`transform: translateX(-50%) translateY(-50%) rotate({value.rotation || 0}deg);
height: {(value.scale || 0.0) * HEIGHT}px;
margin-left: {HEIGHT * value.aspect / 2 + (value.position.x || 0) * HEIGHT}px;
margin-top: {HEIGHT / 2 + (value.position.y || 0) * HEIGHT}px;
max-width:none\`}
src={url}
/>
</div>`}
/>
<h3 id="pseudo-code"><a href="#pseudo-code">Pseudo code to crop</a></h3>
<ol>
<li>
Choose a <code>target_height</code> and calculate the width for the cropped image:
<Highlight
language={typescript}
code={`let target_width = target_height * value.aspect;`}
/>
</li>
<li>
Calculate factor by which to scale:
<Highlight
language={typescript}
code={`let s = value.scale * target_height / media.height;`}
/>
</li>
<li>
Scale media by <code>s</code>:
<Highlight language={typescript} code={`let resized_media = scale(media, s);`} />
</li>
<li>
Rotate media by <code>value.rotation</code>:
<Highlight
language={typescript}
code={`let resized_and_rotated_media =
rotate(resized_media, value.rotation);`}
/>
</li>
<li>
Calculate top left position of area to extract: <Highlight
language={typescript}
code={`let left =
(resized_and_rotated_media.width - target_width) / 2.0
- value.x * target_height;
let top =
(resized_and_rotated_media.height - target_height) / 2.0
- value.y * target_height;`}
/>
</li>
<li>
Extract area:
<Highlight
language={typescript}
code={`let cropped_media =
extract_area(resized_and_rotated_media, left, top,
target_width, target_height);`}
/>
</li>
</ol>
</div>
<div class="box">
<h2 id="what-it-doesnt-do">
<a href="#what-it-doesnt-do">What this component doesn't do</a>
</h2>
<ol>
<li>
Does not modify/crop the image, you have to do that by whatever means make sense for
your application.
</li>
<li>
Doesn't (yet) provide usable controls. Currently, you need to implement your own.
<p>
Similar to the overlay, it would be nice to include some controls to make this
more usable out of the box. Contributions are very welcome.
</p>
</li>
</ol>
</div>
<div class="box">
<h2>Inspiration and Acknowledgements</h2>
<p>
One big inspiration for this component was the Android library
<a href="https://github.com/Yalantis/uCrop">uCrop by Yalantis</a>. What is particularly
valuable is that the developers shared their thought process in
<a
href="https://yalantis.com/blog/how-we-created-ucrop-our-own-image-cropping-library-for-android/"
>this blog post</a
>.
</p>
<p>
Another very helpful resource was <a
href="https://github.com/ValentinH/svelte-easy-crop">svelte-easy-crop</a
> which gave me a basic understanding of how to implement a crop window component in Svelte
(and HTML/JS in general).
</p>
<p>
There's no code being reused between either of these components and this one (all
calculations had to be recreated from textbook math).
</p>
<small>
<p>
Video from shantararam at pixabay: <a
href="https://pixabay.com/videos/mountain-nature-snow-old-mountain-8837/"
>mountain-nature-snow-old-mountain-8837</a
>
</p>
</small>
</div>
</div>
<style>
.container {
max-width: 50em;
margin: 0 auto;
padding: 4rem 1rem;
}
.box {
padding: 1.5rem 0;
}
:global(pre),
p {
padding: 0.5rem 0;
}
ol {
padding-left: 1.5rem;
}
li {
margin: 1rem 0;
}
h1 {
padding: 2rem 0;
}
h2 {
margin-top: 1rem;
font-size: 1.4rem;
padding: 1rem 0;
}
h3 {
padding: 1rem 0;
}
h1 a:hover,
h2 a:hover,
h3 a:hover {
text-decoration: none;
}
*:target * {
background: #37b5b5;
color: black;
}
</style>