-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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) remove ts-nocheck from image class #9036
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
42bacdc
save progress
asturur bd67361
image progress
asturur c64fb14
Merge branch 'master' into image-ts-nocheck
asturur d5c7026
mini progress
asturur f490fa7
pass
asturur 2422ba0
Merge branch 'master' into image-ts-nocheck
asturur f9c824a
one test failing
asturur 9f8cb2d
added test
asturur d0c0ada
unsure
asturur 95cd3a6
add changelog
asturur 4d5e203
Merge branch 'master' into image-ts-nocheck
asturur 2f62a04
added a comment
asturur 771ec3f
Merge branch 'image-ts-nocheck' of github.com:fabricjs/fabric.js into…
asturur 46bc13d
Update globalCompositeOperation.js
asturur 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
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,10 +1,9 @@ | ||
// @ts-nocheck | ||
import { getFabricDocument, getEnv } from '../env'; | ||
import type { BaseFilter } from '../filters/BaseFilter'; | ||
import { getFilterBackend } from '../filters/FilterBackend'; | ||
import { SHARED_ATTRIBUTES } from '../parser/attributes'; | ||
import { parseAttributes } from '../parser/parseAttributes'; | ||
import type { TClassProperties, TSize } from '../typedefs'; | ||
import type { TClassProperties, TCrossOrigin, TSize } from '../typedefs'; | ||
import type { Abortable } from '../typedefs'; | ||
import { uid } from '../util/internals/uid'; | ||
import { createCanvasElement } from '../util/misc/dom'; | ||
|
@@ -26,6 +25,7 @@ import type { | |
import type { ObjectEvents } from '../EventTypeDefs'; | ||
import { WebGLFilterBackend } from '../filters/WebGLFilterBackend'; | ||
import { NONE } from '../constants'; | ||
import { getDocumentFromElement } from '../util/dom_misc'; | ||
|
||
// @todo Would be nice to have filtering code not imported directly. | ||
|
||
|
@@ -40,7 +40,6 @@ interface UniqueImageProps { | |
cropX: number; | ||
cropY: number; | ||
imageSmoothing: boolean; | ||
crossOrigin: string | null; | ||
filters: BaseFilter[]; | ||
resizeFilter?: BaseFilter; | ||
} | ||
|
@@ -57,7 +56,7 @@ export const imageDefaultValues: Partial<UniqueImageProps> & | |
|
||
export interface SerializedImageProps extends SerializedObjectProps { | ||
src: string; | ||
crossOrigin: string | null; | ||
crossOrigin: TCrossOrigin; | ||
filters: any[]; | ||
resizeFilter?: any; | ||
cropX: number; | ||
|
@@ -168,7 +167,7 @@ export class Image< | |
|
||
protected declare _element: ImageSource; | ||
protected declare _originalElement: ImageSource; | ||
protected declare _filteredEl: ImageSource; | ||
protected declare _filteredEl?: HTMLCanvasElement; | ||
|
||
static type = 'Image'; | ||
|
||
|
@@ -199,7 +198,7 @@ export class Image< | |
this.setElement( | ||
typeof arg0 === 'string' | ||
? (( | ||
(this.canvas && getElementDocument(this.canvas.getElement())) || | ||
(this.canvas && getDocumentFromElement(this.canvas.getElement())) || | ||
getFabricDocument() | ||
).getElementById(arg0) as ImageSource) | ||
: arg0, | ||
|
@@ -357,12 +356,12 @@ export class Image< | |
* of the instance | ||
*/ | ||
_toSVG() { | ||
const imageMarkup = [], | ||
const imageMarkup: string[] = [], | ||
element = this._element, | ||
x = -this.width / 2, | ||
y = -this.height / 2; | ||
let svgString = [], | ||
strokeSvg, | ||
let svgString: string[] = [], | ||
strokeSvg: string[] = [], | ||
clipPath = '', | ||
imageRendering = ''; | ||
if (!element) { | ||
|
@@ -386,28 +385,21 @@ export class Image< | |
clipPath = ' clip-path="url(#imageCrop_' + clipPathId + ')" '; | ||
} | ||
if (!this.imageSmoothing) { | ||
imageRendering = '" image-rendering="optimizeSpeed'; | ||
imageRendering = ' image-rendering="optimizeSpeed"'; | ||
} | ||
imageMarkup.push( | ||
'\t<image ', | ||
'COMMON_PARTS', | ||
'xlink:href="', | ||
this.getSvgSrc(true), | ||
'" x="', | ||
x - this.cropX, | ||
'" y="', | ||
y - this.cropY, | ||
// we're essentially moving origin of transformation from top/left corner to the center of the shape | ||
// by wrapping it in container <g> element with actual transformation, then offsetting object to the top/left | ||
// so that object's center aligns with container's left/top | ||
'" width="', | ||
element.width || element.naturalWidth, | ||
'" height="', | ||
element.height || element.naturalHeight, | ||
imageRendering, | ||
'"', | ||
clipPath, | ||
'></image>\n' | ||
`xlink:href="${this.getSvgSrc(true)}" x="${x - this.cropX}" y="${ | ||
y - this.cropY | ||
// we're essentially moving origin of transformation from top/left corner to the center of the shape | ||
// by wrapping it in container <g> element with actual transformation, then offsetting object to the top/left | ||
// so that object's center aligns with container's left/top | ||
}" width="${ | ||
element.width || (element as HTMLImageElement).naturalWidth | ||
}" height="${ | ||
element.height || (element as HTMLImageElement).naturalHeight | ||
}"${imageRendering}${clipPath}></image>\n` | ||
); | ||
|
||
if (this.stroke || this.strokeDashArray) { | ||
|
@@ -445,14 +437,14 @@ export class Image< | |
getSrc(filtered?: boolean): string { | ||
const element = filtered ? this._element : this._originalElement; | ||
if (element) { | ||
if (element.toDataURL) { | ||
return element.toDataURL(); | ||
if ((element as HTMLCanvasElement).toDataURL) { | ||
return (element as HTMLCanvasElement).toDataURL(); | ||
} | ||
|
||
if (this.srcFromAttribute) { | ||
return element.getAttribute('src'); | ||
return element.getAttribute('src') || ''; | ||
} else { | ||
return element.src; | ||
return (element as HTMLImageElement).src; | ||
} | ||
} else { | ||
return this.src || ''; | ||
|
@@ -541,15 +533,18 @@ export class Image< | |
|
||
if (filters.length === 0) { | ||
this._element = this._originalElement; | ||
this._filteredEl = null; | ||
// this is unsafe and needs to be rethinkend | ||
this._filteredEl = undefined; | ||
this._filterScalingX = 1; | ||
this._filterScalingY = 1; | ||
return; | ||
} | ||
|
||
const imgElement = this._originalElement, | ||
sourceWidth = imgElement.naturalWidth || imgElement.width, | ||
sourceHeight = imgElement.naturalHeight || imgElement.height; | ||
sourceWidth = | ||
(imgElement as HTMLImageElement).naturalWidth || imgElement.width, | ||
sourceHeight = | ||
(imgElement as HTMLImageElement).naturalHeight || imgElement.height; | ||
|
||
if (this._element === this._originalElement) { | ||
// if the element is the same we need to create a new element | ||
|
@@ -563,7 +558,7 @@ export class Image< | |
// also dereference the eventual resized _element | ||
this._element = this._filteredEl; | ||
this._filteredEl | ||
.getContext('2d') | ||
.getContext('2d')! | ||
.clearRect(0, 0, sourceWidth, sourceHeight); | ||
// we also need to resize again at next renderAll, so remove saved _lastScaleX/Y | ||
this._lastScaleX = 1; | ||
|
@@ -636,8 +631,11 @@ export class Image< | |
// crop values cannot be lesser than 0. | ||
cropX = Math.max(this.cropX, 0), | ||
cropY = Math.max(this.cropY, 0), | ||
elWidth = elementToDraw.naturalWidth || elementToDraw.width, | ||
elHeight = elementToDraw.naturalHeight || elementToDraw.height, | ||
elWidth = | ||
(elementToDraw as HTMLImageElement).naturalWidth || elementToDraw.width, | ||
elHeight = | ||
(elementToDraw as HTMLImageElement).naturalHeight || | ||
elementToDraw.height, | ||
sX = cropX * scaleX, | ||
sY = cropY * scaleY, | ||
// the width height cannot exceed element width/height, starting from the crop offset. | ||
|
@@ -793,16 +791,14 @@ export class Image< | |
options: Abortable = {} | ||
) { | ||
return Promise.all([ | ||
loadImage(src, { ...options, crossOrigin }), | ||
f && enlivenObjects(f, options), | ||
loadImage(src!, { ...options, crossOrigin }), | ||
f && enlivenObjects<BaseFilter>(f, options), | ||
// TODO: redundant - handled by enlivenObjectEnlivables | ||
rf && enlivenObjects([rf], options), | ||
rf && enlivenObjects<BaseFilter>([rf], options), | ||
enlivenObjectEnlivables(object, options), | ||
]).then(([el, filters = [], [resizeFilter] = [], hydratedProps = {}]) => { | ||
return new this(el, { | ||
...object, | ||
src, | ||
crossOrigin, | ||
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 suspect this is the cause of some breakage in image.clone i can't explain. |
||
filters, | ||
resizeFilter, | ||
...hydratedProps, | ||
|
@@ -817,11 +813,11 @@ export class Image< | |
* @param {LoadImageOptions} [options] Options object | ||
* @returns {Promise<Image>} | ||
*/ | ||
static fromURL<T extends TProps<SerializedImageProps>>( | ||
static fromURL( | ||
url: string, | ||
options: T & LoadImageOptions = {} | ||
{ crossOrigin = 'anonymous', signal }: LoadImageOptions = {} | ||
): Promise<Image> { | ||
return loadImage(url, options).then((img) => new this(img, options)); | ||
return loadImage(url, { crossOrigin, signal }).then((img) => new this(img)); | ||
} | ||
|
||
/** | ||
|
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
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.
buuuug
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.
TS helps sometimes