LCOV - code coverage report
Current view: top level - packages/layer-editor/src/render - bake.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 39.6 % 53 21
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { CompactRect as Rect } from '@comfyorg/shared-frontend-utils/geometry'
       2              : import type { Transform } from '#/node'
       3              : import type { Bitmap } from './place'
       4              : 
       5            7 : export function placedBounds(t: Transform): Rect {
       6            7 :   const cx = t.x + t.w / 2
       7            7 :   const cy = t.y + t.h / 2
       8            7 :   const cos = Math.cos(t.rotation)
       9            7 :   const sin = Math.sin(t.rotation)
      10            7 :   const hw = t.w / 2
      11            7 :   const hh = t.h / 2
      12            7 :   let minX = Infinity
      13            7 :   let minY = Infinity
      14            7 :   let maxX = -Infinity
      15            7 :   let maxY = -Infinity
      16            7 :   for (const [dx, dy] of [
      17              :     [-hw, -hh],
      18              :     [hw, -hh],
      19              :     [hw, hh],
      20              :     [-hw, hh]
      21              :   ] as const) {
      22           28 :     const px = cx + dx * cos - dy * sin
      23           28 :     const py = cy + dx * sin + dy * cos
      24           28 :     minX = Math.min(minX, px)
      25           28 :     minY = Math.min(minY, py)
      26           28 :     maxX = Math.max(maxX, px)
      27           28 :     maxY = Math.max(maxY, py)
      28              :   }
      29            7 :   const x = Math.floor(minX)
      30            7 :   const y = Math.floor(minY)
      31            7 :   return {
      32              :     x,
      33              :     y,
      34              :     w: Math.max(1, Math.ceil(maxX) - x),
      35              :     h: Math.max(1, Math.ceil(maxY) - y)
      36              :   }
      37              : }
      38              : 
      39            0 : export function isIdentityPlacement(
      40              :   t: Transform,
      41              :   naturalW: number,
      42              :   naturalH: number
      43              : ): boolean {
      44            0 :   return (
      45              :     t.rotation === 0 &&
      46              :     Math.round(t.w) === naturalW &&
      47              :     Math.round(t.h) === naturalH
      48              :   )
      49              : }
      50              : 
      51            0 : export function drawPlacedInto(
      52              :   ctx: CanvasRenderingContext2D,
      53              :   bitmap: Bitmap,
      54              :   t: Transform,
      55              :   originX: number,
      56              :   originY: number
      57              : ): void {
      58            0 :   ctx.save()
      59            0 :   try {
      60            0 :     ctx.translate(t.x + t.w / 2 - originX, t.y + t.h / 2 - originY)
      61            0 :     ctx.rotate(t.rotation)
      62            0 :     ctx.drawImage(bitmap, -t.w / 2, -t.h / 2, t.w, t.h)
      63              :   } finally {
      64            0 :     ctx.restore()
      65              :   }
      66              : }
      67              : 
      68            0 : export function bakePlaced(
      69              :   bitmap: Bitmap,
      70              :   t: Transform
      71              : ): { canvas: HTMLCanvasElement; bounds: Rect } | null {
      72            0 :   const bounds = placedBounds(t)
      73            0 :   const canvas = document.createElement('canvas')
      74            0 :   canvas.width = bounds.w
      75            0 :   canvas.height = bounds.h
      76            0 :   const ctx = canvas.getContext('2d')
      77            0 :   if (!ctx) return null
      78            0 :   ctx.imageSmoothingEnabled = true
      79            0 :   ctx.imageSmoothingQuality = 'high'
      80            0 :   drawPlacedInto(ctx, bitmap, t, bounds.x, bounds.y)
      81            0 :   return { canvas, bounds }
      82              : }
      83              : 
      84            0 : export function bakeMaskInto(
      85              :   maskBitmap: Bitmap,
      86              :   oldTransform: Transform,
      87              :   bounds: Rect,
      88              :   fill: 'white' | 'black'
      89              : ): HTMLCanvasElement | null {
      90            0 :   const canvas = document.createElement('canvas')
      91            0 :   canvas.width = bounds.w
      92            0 :   canvas.height = bounds.h
      93            0 :   const ctx = canvas.getContext('2d')
      94            0 :   if (!ctx) return null
      95            0 :   ctx.fillStyle = fill === 'white' ? '#ffffff' : '#000000'
      96            0 :   ctx.fillRect(0, 0, bounds.w, bounds.h)
      97            0 :   ctx.imageSmoothingEnabled = true
      98            0 :   ctx.imageSmoothingQuality = 'high'
      99            0 :   drawPlacedInto(ctx, maskBitmap, oldTransform, bounds.x, bounds.y)
     100            0 :   return canvas
     101              : }
        

Generated by: LCOV version 2.0-1