1- import React , { useState , useRef , useEffect } from 'react' ;
1+ import React , { useState , useRef , useEffect , useCallback } from 'react' ;
22import { Link } from 'react-router-dom' ;
33import { ArrowLeftIcon } from '@phosphor-icons/react' ;
44import { usePalette } from 'color-thief-react' ;
@@ -71,7 +71,7 @@ function ImageToolkitPage() {
7171 }
7272 } ;
7373
74- const toGrayscale = ( imageData ) => {
74+ const toGrayscale = useCallback ( ( imageData ) => {
7575 const data = new Uint8ClampedArray ( imageData . data ) ;
7676 for ( let i = 0 ; i < data . length ; i += 4 ) {
7777 const avg = ( data [ i ] + data [ i + 1 ] + data [ i + 2 ] ) / 3 ;
@@ -80,9 +80,9 @@ function ImageToolkitPage() {
8080 data [ i + 2 ] = avg ;
8181 }
8282 return data ;
83- }
83+ } , [ ] ) ;
8484
85- const sobel = ( imageData ) => {
85+ const sobel = useCallback ( ( imageData ) => {
8686 const width = imageData . width ;
8787 const height = imageData . height ;
8888 const grayscaleData = toGrayscale ( imageData ) ;
@@ -118,9 +118,9 @@ function ImageToolkitPage() {
118118 }
119119 }
120120 return new ImageData ( sobelData , width , height ) ;
121- } ;
121+ } , [ toGrayscale ] ) ;
122122
123- const combine = ( quantized , edges ) => {
123+ const combine = useCallback ( ( quantized , edges ) => {
124124 const quantizedData = quantized . data ;
125125 const edgesData = edges . data ;
126126 const finalData = new Uint8ClampedArray ( quantizedData . length ) ;
@@ -138,9 +138,9 @@ function ImageToolkitPage() {
138138 }
139139 }
140140 return new ImageData ( finalData , quantized . width , quantized . height ) ;
141- }
141+ } , [ ] ) ;
142142
143- const bayerDither = ( imageData ) => {
143+ const bayerDither = useCallback ( ( imageData ) => {
144144 const pixels = new Uint8ClampedArray ( imageData . data ) ;
145145 const width = imageData . width ;
146146 const height = imageData . height ;
@@ -167,9 +167,9 @@ function ImageToolkitPage() {
167167 }
168168 }
169169 return new ImageData ( pixels , width , height ) ;
170- } ;
170+ } , [ ] ) ;
171171
172- const quantizeColors = ( imageData , levels ) => {
172+ const quantizeColors = useCallback ( ( imageData , levels ) => {
173173 const data = new Uint8ClampedArray ( imageData . data ) ;
174174 const factor = 255 / ( levels - 1 ) ;
175175 for ( let i = 0 ; i < data . length ; i += 4 ) {
@@ -178,9 +178,9 @@ function ImageToolkitPage() {
178178 data [ i + 2 ] = Math . round ( Math . round ( data [ i + 2 ] / factor ) * factor ) ;
179179 }
180180 return new ImageData ( data , imageData . width , imageData . height ) ;
181- } ;
181+ } , [ ] ) ;
182182
183- const halftone = ( imageData , gridSize ) => {
183+ const halftone = useCallback ( ( imageData , gridSize ) => {
184184 const width = imageData . width ;
185185 const height = imageData . height ;
186186 const grayscaleData = toGrayscale ( imageData ) ;
@@ -212,9 +212,9 @@ function ImageToolkitPage() {
212212 }
213213 }
214214 return ctx . getImageData ( 0 , 0 , width , height ) ;
215- }
215+ } , [ toGrayscale ] ) ;
216216
217- const posterize = ( imageData , levels ) => {
217+ const posterize = useCallback ( ( imageData , levels ) => {
218218 const data = new Uint8ClampedArray ( imageData . data ) ;
219219 const step = 255 / ( levels - 1 ) ;
220220 for ( let i = 0 ; i < data . length ; i += 4 ) {
@@ -223,9 +223,9 @@ function ImageToolkitPage() {
223223 data [ i + 2 ] = Math . round ( Math . round ( data [ i + 2 ] / step ) * step ) ;
224224 }
225225 return new ImageData ( data , imageData . width , imageData . height ) ;
226- } ;
226+ } , [ ] ) ;
227227
228- const asciiArt = ( imageData , characterRamp ) => {
228+ const asciiArt = useCallback ( ( imageData , characterRamp ) => {
229229 const data = toGrayscale ( imageData ) ;
230230 const { width, height } = imageData ;
231231 let ascii = '' ;
@@ -239,7 +239,7 @@ function ImageToolkitPage() {
239239 ascii += '\n' ;
240240 }
241241 return ascii ;
242- } ;
242+ } , [ toGrayscale ] ) ;
243243
244244 useEffect ( ( ) => {
245245 if ( ! image || ! canvasRef . current ) return ;
@@ -345,7 +345,7 @@ function ImageToolkitPage() {
345345 setAsciiArtOutput ( ascii ) ;
346346 }
347347 } ;
348- } , [ image , activeEffect , blurAmount ] ) ;
348+ } , [ image , activeEffect , blurAmount , asciiArt , bayerDither , combine , halftone , posterize , quantizeColors , sobel ] ) ;
349349
350350 const handleGetColorPalette = ( ) => {
351351 if ( image ) {
0 commit comments