From e01b849ea4758ad81d66bf85bbaf3477f0ea20b5 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Fri, 24 Jan 2025 20:38:52 +0100 Subject: [PATCH] Preparations to support canvas el as argument --- src/color-thief.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/color-thief.ts b/src/color-thief.ts index ab2872a..b5317ab 100644 --- a/src/color-thief.ts +++ b/src/color-thief.ts @@ -34,12 +34,21 @@ class CanvasImage { height: number; - constructor ( image: HTMLImageElement ) { - this.canvas = document.createElement( 'canvas' ); - this.context = this.canvas.getContext( '2d' ); - this.width = this.canvas.width = image.naturalWidth; - this.height = this.canvas.height = image.naturalHeight; - this.context.drawImage( image, 0, 0, this.width, this.height ); + constructor ( image?: HTMLImageElement, canvas?: HTMLCanvasElement ) { + if ( image ) { + this.canvas = document.createElement( 'canvas' ); + this.context = this.canvas.getContext( '2d' ); + this.width = this.canvas.width = image.naturalWidth; + this.height = this.canvas.height = image.naturalHeight; + this.context.drawImage( image, 0, 0, this.width, this.height ); + } else if ( canvas ) { + this.canvas = canvas; + this.context = this.canvas.getContext( '2d' ); + this.width = this.canvas.width; + this.height = this.canvas.height; + } else { + throw new Error("One of the two constructor arguments needed"); + } } getImageData = function () {