Skip to content

Commit

Permalink
Added CanvasRenderingContext2DSettings to the readFromCanvas methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Dec 11, 2023
1 parent 1976dd0 commit 5283ee7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/image-magick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,19 @@ export class ImageMagick {
* Read single image frame from canvas.
* @param canvas - The canvas to read the image from.
* @param func - The function that will be invoked with the image.
* @param settings - The {@link CanvasRenderingContext2DSettings} to use when reading the image.
*/
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => TReturnType): TReturnType;
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => TReturnType, settings?: CanvasRenderingContext2DSettings): TReturnType;
/**
* Read single image frame from canvas.
* @param canvas - The canvas to read the image from.
* @param func - The async function that will be invoked with the image.
* @param settings - The {@link CanvasRenderingContext2DSettings} to use when reading the image.
*/
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => Promise<TReturnType>): Promise<TReturnType>;
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => TReturnType | Promise<TReturnType>): TReturnType | Promise<TReturnType> {
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => Promise<TReturnType>, settings?: CanvasRenderingContext2DSettings): Promise<TReturnType>;
static readFromCanvas<TReturnType>(canvas: HTMLCanvasElement, func: (image: IMagickImage) => TReturnType | Promise<TReturnType>, settings?: CanvasRenderingContext2DSettings): TReturnType | Promise<TReturnType> {
return MagickImage._create(image => {
image.readFromCanvas(canvas);
image.readFromCanvas(canvas, settings);
return func(image);
});
}
Expand Down
17 changes: 9 additions & 8 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,9 @@ export interface IMagickImage extends IDisposable {
/**
* Read single image frame from canvas.
* @param canvas - The canvas to read the image from.
* @param settings - The {@link CanvasRenderingContext2DSettings} to use when reading the image.
*/
readFromCanvas(canvas: HTMLCanvasElement): void;
readFromCanvas(canvas: HTMLCanvasElement, settings?: CanvasRenderingContext2DSettings): void;

/**
* Removes the artifact with the specified name.
Expand Down Expand Up @@ -2627,20 +2628,20 @@ export class MagickImage extends NativeInstance implements IMagickImage {
this.readOrPing(false, fileNameOrArrayOrColor, settingsOrWidthOrUndefined, heightOrUndefined);
}

readFromCanvas(canvas: HTMLCanvasElement): void {
readFromCanvas(canvas: HTMLCanvasElement, settings?: CanvasRenderingContext2DSettings): void {
const ctx = canvas.getContext('2d');
if (ctx === null)
return;

const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height, settings);

const settings = new MagickReadSettings();
settings.format = MagickFormat.Rgba;
settings.width = canvas.width;
settings.height = canvas.height;
const readSettings = new MagickReadSettings();
readSettings.format = MagickFormat.Rgba;
readSettings.width = canvas.width;
readSettings.height = canvas.height;

this.useException(exception => {
this.readFromArray(imageData.data, settings, exception);
this.readFromArray(imageData.data, readSettings, exception);
});
}

Expand Down

0 comments on commit 5283ee7

Please sign in to comment.