@@ -19,6 +19,8 @@ import type {
1919 TCacheCanvasDimensions ,
2020 Abortable ,
2121 TOptions ,
22+ ObjectToCanvasElementOptions ,
23+ ObjectToDataUrlOptions ,
2224} from '../../typedefs' ;
2325import { classRegistry } from '../../ClassRegistry' ;
2426import { runningAnimations } from '../../util/animation/AnimationRegistry' ;
@@ -1335,7 +1337,7 @@ export class FabricObject<
13351337 * @param {Boolean } [options.withoutShadow] Remove current object shadow. Introduced in 2.4.2
13361338 * @return {FabricImage } Object cloned as image.
13371339 */
1338- cloneAsImage ( options : any ) : FabricImage {
1340+ cloneAsImage ( options ?: ObjectToCanvasElementOptions ) : FabricImage {
13391341 const canvasEl = this . toCanvasElement ( options ) ;
13401342 // TODO: how to import Image w/o an import cycle?
13411343 const ImageClass = classRegistry . getClass ( 'image' ) ;
@@ -1356,23 +1358,30 @@ export class FabricObject<
13561358 * @param {Boolean } [options.viewportTransform] Account for canvas viewport transform
13571359 * @return {HTMLCanvasElement } Returns DOM element <canvas> with the FabricObject
13581360 */
1359- toCanvasElement ( options : any = { } ) {
1361+ toCanvasElement ( {
1362+ enableRetinaScaling,
1363+ withoutTransform,
1364+ withoutShadow,
1365+ viewportTransform,
1366+ format,
1367+ multiplier,
1368+ canvasElement,
1369+ ...options
1370+ } : ObjectToCanvasElementOptions = { } ) {
13601371 const origParams = saveObjectTransform ( this ) ,
13611372 originalGroup = this . group ,
13621373 originalShadow = this . shadow ,
1363- abs = Math . abs ,
1364- retinaScaling = options . enableRetinaScaling
1374+ retinaScaling = enableRetinaScaling
13651375 ? Math . max ( config . devicePixelRatio , 1 )
1366- : 1 ,
1367- multiplier = ( options . multiplier || 1 ) * retinaScaling ;
1376+ : 1 ;
13681377 delete this . group ;
1369- if ( options . withoutTransform ) {
1378+ if ( withoutTransform ) {
13701379 resetObjectTransform ( this ) ;
13711380 }
1372- if ( options . withoutShadow ) {
1381+ if ( withoutShadow ) {
13731382 this . shadow = null ;
13741383 }
1375- if ( options . viewportTransform ) {
1384+ if ( viewportTransform ) {
13761385 sendObjectToPlane ( this , this . getViewportTransform ( ) ) ;
13771386 }
13781387
@@ -1389,9 +1398,13 @@ export class FabricObject<
13891398 : this . getObjectScaling ( ) ;
13901399 // consider non scaling shadow.
13911400 shadowOffset . x =
1392- 2 * Math . round ( abs ( shadow . offsetX ) + shadowBlur ) * abs ( scaling . x ) ;
1401+ 2 *
1402+ Math . round ( Math . abs ( shadow . offsetX ) + shadowBlur ) *
1403+ Math . abs ( scaling . x ) ;
13931404 shadowOffset . y =
1394- 2 * Math . round ( abs ( shadow . offsetY ) + shadowBlur ) * abs ( scaling . y ) ;
1405+ 2 *
1406+ Math . round ( Math . abs ( shadow . offsetY ) + shadowBlur ) *
1407+ Math . abs ( scaling . y ) ;
13951408 }
13961409 const width = boundingRect . width + shadowOffset . x ,
13971410 height = boundingRect . height + shadowOffset . y ;
@@ -1404,7 +1417,7 @@ export class FabricObject<
14041417 renderOnAddRemove : false ,
14051418 skipOffscreen : false ,
14061419 } ) ;
1407- if ( options . format === 'jpeg' ) {
1420+ if ( format === 'jpeg' ) {
14081421 canvas . backgroundColor = '#fff' ;
14091422 }
14101423 this . setPositionByOrigin (
@@ -1418,7 +1431,11 @@ export class FabricObject<
14181431 canvas . _objects = [ this ] ;
14191432 this . set ( 'canvas' , canvas ) ;
14201433 this . setCoords ( ) ;
1421- const canvasEl = canvas . toCanvasElement ( multiplier || 1 , options ) ;
1434+ const canvasEl = canvas . toCanvasElement (
1435+ ( multiplier || 1 ) * retinaScaling ,
1436+ options ,
1437+ canvasElement
1438+ ) ;
14221439 this . set ( 'canvas' , originalCanvas ) ;
14231440 this . shadow = originalShadow ;
14241441 if ( originalGroup ) {
@@ -1450,7 +1467,7 @@ export class FabricObject<
14501467 * @param {Boolean } [options.withoutShadow] Remove current object shadow. Introduced in 2.4.2
14511468 * @return {String } Returns a data: URL containing a representation of the object in the format specified by options.format
14521469 */
1453- toDataURL ( options : any = { } ) {
1470+ toDataURL ( options : ObjectToDataUrlOptions = { } ) {
14541471 return toDataURL (
14551472 this . toCanvasElement ( options ) ,
14561473 options . format || 'png' ,
0 commit comments