File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { MceImage , MceRect } from '../shapes' ;
2
+
3
+ export function fillImage ( rect : MceRect , sourceImage : HTMLImageElement ) : MceImage {
4
+ const scale = Math . min ( rect . width / sourceImage . width , rect . height / sourceImage . height ) ;
5
+ const cropWidth = rect . width / scale ;
6
+ const cropHeight = rect . height / scale ;
7
+
8
+ // Center the crop area
9
+ const cropY = Math . max ( 0 , ( sourceImage . height - cropHeight ) / 2 ) ;
10
+ const cropX = Math . max ( 0 , ( sourceImage . width - cropWidth ) / 2 ) ;
11
+
12
+ // Center the image in the rect
13
+ const left = rect . left + ( rect . width - sourceImage . width * scale ) / 2 ;
14
+ const top = rect . top + ( rect . height - sourceImage . height * scale ) / 2 ;
15
+
16
+ return new MceImage ( sourceImage , {
17
+ angle : rect . angle ,
18
+ left,
19
+ top,
20
+ width : cropWidth ,
21
+ height : cropHeight ,
22
+ scaleX : scale ,
23
+ scaleY : scale ,
24
+ cropY,
25
+ cropX
26
+ } ) ;
27
+ }
Original file line number Diff line number Diff line change 1
1
export * from './fit-image' ;
2
2
export * from './mce-canvas-replacer' ;
3
3
export * from './stretch-image' ;
4
+ export * from './fill-image' ;
Original file line number Diff line number Diff line change 1
1
import { MceImage , MceRect , MceTextbox } from '../shapes' ;
2
2
import { fitImage } from './fit-image' ;
3
3
import { stretchImage } from './stretch-image' ;
4
+ import { fillImage } from './fill-image' ;
4
5
import { MceStaticCanvas } from '../mce-static-canvas' ;
5
6
import { Object as FabricObject } from 'fabric' ;
6
7
import { loadImage } from './load-image' ;
@@ -36,7 +37,7 @@ export class MceCanvasReplacer {
36
37
* @param mode Mode of fitting image to the rectangle.
37
38
* @returns Promise that resolves when the rect is replaced.
38
39
*/
39
- public async replaceRectToImage ( layer : MceLayer , sourceImage : HTMLImageElement | string , mode : 'stretch' | 'fit' ) : Promise < void > {
40
+ public async replaceRectToImage ( layer : MceLayer , sourceImage : HTMLImageElement | string , mode : 'stretch' | 'fit' | 'fill' ) : Promise < void > {
40
41
const rect = this . objects [ layer . realIndex ] as MceRect ;
41
42
if ( ! rect . visible ) {
42
43
// If the layer is hidden, do nothing.
@@ -54,6 +55,9 @@ export class MceCanvasReplacer {
54
55
case 'fit' :
55
56
image = fitImage ( rect , sourceImage ) ;
56
57
break ;
58
+ case 'fill' :
59
+ image = fillImage ( rect , sourceImage ) ;
60
+ break ;
57
61
default :
58
62
throw new Error ( `Unknown mode: ${ mode } ` ) ;
59
63
}
You can’t perform that action at this time.
0 commit comments