Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML ><html lang="en"><head><title>
- BUF_VEW
- </title></head><body></body><script> //://///////////////////://
- //://////////////////////////////////////////////////////////://
- var GLOBAL_DATA=( INIT_GLOBAL_DATA( ) );
- function INIT_GLOBAL_DATA( ){
- var til_map=( CreateTileMap(512) );
- var map_wid=( til_map.map_wid );
- var map_hig=( til_map.map_hig );
- var ini_wid = 640 ;
- var ini_hig = 640 ;
- //: [0]: INPUT VIEWPORT:
- var rec_vp0 ={
- x_0 : 0 + 32
- , y_0 : 0 + 32
- , x_1 : ini_wid - 32
- , y_1 : ini_hig - 32
- };; Object.seal( rec_vp0 );
- //: [1]: INPUT VIEWPORT:
- var rec_vp1 ={
- x_0 : 0 + 64
- , y_0 : 0 + 64
- , x_1 : map_wid - 64
- , y_1 : map_hig - 64
- };;Object.seal( rec_vp1 );
- var GLOBAL_DATA={
- til_map : til_map
- , rec_vp0 : rec_vp0
- , rec_vp1 : rec_vp1
- , initial_canvas_wid : ini_wid
- , initial_canvas_hig : ini_hig
- , initial_tilemap_wid : map_wid
- , initial_tilemap_hig : map_hig
- };;
- Object.seal( GLOBAL_DATA );
- return( GLOBAL_DATA );
- };;
- //://////////////////////////////////////////////////////////://
- function CreateTileMap( map_san /** span==wid==hig **/ ){
- var til_map={ /** map_dat **/
- map_u8a : null
- , map_wid : map_san
- , map_hig : map_san
- , map_com : 4 /**NumberOfComponentsPerPixel:ARGB**/
- };;
- Object.seal( til_map );
- var map_len=( 1
- * til_map.map_wid
- * til_map.map_hig
- * til_map.map_com
- );;
- til_map.map_u8a= new Uint8Array( map_len );
- FillTilemapWithTestPattern( til_map );
- return( til_map );
- };;
- //://////////////////////////////////////////////////////////://
- //: Main function to execute once all script is loaded:
- function main(){
- //: Create a [ can / canvas ]:
- var can = attachCanvasToDom();
- //:Has pixel putting and getting functions:
- var canvas_adapter =( NEW_CanvasAdapter( can ) );
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //: REC_MAP --> BUF_VEW, upgrades:
- //: 1. Still mapping one viewport to another.
- //: 2. Source viewport samples from off-screen bitmap.
- //: 3. Off screen pixel values will eventually be
- //: interpreted as tile values in future code tutorials.
- var til_map = GLOBAL_DATA.til_map;
- var rec_src = GLOBAL_DATA.rec_vp1; //: [vp1|vp2|vp3]
- var rec_dst = GLOBAL_DATA.rec_vp0;
- DoMapping(
- canvas_adapter //: <--- rec_dst puts here.
- , til_map //:<----------- rec_src takes from here.
- , rec_src //:source______rectangle
- , rec_dst //:destination_rectangle
- );;
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- };;
- //://////////////////////////////////////////////////////////://
- //:Read data off of til_map using rec_dst
- //:and then write to canvas_adapter using rec_src.
- function DoMapping(
- canvas_adapter
- , til_map
- , rec_src //:source______rectangle
- , rec_dst //:destination_rectangle
- ){
- //://////////////////////////////////////////////////://
- if(!canvas_adapter){ throw("[2020_05_07:300PM:A]"); };
- if(!til_map ){ throw("[2020_05_07:300PM:B]"); };
- if(!rec_src ){ throw("[2020_05_07:300PM:C]"); };
- if(!rec_dst ){ throw("[2020_05_07:300PM:D]"); };
- if( rec_src === rec_dst ){
- throw("[Rectangle_Objects_Are_Identical]");
- };;
- //://////////////////////////////////////////////////://
- var rgb_black=[0,0,0,255];
- var rgb=[1,2,3,4];//:rgba
- //:SOURCE:
- var sx0 = rec_src.x_0;
- var sx1 = rec_src.x_1;
- var sy0 = rec_src.y_0;
- var sy1 = rec_src.y_1;
- //:DEST:
- var dx0 = rec_dst.x_0;
- var dx1 = rec_dst.x_1;
- var dy0 = rec_dst.y_0;
- var dy1 = rec_dst.y_1;
- //:Rather than just looping over every pixel of
- //:destination viewport, we will loop through every
- //:pixel of canvas and draw a BLACK pixel if the
- //:point is outside of the viewport to create a
- //:letter-boxing effect.
- var cx0 = 0;
- var cy0 = 0;
- var cx1 = canvas_adapter.GetWid() - 1;
- var cy1 = canvas_adapter.GetHig() - 1;
- //:Loop through every pixel of CANVAS
- //:instead of every pixel of destination
- //:viewport.
- for(var d_x = cx0; d_x <= cx1; d_x++){
- for(var d_y = cy0; d_y <= cy1; d_y++){
- var outside_destination_viewport=( true );
- if( d_x >= rec_dst.x_0 ){ //:LEFT
- if( d_x <= rec_dst.x_1 ){ //:RIGT
- if( d_y >= rec_dst.y_0 ){ //:TOP
- if( d_y <= rec_dst.y_1 ){ //:BOT
- outside_destination_viewport=( false );
- };;};;};;};;
- if( outside_destination_viewport ){
- canvas_adapter.PutPix(d_x,d_y,rgb_black);
- }else{
- //: Percentages (dpx & spy):
- //: dpx = (d_x - dx0) / ( dx1 - dx0 )
- //: spx = (s_x - sx0) / ( sx1 - sx0 )
- //:........................SOLVE_FOR
- //:..........................|||
- //:..........................VVV
- //: (d_x-dx0)/(dx1-dx0) === (s_x-sx0)/(sx1-sx0)
- //:((d_x-dx0)/(dx1-dx0))*(sx1-sx0) === s_x-sx0
- //:(((d_x-dx0)/(dx1-dx0))*(sx1-sx0))+sx0 === s_x
- var s_x =(((d_x-dx0)/(dx1-dx0))*(sx1-sx0))+sx0;
- var s_y =(((d_y-dy0)/(dy1-dy0))*(sy1-sy0))+sy0;
- s_x = Math.floor( s_x );
- s_y = Math.floor( s_y );
- //:canvas_adapter.GetPix(s_x,s_y,rgb);
- //://////////////////////////////////////////://
- //:Convert XY to index:
- var wid = til_map.map_wid;
- var com = til_map.map_com;
- var dex = ( (s_y*com) * wid) + (s_x*com);
- rgb[ 0 ] = til_map.map_u8a[dex+0];
- rgb[ 1 ] = til_map.map_u8a[dex+1];
- rgb[ 2 ] = til_map.map_u8a[dex+2];
- rgb[ 3 ] = til_map.map_u8a[dex+3];
- //://////////////////////////////////////////://
- canvas_adapter.PutPix(d_x,d_y,rgb);
- };; //:[ outside_destination_viewport ? ]
- };;};; //:Next[ d_x , d_y ]
- canvas_adapter.Apply();
- };;//://///////////////////////////////////////:DoMapping://
- //://////////////////////////////////////////////////////////://
- function attachCanvasToDom(){
- //:C: [ can / canvas ] Creation:
- //:CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC://
- //:Create Canvas and append to body: ------------------- ://
- var can = document.createElement('canvas');
- document.body.appendChild(can);
- //: Make canvas non-zero in size, so we can see it:
- can.width = GLOBAL_DATA.initial_canvas_wid;
- can.height= GLOBAL_DATA.initial_canvas_hig;
- //: Get the context, fill [ can / canvas ] to get visual:
- var ctx = can.getContext("2d");
- ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
- ctx.fillRect(0,0,can.width, can.height);
- //:CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC://
- //:Return the canvas that was created:
- return( can );
- };;
- //://////////////////////////////////////////////////////////://
- function NEW_CanvasAdapter( can ){
- var canvas_adapter =( new CanvasAdapterClass( can ) );
- return( canvas_adapter );
- };;
- //://////////////////////////////////////////////////////////://
- //:Canvas Adapter Class:
- function CanvasAdapterClass( can /** canvas **/ ){
- var self=( this );
- //Publicly Exposed Functions
- //:PEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPE://
- this.PutPix = _putPix;
- this.GetPix = _getPix;
- this.GetWid = _getWid;
- this.GetHig = _getHig;
- this.Apply = _apply;
- //:PEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPEPE://
- if(!can){
- throw("[NilCanvasGivenToPenConstruct:2020_05_06]");
- };;
- function _constructor( canvas_adapter /** self **/ ){
- //://////////////////////////////////////////////////://
- //: Setup inclusive bounds:
- var x0 = 0;
- var x1 = can.width - 1;
- var y0 = 0;
- var y1 = can.height -1;
- //: Iterate over entire [can / canvas], and set pixels:
- var rgb=[1,2,3,4];
- for(var x = x0; x <= x1; x++){
- for(var y = y0; y <= y1; y++){
- rgb[0]= x%256;
- rgb[1]= y%256;
- rgb[2]= (x+y)%256;
- rgb[3]= 255;
- canvas_adapter.PutPix( x, y, rgb );
- };;};; //:next X/Y
- //: Show changes on screen:
- canvas_adapter.Apply();
- //://////////////////////////////////////////////////://
- };;
- var _can = can;
- var _ctx = can.getContext("2d");
- //:Cache size of canvas and handles to internal data.
- var _w = _can.width;
- var _h = _can.height;
- var img_dat = _ctx.createImageData(_w,_h); //: _絵資
- var dat_dat = img_dat.data; //: _筆
- function _putPix(x,y,inn_rgb /** rgb:[r,g,b,a] **/ ){
- //:Convert XY to index:
- var dex = ( (y*4) * _w) + (x*4);
- dat_dat[dex+0] = inn_rgb[0];
- dat_dat[dex+1] = inn_rgb[1];
- dat_dat[dex+2] = inn_rgb[2];
- dat_dat[dex+3] = inn_rgb[3];
- };;
- function _getPix(x,y,out_rgb /** rgb:[r,g,b,a] **/ ){
- //:Convert XY to index:
- var dex = ( (y*4) * _w) + (x*4);
- out_rgb[ 0 ] = dat_dat[dex+0];
- out_rgb[ 1 ] = dat_dat[dex+1];
- out_rgb[ 2 ] = dat_dat[dex+2];
- out_rgb[ 3 ] = dat_dat[dex+3];
- };;
- function _getWid(){ return( _w ); };
- function _getHig(){ return( _h ); };
- function _apply(){
- _ctx.putImageData( img_dat, 0,0 );
- };;
- _constructor( self );
- };;
- //://////////////////////////////////////////////////////////://
- function FillTilemapWithTestPattern(
- til_map
- ){
- var num_pix =( 1
- * til_map.map_wid
- * til_map.map_hig
- );;
- if( til_map.map_com != 4 ){
- throw("[LogicCodedFor4ComponentsPerPixel]");
- };;
- for( var pix_dex = 0; pix_dex < num_pix; pix_dex++ ){
- var dex = ( pix_dex * til_map.map_com );
- var wid = til_map.map_wid ;
- var p_x = pix_dex % wid ;
- var p_y = (pix_dex-p_x)/ wid ;
- per_x =( p_x / til_map.map_wid );
- per_y =( p_y / til_map.map_hig );
- var R = Math.floor( per_x * 255 );
- var G = Math.floor( per_y * 255 );
- var B = ( p_x + p_y ) % 256;
- til_map.map_u8a[dex+0] = R; //:R
- til_map.map_u8a[dex+1] = G; //:G
- til_map.map_u8a[dex+2] = B; //:B
- til_map.map_u8a[dex+3] = 255; //:A
- };;
- };;
- //://////////////////////////////////////////////////////////://
- main();
- //://////////////////////////////////////////////////////////://
- //://///////////////////////////////////////:// </script></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement