Skip to content
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

chore(TS): Image #8443

Merged
merged 27 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- chore(TS): migrate Image [#8443](https://github.com/fabricjs/fabric.js/pull/8443)
- chore(TS): migrate Shadow [#8462](https://github.com/fabricjs/fabric.js/pull/8462)
- fix(Itext): show incorrect pointer position after scale changed
- chore(TS): migrate text classes/mixins [#8408](https://github.com/fabricjs/fabric.js/pull/8408)
Expand Down
7 changes: 4 additions & 3 deletions src/filters/2d_backend.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Canvas 2D filter backend.
*/
import type { BaseFilter } from './base_filter.class';
import { T2DPipelineState } from './typedefs';

export class Canvas2dFilterBackend {
Expand All @@ -24,8 +25,8 @@ export class Canvas2dFilterBackend {
* @param {HTMLCanvasElement} targetCanvas The destination for filtered output to be drawn.
*/
applyFilters(
filters: any[],
sourceElement: HTMLImageElement | HTMLCanvasElement,
filters: BaseFilter[],
sourceElement: CanvasImageSource,
sourceWidth: number,
sourceHeight: number,
targetCanvas: HTMLCanvasElement
Expand All @@ -47,7 +48,7 @@ export class Canvas2dFilterBackend {
ctx,
filterBackend: this,
};
filters.forEach(function (filter) {
filters.forEach((filter) => {
filter.applyTo(pipelineState);
});
const { imageData: imageDataPostFilter } = pipelineState;
Expand Down
30 changes: 30 additions & 0 deletions src/filters/FilterBackend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { fabric } from '../../HEADER';
import { config } from '../config';
import { Canvas2dFilterBackend } from './2d_backend.class';
import { WebGLFilterBackend } from './webgl_backend.class';
import { webGLProbe } from './WebGLProbe';

export type FilterBackend = WebGLFilterBackend | Canvas2dFilterBackend;

export function initFilterBackend(): FilterBackend {
webGLProbe.queryWebGL();
if (config.enableGLFiltering && webGLProbe.isSupported(config.textureSize)) {
return new WebGLFilterBackend({ tileSize: config.textureSize });
} else {
return new Canvas2dFilterBackend();
}
}

/**
* @todo refactor to a module w/o assigning to fabric
*/
export function getFilterBackend(): FilterBackend {
if (!fabric.filterBackend) {
fabric.filterBackend = initFilterBackend();
}
return fabric.filterBackend;
}

fabric.Canvas2dFilterBackend = Canvas2dFilterBackend;
fabric.WebglFilterBackend = WebGLFilterBackend;
fabric.initFilterBackend = initFilterBackend;
20 changes: 1 addition & 19 deletions src/filters/WebGLProbe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { fabric } from '../../HEADER';
import { config } from '../config';
import { createCanvasElement } from '../util/misc/dom';
import { Canvas2dFilterBackend } from './2d_backend.class';
import { WebGLFilterBackend } from './webgl_backend.class';

export enum WebGLPrecision {
low = 'lowp',
Expand All @@ -11,7 +8,7 @@ export enum WebGLPrecision {
}

/**
* Lazy initialize WebGL contants
* Lazy initialize WebGL constants
*/
class WebGLProbe {
maxTextureSize?: number;
Expand Down Expand Up @@ -63,18 +60,3 @@ class WebGLProbe {
}

export const webGLProbe = new WebGLProbe();

export function initFilterBackend():
| WebGLFilterBackend
| Canvas2dFilterBackend {
webGLProbe.queryWebGL();
if (config.enableGLFiltering && webGLProbe.isSupported(config.textureSize)) {
return new WebGLFilterBackend({ tileSize: config.textureSize });
} else {
return new Canvas2dFilterBackend();
}
}

fabric.Canvas2dFilterBackend = Canvas2dFilterBackend;
fabric.WebglFilterBackend = WebGLFilterBackend;
fabric.initFilterBackend = initFilterBackend;
3 changes: 2 additions & 1 deletion src/filters/base_filter.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ export abstract class BaseFilter {
* Other filters may need their own version ( ColorMatrix, HueRotation, gamma, ComposedFilter )
* @param {Object} options
**/
isNeutralState(/* options */): boolean {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isNeutralState(options?: any): boolean {
const main = this.mainParameter,
// @ts-ignore ts you are lying
proto = this.__proto__;
Expand Down
2 changes: 1 addition & 1 deletion src/filters/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type T2DPipelineState = {
filterBackend: Canvas2dFilterBackend;
canvasEl: HTMLCanvasElement;
imageData: ImageData;
originalEl: HTMLCanvasElement | HTMLImageElement;
originalEl: CanvasImageSource;
originalImageData?: ImageData;
ctx: CanvasRenderingContext2D;
helpLayer?: HTMLCanvasElement;
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/object_interactivity.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class InteractiveFabricObject extends FabricObject {
* Constructor
* @param {Object} [options] Options object
*/
constructor(options: Record<string, unknown>) {
constructor(options?: Record<string, unknown>) {
super(options);
}

Expand Down
18 changes: 10 additions & 8 deletions src/parser/parseAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import { setStrokeFillOpacity } from './setStrokeFillOpacity';
* @param {Array} attributes Array of attributes to parse
* @return {Object} object containing parsed attributes' names/values
*/
export function parseAttributes(element, attributes, svgUid?: string) {
export function parseAttributes(
element,
attributes,
svgUid?: string
): Record<string, any> {
if (!element) {
return;
return {};
}

let value,
Expand Down Expand Up @@ -65,12 +69,10 @@ export function parseAttributes(element, attributes, svgUid?: string) {
);
}

let normalizedAttr,
normalizedValue,
normalizedStyle = {};
const normalizedStyle = {};
for (const attr in ownAttributes) {
normalizedAttr = normalizeAttr(attr);
normalizedValue = normalizeValue(
const normalizedAttr = normalizeAttr(attr);
const normalizedValue = normalizeValue(
normalizedAttr,
ownAttributes[attr],
parentAttributes,
Expand All @@ -81,7 +83,7 @@ export function parseAttributes(element, attributes, svgUid?: string) {
if (normalizedStyle && normalizedStyle.font) {
parseFontDeclaration(normalizedStyle.font, normalizedStyle);
}
const mergedAttrs = Object.assign(parentAttributes, normalizedStyle);
const mergedAttrs = { ...parentAttributes, ...normalizedStyle };
return svgValidParentsRegEx.test(element.nodeName)
? mergedAttrs
: setStrokeFillOpacity(mergedAttrs);
Expand Down
Loading