forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(TS): Move more utils to TS (fabricjs#8164)
- Loading branch information
1 parent
86bf322
commit 52b8995
Showing
20 changed files
with
311 additions
and
246 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const capValue = (min: number, value: number, max: number) => Math.max(min, Math.min(value, max)); |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fabric } from '../../../HEADER'; | ||
import { ImageFormat } from '../../typedefs'; | ||
/** | ||
* Creates canvas element | ||
* @static | ||
* @memberOf fabric.util | ||
* @return {CanvasElement} initialized canvas element | ||
*/ | ||
export const createCanvasElement = (): HTMLCanvasElement => fabric.document.createElement('canvas'); | ||
|
||
/** | ||
* Creates image element (works on client and node) | ||
* @static | ||
* @memberOf fabric.util | ||
* @return {HTMLImageElement} HTML image element | ||
*/ | ||
export const createImage = () =>fabric.document.createElement('img'); | ||
|
||
/** | ||
* Creates a canvas element that is a copy of another and is also painted | ||
* @param {CanvasElement} canvas to copy size and content of | ||
* @static | ||
* @memberOf fabric.util | ||
* @return {CanvasElement} initialized canvas element | ||
*/ | ||
export const copyCanvasElement = (canvas: HTMLCanvasElement): HTMLCanvasElement => { | ||
const newCanvas = createCanvasElement(); | ||
newCanvas.width = canvas.width; | ||
newCanvas.height = canvas.height; | ||
newCanvas.getContext('2d')?.drawImage(canvas, 0, 0); | ||
return newCanvas; | ||
}; | ||
|
||
/** | ||
* since 2.6.0 moved from canvas instance to utility. | ||
* possibly useless | ||
* @param {CanvasElement} canvasEl to copy size and content of | ||
* @param {String} format 'jpeg' or 'png', in some browsers 'webp' is ok too | ||
* @param {Number} quality <= 1 and > 0 | ||
* @static | ||
* @memberOf fabric.util | ||
* @return {String} data url | ||
*/ | ||
export const toDataURL = (canvasEl: HTMLCanvasElement, format: ImageFormat, quality: number) => canvasEl.toDataURL(`image/${format}`, quality); |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
interface IWithDimensions { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
/** | ||
* Finds the scale for the object source to fit inside the object destination, | ||
* keeping aspect ratio intact. | ||
* respect the total allowed area for the cache. | ||
* @memberOf fabric.util | ||
* @param {Object | fabric.Object} source | ||
* @param {Number} source.height natural unscaled height of the object | ||
* @param {Number} source.width natural unscaled width of the object | ||
* @param {Object | fabric.Object} destination | ||
* @param {Number} destination.height natural unscaled height of the object | ||
* @param {Number} destination.width natural unscaled width of the object | ||
* @return {Number} scale factor to apply to source to fit into destination | ||
*/ | ||
export const findScaleToFit = (source: IWithDimensions, destination: IWithDimensions) => | ||
Math.min(destination.width / source.width, destination.height / source.height); | ||
|
||
/** | ||
* Finds the scale for the object source to cover entirely the object destination, | ||
* keeping aspect ratio intact. | ||
* respect the total allowed area for the cache. | ||
* @memberOf fabric.util | ||
* @param {Object | fabric.Object} source | ||
* @param {Number} source.height natural unscaled height of the object | ||
* @param {Number} source.width natural unscaled width of the object | ||
* @param {Object | fabric.Object} destination | ||
* @param {Number} destination.height natural unscaled height of the object | ||
* @param {Number} destination.width natural unscaled width of the object | ||
* @return {Number} scale factor to apply to source to cover destination | ||
*/ | ||
export const findScaleToCover = (source: IWithDimensions, destination: IWithDimensions) => | ||
Math.max(destination.width / source.width, destination.height / source.height); |
Oops, something went wrong.