@@ -82,7 +82,7 @@ export default function MonochromeMergeClient() {
8282 outputData . data [ i ] = colorValue ;
8383 outputData . data [ i + 1 ] = colorValue ;
8484 outputData . data [ i + 2 ] = colorValue ;
85- outputData . data [ i + 3 ] = alpha ;
85+ outputData . data [ i + 3 ] = alpha * 0.5 ; // Set alpha to 50% of the calculated value
8686 }
8787 return outputData ;
8888 } ;
@@ -97,9 +97,21 @@ export default function MonochromeMergeClient() {
9797 const darkImg = new window . Image ( ) ;
9898
9999 const process = ( ) => {
100- const maxWidth = Math . max ( lightImg . width , darkImg . width ) ;
101- const maxHeight = Math . max ( lightImg . height , darkImg . height ) ;
100+ let maxWidth = Math . max ( lightImg . width , darkImg . width ) ;
101+ let maxHeight = Math . max ( lightImg . height , darkImg . height ) ;
102+ const maxSize = 900 ;
102103
104+ if ( maxWidth > maxSize || maxHeight > maxSize ) {
105+ if ( maxWidth > maxHeight ) {
106+ maxHeight = Math . round ( ( maxSize / maxWidth ) * maxHeight ) ;
107+ maxWidth = maxSize ;
108+ } else {
109+ maxWidth = Math . round ( ( maxSize / maxHeight ) * maxWidth ) ;
110+ maxHeight = maxSize ;
111+ }
112+ }
113+
114+ // Scale and draw light image
103115 const lightCanvas = document . createElement ( "canvas" ) ;
104116 lightCanvas . width = maxWidth ;
105117 lightCanvas . height = maxHeight ;
@@ -124,6 +136,7 @@ export default function MonochromeMergeClient() {
124136 ) ;
125137 const lightScaledData = lightCtx . getImageData ( 0 , 0 , maxWidth , maxHeight ) ;
126138
139+ // Scale and draw dark image
127140 const darkCanvas = document . createElement ( "canvas" ) ;
128141 darkCanvas . width = maxWidth ;
129142 darkCanvas . height = maxHeight ;
@@ -140,31 +153,32 @@ export default function MonochromeMergeClient() {
140153 darkCtx . drawImage ( darkImg , darkX , darkY , darkNewWidth , darkNewHeight ) ;
141154 const darkScaledData = darkCtx . getImageData ( 0 , 0 , maxWidth , maxHeight ) ;
142155
156+ // Process both scaled images
143157 const processedLight = processImageData ( lightScaledData , true ) ;
144158 const processedDark = processImageData ( darkScaledData , false ) ;
145159
160+ // Composite the images by overlaying
146161 const outputCanvas = document . createElement ( "canvas" ) ;
147162 outputCanvas . width = maxWidth ;
148163 outputCanvas . height = maxHeight ;
149164 const outputCtx = outputCanvas . getContext ( "2d" ) ;
150165 if ( ! outputCtx ) return ;
151166
152- const outputData = outputCtx . createImageData ( maxWidth , maxHeight ) ;
167+ // Create temporary canvases to hold the processed ImageData
168+ const tempLightCanvas = document . createElement ( "canvas" ) ;
169+ tempLightCanvas . width = maxWidth ;
170+ tempLightCanvas . height = maxHeight ;
171+ tempLightCanvas . getContext ( "2d" ) ?. putImageData ( processedLight , 0 , 0 ) ;
153172
154- for ( let y = 0 ; y < maxHeight ; y ++ ) {
155- for ( let x = 0 ; x < maxWidth ; x ++ ) {
156- const i = ( y * maxWidth + x ) * 4 ;
157- const isLightPixel = ( x + y ) % 2 === 0 ;
158- const source = isLightPixel ? processedLight : processedDark ;
173+ const tempDarkCanvas = document . createElement ( "canvas" ) ;
174+ tempDarkCanvas . width = maxWidth ;
175+ tempDarkCanvas . height = maxHeight ;
176+ tempDarkCanvas . getContext ( "2d" ) ?. putImageData ( processedDark , 0 , 0 ) ;
159177
160- outputData . data [ i ] = source . data [ i ] ;
161- outputData . data [ i + 1 ] = source . data [ i + 1 ] ;
162- outputData . data [ i + 2 ] = source . data [ i + 2 ] ;
163- outputData . data [ i + 3 ] = source . data [ i + 3 ] ;
164- }
165- }
178+ // Draw the images on top of each other
179+ outputCtx . drawImage ( tempDarkCanvas , 0 , 0 ) ;
180+ outputCtx . drawImage ( tempLightCanvas , 0 , 0 ) ;
166181
167- outputCtx . putImageData ( outputData , 0 , 0 ) ;
168182 setProcessedImage ( outputCanvas . toDataURL ( "image/png" ) ) ;
169183 } ;
170184
0 commit comments