Skip to content

Commit

Permalink
Merge pull request #13 from guesant/next
Browse files Browse the repository at this point in the history
next
  • Loading branch information
guesant authored Jan 10, 2022
2 parents 8a37a1d + 6b57423 commit 8cc20cc
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Examples
sidebar: auto
---

<style>
<style scoped>
.theme-default-content:not(.custom) {
max-width: 1280px;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fabricjs-object-fit",
"version": "0.0.0-beta.3",
"version": "0.0.0-beta.5",
"description": "CSS-like 'object-fit' and 'object-position' behavior for FabricJS.",
"main": "lib/index.js",
"module": "lib/index.mjs",
Expand Down
55 changes: 35 additions & 20 deletions src/createObjectFitClass.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { fabric } from "fabric";
import { FitMode } from "./enums/FitMode";
import { detachObjectFromGroup } from "./misc/Fabric/detachObjectFromGroup";
import { fabricObjectDefaults } from "./misc/Fabric/fabricObjectDefaults";
import { getEnlivedObject } from "./misc/Fabric/getEnlivedObject";
import { defaultPosition } from "./misc/Position/defaultPosition";
Expand Down Expand Up @@ -81,7 +83,7 @@ export const createObjectFitClass = (ns: IFabricNS): IObjectFitConstructor => {
object.set(resetTransformOptions);
object.setCoords();

object.group?.removeWithUpdate(object);
detachObjectFromGroup(object);

this._object = object;

Expand All @@ -90,15 +92,15 @@ export const createObjectFitClass = (ns: IFabricNS): IObjectFitConstructor => {
}

constructor(
object: fabric.Object | null | undefined,
options: IObjectFitConstructorOptions
object?: fabric.Object | null | undefined,
options: IObjectFitConstructorOptions = {}
) {
super(undefined, { ...fabricObjectDefaults });

const {
mode,
width,
height,
width = NaN,
height = NaN,
mode = FitMode.FILL,
useObjectTransform = true,
enableRecomputeOnScaled = true,
enableRecomputeOnScaling = false,
Expand Down Expand Up @@ -156,13 +158,23 @@ export const createObjectFitClass = (ns: IFabricNS): IObjectFitConstructor => {
}

recompute() {
if (this._objectGroup) {
if (Number.isNaN(this.width)) {
this.width = this._objectGroup.width!;
}

if (Number.isNaN(this.height)) {
this.height = this._objectGroup.height!;
}
}

const { width, height, mode, position } = this;

const currentTransformOptions = this.getCurrentTransformOptions();

this.resetContainer();

if (this._objectGroup) {
if (this._objectGroup && !Number.isNaN(width) && !Number.isNaN(height)) {
const fittedObject = getFittedObject(
this._objectGroup,
{
Expand Down Expand Up @@ -236,12 +248,12 @@ export const createObjectFitClass = (ns: IFabricNS): IObjectFitConstructor => {
const currentObject = this._object;

if (this._objectGroup) {
this._objectGroup.group?.removeWithUpdate(this._objectGroup);
detachObjectFromGroup(this._objectGroup);
this._objectGroup = null;
}

if (this._object) {
this._object.group?.removeWithUpdate(this._object);
detachObjectFromGroup(this._object);

this._object.setCoords();

Expand All @@ -259,17 +271,20 @@ export const createObjectFitClass = (ns: IFabricNS): IObjectFitConstructor => {
return currentObject;
}

toObject(): IObjectFitSerialized {
return ns.util.object.extend((this as any).callSuper("toObject"), {
mode: this.mode,
width: this.width,
height: this.height,
position: {
x: this.position.x?.toJSON(),
y: this.position.y?.toJSON()
},
object: this.object?.toObject()
});
toObject(propertiesToInclude?: string[]): IObjectFitSerialized {
return ns.util.object.extend(
(this as any).callSuper(
"toObject",
["mode", "width", "height"].concat(propertiesToInclude ?? [])
),
{
position: {
x: this.position.x?.toJSON(),
y: this.position.y?.toJSON()
},
object: this.object?.toObject()
}
);
}

static fromObject(objectFitObject: IObjectFitSerialized, callback?: any) {
Expand Down
4 changes: 1 addition & 3 deletions src/misc/Fabric/detachObjectFromGroup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { fabric } from "fabric";

export const detachObjectFromGroup = (object: fabric.Object) => {
if (object.group) {
object.group.removeWithUpdate(object);
}
object.group?.removeWithUpdate(object);
};
2 changes: 1 addition & 1 deletion src/misc/Point/parsePoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const parsePoint = (

default: {
const type = (serializedPoint as IPointSerialized).type;
throw new Error(`The point type "${type}" are not implemented`);
throw new Error(`The point type "${type}" are not implemented.`);
}
}
} catch (_err) {
Expand Down
2 changes: 2 additions & 0 deletions src/misc/getObjectMaskedByRectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const getObjectMaskedByRectangle = (

group.add(object);
group._updateObjectsCoords();

group.clipPath = groupMask;

group.setCoords();

return group;
};
2 changes: 1 addition & 1 deletion src/types/IObjectFit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export interface IObjectFit extends fabric.Group {

recompute(): void;

toObject(): IObjectFitSerialized;
toObject(propertiesToInclude?: string[]): IObjectFitSerialized;
}
4 changes: 2 additions & 2 deletions src/types/IObjectFitConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { IObjectFitSerialized } from "./IObjectFitSerialized";

export interface IObjectFitConstructor {
new (
object: fabric.Object | null | undefined,
options: IObjectFitConstructorOptions
object?: fabric.Object | null | undefined,
options?: IObjectFitConstructorOptions
): IObjectFit;

fromObject(
Expand Down
8 changes: 4 additions & 4 deletions src/types/IObjectFitConstructorOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { IObjectFit } from "..";
import { IObjectFit } from "../types/IObjectFit";
import { IGetFittedObjectOptions } from "./IGetFittedObjectOptions";

export type IObjectFitConstructorOptions = IGetFittedObjectOptions &
Partial<
export type IObjectFitConstructorOptions = Partial<
IGetFittedObjectOptions &
Pick<
IObjectFit,
| "useObjectTransform"
| "enableRecomputeOnScaled"
| "enableRecomputeOnScaling"
>
>;
>;

0 comments on commit 8cc20cc

Please sign in to comment.