Uhfgood

extract characters into strings

Jan 19th, 2023 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create our application instance
  2. var app =
  3.     new PIXI.Application
  4.     (
  5.         {
  6.             width: window.innerWidth,
  7.             height: window.innerHeight,
  8.             backgroundColor: 0x2c3e50
  9.         }
  10.     );
  11.  
  12. document.body.appendChild(app.view);
  13.  
  14. app.loader.add( 'font', 'https://i.imgur.com/AbFrBbV.png' ).load( startup );
  15.  
  16. function startup()
  17. {
  18.     var imageSprite = new PIXI.Sprite(app.loader.resources.font.texture);
  19.     let base64 = app.renderer.plugins.extract.base64( imageSprite );
  20.     console.log( base64 );
  21.     const bwFilter = new PIXI.filters.ColorMatrixFilter();
  22.     bwFilter.greyscale(1);
  23.     imageSprite.filters = [ bwFilter ];
  24.  
  25.     app.stage.addChild( imageSprite );
  26.     app.ticker.add
  27.     (
  28.         () =>
  29.         {
  30.             app.render();
  31.         }
  32.     );
  33.  
  34.     let newSprite = PIXI.Sprite.from( base64 );
  35.     newSprite.tint = 0xFFFF00;
  36.     app.stage.addChild( newSprite );
  37.     const charWidth = 9;
  38.     const charHeight = 16;
  39.     const binaryStrings = [];
  40.  
  41.     let img = imageSprite.texture.baseTexture.resource.source;
  42.     let canvas = document.createElement( "CANVAS" );
  43.     canvas.width = 144; canvas.height = 256;
  44.  
  45.     let ctx = canvas.getContext( '2d' );
  46.     ctx.drawImage( img, 0, 0 );
  47.     console.log( "canvas width: " + canvas.width + ", canvas height: " + canvas.height );
  48.     console.log( "image width: " + imageSprite.width + ", image height: " + imageSprite.height );
  49.     for( let y = 0; y < canvas.height; y += charHeight )
  50.     {
  51.         for( let x = 0; x < canvas.width; x += charWidth )
  52.         {
  53.             let binaryString = '';
  54.             for( let charY = 0; charY < charHeight; charY++ )  
  55.             {
  56.                 for( let charX = 0; charX < charWidth; charX++ )
  57.                 {
  58.                     const pixelData = ctx.getImageData( x + charX, y + charY, 1, 1 ).data;
  59.                     if( pixelData[0] === 0 &&
  60.                         pixelData[1] === 0 &&
  61.                         pixelData[2] === 0 &&
  62.                         pixelData[3] === 255 )
  63.                     {
  64.                         binaryString += '0';
  65.                     }
  66.                     else
  67.                     {
  68.                         binaryString += '1';
  69.                     }
  70.                 }
  71.             }
  72.             binaryStrings.push( binaryString );
  73.             console.log( binaryString.length );
  74.         }
  75.     }
  76.     console.log( binaryStrings.length );
  77.  
  78. }
Add Comment
Please, Sign In to add comment