File size: 8,849 Bytes
80bcac8 25e7739 80bcac8 25e7739 80bcac8 25e7739 80bcac8 25e7739 80bcac8 25e7739 80bcac8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | 'use client'
import { useRef, useEffect, useCallback, useMemo } from 'react'
import { useAppStore, useConvolutionStore, useSettingsStore } from '@/store'
import type { ConvolutionStep } from '@/types'
import { cn } from '@/utils/helpers'
interface ConvolutionCanvasProps {
width?: number
height?: number
className?: string
onInspectClick?: (x: number, y: number) => void
}
export function ConvolutionCanvas({ width = 800, height = 600, className, onInspectClick }: ConvolutionCanvasProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const animationRef = useRef<number | null>(null)
const { originalImageData, processedImageData, convolution } = useAppStore()
const { showOverlay, zoomLevel, panOffset, padding } = useSettingsStore()
const currentStep = convolution.currentStep
const drawCanvas = useCallback(() => {
const canvas = canvasRef.current
const ctx = canvas?.getContext('2d', { willReadFrequently: true })
if (!canvas || !ctx) return
const displayWidth = Math.round(width * zoomLevel)
const displayHeight = Math.round(height * zoomLevel)
canvas.width = displayWidth
canvas.height = displayHeight
ctx.clearRect(0, 0, displayWidth, displayHeight)
ctx.imageSmoothingEnabled = false
// Draw original image as background (dimmed)
if (originalImageData) {
const tempCanvas = document.createElement('canvas')
tempCanvas.width = originalImageData.width
tempCanvas.height = originalImageData.height
const tempCtx = tempCanvas.getContext('2d')
if (tempCtx) {
tempCtx.putImageData(originalImageData, 0, 0)
ctx.globalAlpha = 0.3
ctx.drawImage(
tempCanvas,
0,
0,
originalImageData.width,
originalImageData.height,
panOffset.x,
panOffset.y,
originalImageData.width * zoomLevel,
originalImageData.height * zoomLevel
)
ctx.globalAlpha = 1
}
}
// Draw processed image progressively
if (processedImageData) {
const tempCanvas = document.createElement('canvas')
tempCanvas.width = processedImageData.width
tempCanvas.height = processedImageData.height
const tempCtx = tempCanvas.getContext('2d')
if (tempCtx) {
tempCtx.putImageData(processedImageData, 0, 0)
ctx.drawImage(
tempCanvas,
0,
0,
processedImageData.width,
processedImageData.height,
panOffset.x,
panOffset.y,
processedImageData.width * zoomLevel,
processedImageData.height * zoomLevel
)
}
}
// Draw convolution visualization overlays
if (currentStep && currentStep.x >= 0 && currentStep.y >= 0) {
drawConvolutionOverlay(ctx, currentStep, zoomLevel, panOffset, showOverlay)
}
// Draw progress indicator
if (convolution.status === 'running' || convolution.status === 'paused') {
drawProgressBar(ctx, displayWidth, displayHeight, convolution.progress)
}
}, [
originalImageData,
processedImageData,
currentStep,
convolution.status,
convolution.progress,
zoomLevel,
panOffset,
showOverlay,
width,
height,
])
const handleCanvasClick = useCallback(
(e: React.MouseEvent<HTMLCanvasElement>) => {
if (!onInspectClick) return
const rect = e.currentTarget.getBoundingClientRect()
const canvasX = e.clientX - rect.left
const canvasY = e.clientY - rect.top
// Convert canvas coordinates to image coordinates
const imageX = Math.floor((canvasX - panOffset.x) / zoomLevel)
const imageY = Math.floor((canvasY - panOffset.y) / zoomLevel)
if (imageX >= 0 && imageX < width && imageY >= 0 && imageY < height) {
onInspectClick(imageX, imageY)
}
},
[onInspectClick, panOffset, zoomLevel, width, height]
)
useEffect(() => {
drawCanvas()
}, [drawCanvas])
// Animation loop for smooth rendering
useEffect(() => {
const animate = () => {
drawCanvas()
animationRef.current = requestAnimationFrame(animate)
}
animationRef.current = requestAnimationFrame(animate)
return () => {
if (animationRef.current) cancelAnimationFrame(animationRef.current)
}
}, [drawCanvas])
return (
<div className={cn('relative overflow-hidden bg-gray-100 dark:bg-gray-900 rounded-lg', className)}>
<canvas
ref={canvasRef}
width={width}
height={height}
className="block cursor-crosshair"
style={{ touchAction: 'none' }}
onClick={onInspectClick ? handleCanvasClick : undefined}
/>
</div>
)
}
function drawConvolutionOverlay(
ctx: CanvasRenderingContext2D,
step: ConvolutionStep,
zoom: number,
pan: { x: number; y: number },
showOverlay: {
kernelBoundary: boolean
pixelValues: boolean
multiplications: boolean
summation: boolean
}
) {
const { x, y, inputPatch, kernel, multiplications, sum, normalizedValue, channel } = step
const kernelSize = kernel.length
const halfKernel = Math.floor(kernelSize / 2)
const cellSize = Math.max(20, Math.min(60, 40 * zoom))
const centerX = pan.x + x * zoom
const centerY = pan.y + y * zoom
// Draw kernel boundary
if (showOverlay.kernelBoundary) {
ctx.strokeStyle = 'rgba(59, 130, 246, 0.8)'
ctx.lineWidth = 2
ctx.setLineDash([5, 5])
ctx.strokeRect(
centerX - halfKernel * cellSize * zoom,
centerY - halfKernel * cellSize * zoom,
kernelSize * cellSize * zoom,
kernelSize * cellSize * zoom
)
ctx.setLineDash([])
}
// Draw kernel matrix with values
if (showOverlay.pixelValues || showOverlay.multiplications) {
const fontSize = Math.max(10, Math.min(14, 12 * zoom))
ctx.font = `${fontSize}px monospace`
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
for (let ky = 0; ky < kernelSize; ky++) {
for (let kx = 0; kx < kernelSize; kx++) {
const cellX = centerX + (kx - halfKernel) * cellSize * zoom
const cellY = centerY + (ky - halfKernel) * cellSize * zoom
// Cell background
ctx.fillStyle = 'rgba(255, 255, 255, 0.9)'
ctx.fillRect(cellX - cellSize / 2, cellY - cellSize / 2, cellSize, cellSize)
ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)'
ctx.strokeRect(cellX - cellSize / 2, cellY - cellSize / 2, cellSize, cellSize)
if (showOverlay.multiplications && multiplications[ky]?.[kx] !== undefined) {
// Show multiplication: pixel × kernel
const pixelVal = inputPatch[ky]?.[kx] ?? 0
const kernelVal = kernel[ky]?.[kx] ?? 0
const mult = multiplications[ky][kx]
ctx.fillStyle = '#1e40af'
ctx.fillText(`${pixelVal.toFixed(2)}×${kernelVal.toFixed(2)}`, cellX, cellY - fontSize / 2)
ctx.fillStyle = '#dc2626'
ctx.fillText(`=${mult.toFixed(3)}`, cellX, cellY + fontSize / 2)
} else if (showOverlay.pixelValues) {
// Show kernel value
const kernelVal = kernel[ky]?.[kx] ?? 0
ctx.fillStyle = '#1e40af'
ctx.fillText(kernelVal.toFixed(2), cellX, cellY)
}
}
}
}
// Draw summation display
if (showOverlay.summation) {
const fontSize = Math.max(12, Math.min(16, 14 * zoom))
ctx.font = `bold ${fontSize}px monospace`
ctx.textAlign = 'left'
ctx.textBaseline = 'top'
const sumX = centerX + (halfKernel + 1) * cellSize * zoom
const sumY = centerY - halfKernel * cellSize * zoom
// Background
const padding = 8
const textWidth = ctx.measureText(`Σ = ${sum.toFixed(3)}`).width + padding * 2
const textHeight = fontSize * 3 + padding * 2
ctx.fillStyle = 'rgba(255, 255, 255, 0.95)'
ctx.fillRect(sumX - padding, sumY - padding, textWidth, textHeight)
ctx.strokeStyle = '#3b82f6'
ctx.lineWidth = 2
ctx.strokeRect(sumX - padding, sumY - padding, textWidth, textHeight)
ctx.fillStyle = '#1e40af'
ctx.fillText(`Channel: ${['R', 'G', 'B', 'A'][channel] ?? channel}`, sumX, sumY)
ctx.fillText(`Sum: ${sum.toFixed(3)}`, sumX, sumY + fontSize + 4)
ctx.fillStyle = '#16a34a'
ctx.fillText(`Output: ${normalizedValue.toFixed(3)}`, sumX, sumY + (fontSize + 4) * 2)
}
}
function drawProgressBar(
ctx: CanvasRenderingContext2D,
width: number,
height: number,
progress: number
) {
const barHeight = 4
const barWidth = width * 0.8
const x = (width - barWidth) / 2
const y = height - 20
ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'
ctx.fillRect(x, y, barWidth, barHeight)
ctx.fillStyle = '#3b82f6'
ctx.fillRect(x, y, barWidth * progress, barHeight)
ctx.fillStyle = '#fff'
ctx.font = '12px monospace'
ctx.textAlign = 'center'
ctx.fillText(`${Math.round(progress * 100)}%`, width / 2, y - 4)
} |