File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ function convertImageToBase64 ( image ) {
2+ const canvas = document . createElement ( 'canvas' ) ;
3+ const context = canvas . getContext ( '2d' ) ;
4+ canvas . width = image . width ;
5+ canvas . height = image . height ;
6+ context . drawImage ( image , 0 , 0 ) ;
7+ return canvas . toDataURL ( 'image/png' ) ; // 根据需要指定输出格式
8+ }
9+
10+ const showImageInConsole = src => {
11+ const img = new Image ( ) ;
12+ img . setAttribute ( 'src' , src ) ;
13+ document . body . appendChild ( img ) ;
14+ img . onload = ( ) => {
15+ const base64 = convertImageToBase64 ( img ) ;
16+ const css = [
17+ "padding: " + ( img . height / 2 - 8 ) + "px " + img . width / 2 + "px;" ,
18+ "line-height: " + img . height + "px;" ,
19+ "background: url(" + base64 + ")no-repeat center / cover;" ,
20+ ] ;
21+ console . log ( "%c " , css . join ( "" ) ) ;
22+ consoleUtil ( base64 ) ;
23+ document . body . removeChild ( img ) ;
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments