11// add support for cordova-plugin-file
2- const moduleMapper = window . cordova && window . cordova . require ( 'cordova/modulemapper' ) ;
2+ const moduleMapper = typeof window !== 'undefined' && window . cordova && window . cordova . require ( 'cordova/modulemapper' ) ;
33export const CustomFile = ( moduleMapper && moduleMapper . getOriginalSymbol ( window , 'File' ) ) || File ;
44export const CustomFileReader = ( moduleMapper && moduleMapper . getOriginalSymbol ( window , 'FileReader' ) ) || FileReader ;
55/**
@@ -77,17 +77,7 @@ export function loadImage (src) {
7777 * @returns {HTMLCanvasElement }
7878 */
7979export function drawImageInCanvas ( img ) {
80- let canvas
81- let ctx
82- try {
83- canvas = new OffscreenCanvas ( img . width , img . height )
84- ctx = canvas . getContext ( '2d' )
85- } catch ( e ) {
86- canvas = document . createElement ( 'canvas' )
87- ctx = canvas . getContext ( '2d' )
88- }
89- canvas . width = img . width
90- canvas . height = img . height
80+ const [ canvas , ctx ] = getNewCanvasAndCtx ( img . width , img . height )
9181 ctx . drawImage ( img , 0 , 0 , canvas . width , canvas . height )
9282 return canvas
9383}
@@ -121,16 +111,16 @@ export async function drawFileInCanvas (file) {
121111 * @returns {Promise<File|Blob> }
122112 */
123113export async function canvasToFile ( canvas , fileType , fileName , fileLastModified , quality = 1 ) {
124- let compressedFile
114+ let file
125115 if ( typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas ) {
126- compressedFile = await canvas . convertToBlob ( { type : fileType , quality } )
127- compressedFile . name = fileName
128- compressedFile . lastModified = fileLastModified
116+ file = await canvas . convertToBlob ( { type : fileType , quality } )
117+ file . name = fileName
118+ file . lastModified = fileLastModified
129119 } else {
130120 const dataUrl = canvas . toDataURL ( fileType , quality )
131- compressedFile = await getFilefromDataUrl ( dataUrl , fileName , fileLastModified )
121+ file = await getFilefromDataUrl ( dataUrl , fileName , fileLastModified )
132122 }
133- return compressedFile
123+ return file
134124}
135125
136126/**
@@ -184,55 +174,56 @@ export function getExifOrientation (file) {
184174
185175/**
186176 *
187- * @param img
188- * @param canvas
177+ * @param {HTMLCanvasElement } canvas
189178 * @param options
190- * @returns {Promise<[ HTMLCanvasElement, boolean] > }
179+ * @returns {HTMLCanvasElement> }
191180 */
192- export function handleMaxWidthOrHeight ( img , canvas , options ) {
193- const ctx = canvas . getContext ( '2d' )
194-
181+ export function handleMaxWidthOrHeight ( canvas , options ) {
182+ const width = canvas . width
183+ const height = canvas . height
195184 const maxWidthOrHeight = options . maxWidthOrHeight
196- const needToHandle = Number . isInteger ( maxWidthOrHeight ) && ( img . width > maxWidthOrHeight || img . height > maxWidthOrHeight )
185+
186+ const needToHandle = Number . isInteger ( maxWidthOrHeight ) && ( width > maxWidthOrHeight || height > maxWidthOrHeight )
187+
188+ let newCanvas = canvas
189+ let ctx
190+
197191 if ( needToHandle ) {
198- if ( img . width > img . height ) {
199- canvas . width = maxWidthOrHeight
200- canvas . height = ( img . height / img . width ) * maxWidthOrHeight
192+ [ newCanvas , ctx ] = getNewCanvasAndCtx ( width , height )
193+ if ( width > height ) {
194+ newCanvas . width = maxWidthOrHeight
195+ newCanvas . height = ( height / width ) * maxWidthOrHeight
201196 } else {
202- canvas . width = ( img . width / img . height ) * maxWidthOrHeight
203- canvas . height = maxWidthOrHeight
197+ newCanvas . width = ( width / height ) * maxWidthOrHeight
198+ newCanvas . height = maxWidthOrHeight
204199 }
205- } else {
206- canvas . width = img . width
207- canvas . height = img . height
200+ ctx . drawImage ( canvas , 0 , 0 , newCanvas . width , newCanvas . height )
208201 }
209202
210- ctx . drawImage ( img , 0 , 0 , canvas . width , canvas . height )
211- return [ canvas , needToHandle ]
203+ return newCanvas
212204}
213205
214206/**
215207 * followExifOrientation
216208 * source: https://stackoverflow.com/a/40867559/10395024
217209 *
218- * @param {HTMLImageElement } img
219210 * @param {HTMLCanvasElement } canvas
220211 * @param {number } exifOrientation
221212 * @returns {HTMLCanvasElement } canvas
222213 */
223- export function followExifOrientation ( img , canvas , exifOrientation ) {
224- const ctx = canvas . getContext ( '2d' )
225-
214+ export function followExifOrientation ( canvas , exifOrientation ) {
226215 const width = canvas . width
227216 const height = canvas . height
228217
218+ const [ newCanvas , ctx ] = getNewCanvasAndCtx ( width , height )
219+
229220 // set proper canvas dimensions before transform & export
230221 if ( 4 < exifOrientation && exifOrientation < 9 ) {
231- canvas . width = height
232- canvas . height = width
222+ newCanvas . width = height
223+ newCanvas . height = width
233224 } else {
234- canvas . width = width
235- canvas . height = height
225+ newCanvas . width = width
226+ newCanvas . height = height
236227 }
237228
238229 // transform context before drawing image
@@ -247,7 +238,28 @@ export function followExifOrientation (img, canvas, exifOrientation) {
247238 default : break ;
248239 }
249240
250- ctx . drawImage ( img , 0 , 0 , width , height )
241+ ctx . drawImage ( canvas , 0 , 0 , width , height )
251242
252- return canvas
243+ return newCanvas
244+ }
245+
246+ /**
247+ * get new Canvas and it's context
248+ * @param width
249+ * @param height
250+ * @returns {[HTMLCanvasElement, CanvasRenderingContext2D] }
251+ */
252+ export function getNewCanvasAndCtx ( width , height ) {
253+ let canvas
254+ let ctx
255+ try {
256+ canvas = new OffscreenCanvas ( width , height )
257+ ctx = canvas . getContext ( '2d' )
258+ } catch ( e ) {
259+ canvas = document . createElement ( 'canvas' )
260+ ctx = canvas . getContext ( '2d' )
261+ }
262+ canvas . width = width
263+ canvas . height = height
264+ return [ canvas , ctx ]
253265}
0 commit comments