Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML ><html lang="en"><head><title>
- DAS_BOR
- </title></head><body></body><script> //://///////////////////://
- //://////////////////////////////////////////////////////////://
- //:GW: Global_Whatever:----------------------------------://
- //:GWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGW:SC[JSGW_VARS]://
- //:Containers organized in initialization order.
- //:GLO_DAT: DATA
- //: Primitive types and structs.
- //: ( struct ~=~ class with no methods )
- //:
- //:GLO_FUN: FUNCTIONS:
- //: Functions in the procedural sense. They are
- //: loose functions that are NOT part of a class.
- //: MEMBER METHODS DO NOT BELONG HERE.
- //: If function put into GLO_FUN is namespaced,
- //: the namespace should be accomplished using
- //: "plain C" style syntax.
- //:
- //: YES: MYNAMESPACE_FunctionName
- //: WRONG: MYNAMESPACE.FunctionName
- //:
- //:GLO_CLA: CLASSES:
- //: Previously named "GLO_OBJ" but the word "object"
- //: is much to vauge. Any data structure that
- //: combines both DATA and FUNCTIONS.
- //:GWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGW:SC[JSGW_VARS]://
- var GLO_DAT = null; //:GLOBAL_DATA
- var GLO_FUN = null; //:GLOBAL_FUNCTIONS
- var GLO_CLA = null; //:GLOBAL_CLASSES (AKA:GLO_OBJ)
- //:GWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGWGW:SC[JSGW_VARS]://
- //://////////////////////////////////////////////////////////://
- //: Main function to execute once all script is loaded:
- function main(){
- "use strict"
- //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII:SC[ JSGW_INIT ]://
- GLO_DAT=( INI_GLO_DAT( ) ); //:DATA
- Object.seal( GLO_DAT ); //:DATA
- GLO_FUN=( INI_GLO_FUN( ) ); //:FUNCTIONS
- Object.seal( GLO_FUN ); //:FUNCTIONS
- GLO_CLA=( INI_GLO_CLA( ) ); //:CLASSES/OBJECTS:AKA:GLO_OBJ
- Object.seal( GLO_CLA ); //:CLASSES/OBJECTS:AKA:GLO_OBJ
- //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII:SC[ JSGW_INIT ]://
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //[ BUF_VEW --> UIN_ADA, upgrades: ]//
- //[ 1:When pressing arrow keys, the camera will move ]//
- //[ exactly one camera in the direction of arrow press.]//
- //[ 2:Pressing "+" or "-" keys will zoom in or out ]//
- //[ by doubling or halving off-screen viewport size. ]//
- //:GLO_FUN.DoMapping_vp1_TO_vp0();
- GLO_CLA.vp1_sna.ForceUpdate();
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- //:******************************************************://
- };;
- //://////////////////////////////////////////////////////////://
- //:GIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGI:SC[JSGW_BODY]://
- //: ://
- //: ORDER: ://
- //: 1. INI_GLO_DAT ( DATA / PRIMITIVES / STRUCTS ) ://
- //: 2. INI_GLO_FUN ( FUNCTIONS / PROCEDURES ) ://
- //: 3. INI_GLO_CLA ( CLASSES / OBJECTS ) ://
- //: ://
- //:GIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGI:SC[JSGW_BODY]://
- //://////////////////////////////////////////////////////://
- function INI_GLO_DAT( ){ //://///////////////////////://
- "use strict" //://///////////////////////://
- //://////////////////////////////////////////////////////://
- var til_map=( CreateTileMap(512) );
- var ini_til_wid=( til_map.map_wid );
- var ini_til_hig=( til_map.map_hig );
- var ini_can_wid = 640 ; //:ini_wid:of:Canvas
- var ini_can_hig = 640 ; //:ini_hig:of:Canvas
- //: [0]: INPUT VIEWPORT:
- var rec_vp0 ={
- x_0 : 0 + 32
- , y_0 : 0 + 32
- , x_1 : ini_can_wid - 32
- , y_1 : ini_can_hig - 32
- };; Object.seal( rec_vp0 );
- //: [1]: INPUT VIEWPORT:
- var rec_vp1 ={
- x_0 : 0 + 64
- , y_0 : 0 + 64
- , x_1 : ini_til_wid - 64
- , y_1 : ini_til_hig - 64
- };;Object.seal( rec_vp1 );
- var GLO_DAT={
- til_map : til_map
- , rec_vp0 : rec_vp0
- , rec_vp1 : rec_vp1
- , ini_can_wid : ini_can_wid //:initial_canvas_wid
- , ini_can_hig : ini_can_hig //:initial_canvas_hig
- , ini_til_wid : ini_til_wid //:initial_tilemap_wid
- , ini_til_hig : ini_til_hig //:initial_tilemap_hig
- , glo_eve : NEW_GlobalEventStruct( )
- };;
- Object.seal( GLO_DAT );
- return( GLO_DAT );
- //://////////////////////////////////////////////////////://
- };;function INI_GLO_FUN( ){ //://///////////////////////://
- "use strict" //://///////////////////////://
- //://////////////////////////////////////////////////////://
- var GLO_FUN ={ //:GLO_FUN:GLObal_FUNctions (container )
- //::::::::vp1_TO_vp0::::::::::::::::::vp1_TO_vp0
- DoMapping_vp1_TO_vp0 : SOTM_DoMapping_vp1_TO_vp0
- };;
- Object.seal( GLO_FUN );
- return( GLO_FUN );
- //://////////////////////////////////////////////////////://
- };;function INI_GLO_CLA( ){ //://///////////////////////://
- "use strict" //://///////////////////////://
- //://////////////////////////////////////////////////////://
- //: Create a [ can / canvas ]:
- var can = attachCanvasToDom(
- GLO_DAT.ini_can_wid
- , GLO_DAT.ini_can_hig
- );;
- var GLO_CLA={
- uin_ada : NEW_UserInputAdapterSingleton( can )
- , can_ada : NEW_CanvasAdapter( can )
- , vp1_sna : NEW_ViewportOneSnapper( )
- };;
- //: can_ada: Has pixel putting and getting functions:
- Object.seal( GLO_CLA );
- return( GLO_CLA );
- };; //://////////////////////////////////////////////////://
- //://////////////////////////////////////////////////////://
- //:GIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGIGI:SC[JSGW_BODY]://
- //:GI:Global_Initialization:(ABOVE):-------------------------://
- //://////////////////////////////////////////////////////////://
- function NEW_ViewportOneSnapper( ){ //:GLO_CLA.vp1_sna
- "use strict"
- var vp1_sna =( new ViewportOneSnapper( ) );
- Object.seal( vp1_sna );
- return( vp1_sna );
- };;
- function ViewportOneSnapper( ){ //: vp1_sna
- "use strict"
- //:V:Visible (Exported/Public) Functions:
- //:VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV://
- this.ReadEvent = ( _ReadEvent );
- this.ForceUpdate = ( _ForceUpdate );
- //:VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV://
- //:H:Hidden_Variables:
- //:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH://
- var _ = {};
- _.til_exp = 7; //:Tile_Exponent/ViewportSize
- _.t_x = 1; //:Tile_X:Coordinate_Of_VP1_Position
- _.t_y = 1; //:Tile_Y:Coordinate_Of_VP1_Position
- Object.seal( _ );
- //:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH://
- function _UpdateGraphics( ){
- console.log("[TODO:_UpdateGraphics]");
- GLO_FUN.DoMapping_vp1_TO_vp0();
- };;
- function _UpdateCoordinates_VP1( ){
- var map_wid = GLO_DAT.til_map.map_wid ; //:pix_wid
- var map_hig = GLO_DAT.til_map.map_hig ; //:pix_hig
- if( map_wid != map_hig ){
- throw("[DesignedForPerfectlySquareTileMaps]");
- };;
- var map_san = ( map_wid | map_hig );
- var max_exp = ( Math.log2( map_san ) );
- if( _.til_exp < 0 ){ _.til_exp = 0 ; };
- if( _.til_exp > max_exp ){ _.til_exp = max_exp; };
- var til_san=( Math.pow( 2, _.til_exp ) );//:pix_san
- //://////////////////////////////////////////////////://
- //:How big is the tilemap when measured in terms
- //:of cameras stacked ajacently next to each other
- //:until they fill entire til_map 100% without
- //:any overlap.
- //:SUMMARY:
- //: How big is the grid in camera_tiles.
- //: ( camera_tiles === til_san )
- var gri_wid = map_wid / til_san ; //:grid_wid
- var gri_hig = map_hig / til_san ; //:grid_hig
- //://////////////////////////////////////////////////://
- var m_x = ( gri_wid - 1 ); //:max_index:t_x
- var m_y = ( gri_hig - 1 ); //:max_index:t_y
- if( _.t_x < 0 ){ _.t_x = 0; }; //:LOWER_BOUND : t_x
- if( _.t_y < 0 ){ _.t_y = 0; }; //:LOWER_BOUND : t_y
- if( _.t_x > m_x){ _.t_x =m_x;};
- if( _.t_y > m_y){ _.t_y =m_y;};
- var rec_vp1 = GLO_DAT.rec_vp1;
- var x_0 = _.t_x * til_san;
- var y_0 = _.t_y * til_san;
- GLO_DAT.rec_vp1.x_0 = x_0;
- GLO_DAT.rec_vp1.y_0 = y_0;
- GLO_DAT.rec_vp1.x_1 = ( x_0 + til_san - 1 );
- GLO_DAT.rec_vp1.y_1 = ( y_0 + til_san - 1 );
- };;
- function _ReadEvent( glo_eve ){
- var key = glo_eve.key;
- var val =( 0 - 666 );
- if( 0
- || "ARR_UPP" == key
- || "ARR_DOW" == key
- || "ARR_LEF" == key
- || "ARR_RIG" == key
- ){
- if( key == "ARR_UPP" ){ _SnapMoveVP1_UPP( ); }else
- if( key == "ARR_DOW" ){ _SnapMoveVP1_DOW( ); }else
- if( key == "ARR_LEF" ){ _SnapMoveVP1_LEF( ); }else
- if( key == "ARR_RIG" ){ _SnapMoveVP1_RIG( ); }else
- {
- //:EDCL:Expected_Dead_Code_Line:
- throw("[EDCL:EXPECTED_ONE_OF_FOUR_KEYS_ABOVE]");
- };;
- _Update();
- };;
- if( 0
- || "+" == key
- || "=" == key
- || "-" == key
- || "_" == key
- ){
- if( "+"==key || "="==key ){
- //:ZOOM_IN
- _.til_exp =( _.til_exp - 1 );
- };;
- if( "-"==key || "_"==key ){
- //:ZOOM_OUT:
- _.til_exp =( _.til_exp + 1 );
- };;
- _Update();
- };;
- };;
- function _Update(){
- _UpdateCoordinates_VP1();
- _UpdateGraphics();
- };;
- function _ForceUpdate( ){
- _Update();
- };;
- function _SnapMoveVP1_UPP( ){
- _.t_y =( _.t_y - 1 );
- console.log("_SnapMoveVP1_UPP");
- };;
- function _SnapMoveVP1_DOW( ){
- _.t_y =( _.t_y + 1 );
- console.log("_SnapMoveVP1_DOW");
- };;
- function _SnapMoveVP1_LEF( ){
- _.t_x =( _.t_x - 1 );
- console.log("_SnapMoveVP1_LEF");
- };;
- function _SnapMoveVP1_RIG( ){
- _.t_x =( _.t_x + 1 );
- console.log("_SnapMoveVP1_RIG");
- };;
- };;
- //://////////////////////////////////////////////////////////://
- function NEW_UserInputAdapterSingleton( can /**canvas**/ ){
- "use strict"
- var uin_ada=( new UserInputAdapterSingleton( can ) );
- Object.seal( uin_ada );
- return( uin_ada );
- };;
- function UserInputAdapterSingleton(
- can /** html5_canvas **/
- ){
- "use strict"
- //:Private Variables:
- //:PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP://
- var _ = {};
- _.can = null;
- //:PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP://
- function _Constructor( can ){
- if(!can){throw("[ERR:2020_05_08:0303PM]");};
- _.can = can;
- _.can.addEventListener(
- "click" //:<------------ TypeOfEventAsString
- , _NativeClickEventHandler //:Callback_Function
- );;
- document.addEventListener(
- "keydown" //:<----------- TypeOfEventAsString
- , _NativeKeyboardEventHandler //:Callback_Function
- );;
- };;
- function _NativeKeyboardEventHandler( nat_eve ){
- var glo_eve = GLO_DAT.glo_eve;
- //://////////////////////////////////////////////////://
- //:Store keyboard key that was pressed:
- //://////////////////////////////////////////////://
- var key = nat_eve.key.toUpperCase( );
- if( "ARROWLEFT" == key ){ key = "ARR_LEF"; };
- if( "ARROWRIGHT" == key ){ key = "ARR_RIG"; };
- if( "ARROWUP" == key ){ key = "ARR_UPP"; };
- if( "ARROWDOWN" == key ){ key = "ARR_DOW"; };
- glo_eve.key = key;
- //://////////////////////////////////////////////://
- GLO_CLA.vp1_sna.ReadEvent( glo_eve );
- console.log( glo_eve );
- //://////////////////////////////////////////////////://
- };;
- function _NativeClickEventHandler( nat_eve ){
- var glo_eve = GLO_DAT.glo_eve;
- //://////////////////////////////////////////////////://
- var rec =( _.can.getBoundingClientRect() );
- var CAN_X=( nat_eve.clientX - rec.left );
- var CAN_Y=( nat_eve.clientY - rec.top );
- glo_eve.c_x=( CAN_X ); //:Click_X
- glo_eve.c_y=( CAN_Y ); //:Click_Y
- //://////////////////////////////////////////////////://
- console.log("[CLICKITY_CLICK]");
- };;
- _Constructor( can );
- };;
- //://////////////////////////////////////////////////////////://
- function NEW_GlobalEventStruct( /** void **/ ){
- "use strict"
- var glo_eve ={
- TYP : "glo_eve"
- //: sub_typ : "mouse_event" or "keyboard_event" ?
- //:KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK://
- , key : "" //:What key was pressed?
- , k_d : 0 //:IsKeyCurrentlyDown?
- , k_u : 0 //:IsKeyCurrentlyUp?
- //:KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK://
- //:M:Mouse Event Stuff:
- //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
- , m_x : 0 //:Mouse x
- , m_y : 0 //:Mouse y
- , c_x : 0 //:Mouse CLICK: x
- , c_y : 0 //:Mouse CLICK: y
- , u_x : 0 //:Mouse UP ( u_x == "Up X" )
- , u_y : 0 //:Mouse UP ( u_y == "Up Y" )
- , d_x : 0 //:Mouse DOWN
- , d_y : 0 //:Mouse DOWN
- //:If both are set or unset,
- //:that is an error:
- , m_d : 0 //:IsMouseCurrentlyDown?
- , m_u : 0 //:IsMouseCurrentlyUp?
- //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
- };;Object.seal( glo_eve );
- return( glo_eve );
- };;
- //://////////////////////////////////////////////////////////://
- function CreateTileMap( map_san /** span==wid==hig **/ ){
- "use strict"
- 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 );
- };;
- //://////////////////////////////////////////////////////////://
- //: GLO_FUN.DoMapping_vp1_TO_vp0();
- function SOTM_DoMapping_vp1_TO_vp0( ){
- "use strict"
- var til_map = GLO_DAT.til_map;
- var can_ada =( GLO_CLA.can_ada = GLO_CLA.can_ada );
- var rec_src = GLO_DAT.rec_vp1; //: [vp1|vp2|vp3]
- var rec_dst = GLO_DAT.rec_vp0;
- DoMapping(
- can_ada //: <--------- rec_dst puts to 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
- ){
- "use strict"
- //://////////////////////////////////////////////////://
- 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_grey =[128,128,128,255];
- var rgb_light_grey=[ 230,230,230,255 ];
- var rgb=[1,2,3,4];//:rgba
- var rgb_ref=null;
- var loc={}; //:loc == local coordinates | values.
- loc.s_x = 0;
- loc.s_y = 0;
- Object.seal( loc );
- //:SOURCE:
- var sx0 = rec_src.x_0; //: rec_vp1
- var sx1 = rec_src.x_1; //: rec_vp1
- var sy0 = rec_src.y_0; //: rec_vp1
- var sy1 = rec_src.y_1; //: rec_vp1
- //:DEST:
- var dx0 = rec_dst.x_0; //: rec_vp0
- var dx1 = rec_dst.x_1; //: rec_vp0
- var dy0 = rec_dst.y_0; //: rec_vp0
- var dy1 = rec_dst.y_1; //: rec_vp0
- var vp0_wid = (dx1-dx0+1); //:DESTINATION:WID
- var vp0_hig = (dy1-dy0+1); //:DESTINATION:HIG
- //:VN:Viewport1(wid/hig)_and_N_(X|Y)_Values:---------://
- //:VNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVN://
- //:This equality is only true if ONE pixel on the
- //:source viewport (rec_vp1) is used to encode
- //:a tile value. For example, if a cluster of 4
- //:pixels were used to encode a tile value,
- //:then: n_x != vp1_wid
- //: n_y != vp1_hig
- //:n_x === vp1_wid (n_x: NumberOfSections: x )
- //:n_y === vp1_hig (n_y: NumberOfSections: y )
- //:VNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVN://
- var vp1_wid = (sx1-sx0+1); //:SOURCE::::::WID
- var vp1_hig = (sy1-sy0+1); //:SOURCE::::::HIG
- var SRC_ENC_CLU_X=( 1 ); //:Tile_Encoding_Scheme:X
- var SRC_ENC_CLU_Y=( 1 ); //:Tile_Encoding_Scheme:Y
- var n_x = (sx1-sx0+1) / SRC_ENC_CLU_X;
- var n_y = (sy1-sy0+1) / SRC_ENC_CLU_Y;
- //:VNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVNVN://
- //:Size of tile:
- //: 1: Measured in pixels.
- //: 2: As seen in destination ( rec_vp0 ) viewport.
- var tp0_wid = ( vp0_wid / n_x ); //:TilePixels_DEST( 0 )
- var tp0_hig = ( vp0_hig / n_y ); //:TilePixels_DEST( 0 )
- var tincox0 = 0; //:TileINternalCOordinate:(X0/MIN)
- var tincoy0 = 0; //:TileINternalCOordinate:(Y0/MIN)
- //:TINCO-( MAX( X1 | Y1 ) )
- //:If rec_vp1 can be overlayed completely evenly
- //:into rec_vp0, then [sp0_(wid|hig)]===1
- var tincox1 =( tp0_wid - 1 );
- var tincoy1 =( tp0_hig - 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_light_grey);
- }else{
- //:Convert [d_x,d_y] to local coords [ldx,ldy]:
- var ldx = ( d_x - dx0 ); //:ldx:LocalCord:Dest:X
- var ldy = ( d_y - dy0 ); //:ldy:LocalCord:Dest:Y
- //: 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
- //[ OLD_FORMULA_HAS_OFF_BY_ONE_ERROR: ]//
- //: var s_x =(((d_x-dx0)/(dx1-dx0))*(sx1-sx0))+sx0;
- //: var s_y =(((d_y-dy0)/(dy1-dy0))*(sy1-sy0))+sy0;
- //: s_x === t_x //:Identical_Results
- //: s_y === t_y //:Identical_Results
- var s_x; //:s_x: Source_X
- var s_y; //:s_y: Source_Y
- var t_x; //:t_x: Tile_X
- var t_y; //:t_y: Tile_Y
- //://////////////////////////////////////////://
- var F = ( Math.floor );
- s_x = F(((ldx)/ (vp0_wid))*(vp1_wid) ) + sx0 ;
- s_y = F(((ldy)/ (vp0_hig))*(vp1_hig) ) + sy0 ;
- t_x = F( (ldx)/( (vp0_wid) /(vp1_wid))) + sx0 ;
- t_y = F( (ldy)/( (vp0_hig) /(vp1_hig))) + sy0 ;
- //://////////////////////////////////////////://
- s_x = Math.floor( s_x );
- s_y = Math.floor( s_y );
- //# THIS_IS_NOT_THE_VALUE_OF_THE_CURRENT #//
- //# TILE_THE_PIXEL [ d_x, d_y ] IS_INSIDE_OF!#//
- t_x = Math.floor( t_x ); //:TILE_ON:SRC(rec_vp1)
- t_y = Math.floor( t_y ); //:TILE_ON:SRC(rec_vp1)
- if( s_x != t_x ){
- throw("[s_x!=t_x]");
- };;
- if( s_y != t_y ){
- throw("[s_y!=t_y]");
- };;
- //:SL:SourceLocal: [ loc.s_x , loc.s_y ]
- //:SLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSL://
- //: ( rec_vp1 === rec_src )
- loc.s_x = ( s_x - rec_src.x_0 );
- loc.s_y = ( s_y - rec_src.y_0 );
- //:SLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSLSL://
- //: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];
- rgb_ref = ( rgb );
- //://////////////////////////////////////////://
- //: Error signifies one of these problems:
- //: 1: rec_vp1 is not within bounds of tilemap.
- //: 2: The calculations for s_x & s_y are wrong.
- if( s_x < 0 || s_x >= til_map.map_wid ){
- throw("[CameraLikelyOutOfBounds:s_x]");
- };;
- if( s_y < 0 || s_y >= til_map.map_hig ){
- throw("[CameraLikelyOutOfBounds:s_y]");
- };;
- //: What is the current tile value of the pixel
- //: being drawn on the destination viewport?
- //: CURRENT_TILE_IN_FOCUS==[ vp0_stx , vp0_sty ]
- //# WTF DOES THIS CODE MEAN? #//
- var vp0_stx = F( (ldx+ (s_x*tp0_wid) ) / tp0_wid );
- var vp0_sty = F( (ldy+ (s_y*tp0_hig) ) / tp0_hig );
- //:Get local pixel coordinate value from within
- //:the CURRENT_TILE_IN_FOCUS([vp0_stx,vp0_sty])
- var tinco_x ; //:Tile_Internal_COordinate:X
- var tinco_y ; //:Tile_Internal_COordinate:Y
- /////tinco_x = ldx - ( vp0_stx * tp0_wid /**usedup:x**/ );
- /////tinco_y = ldy - ( vp0_sty * tp0_hig /**usedup:y**/ );
- //:As long as rec_vp1 is snapped to whole
- //:pixel values, then the first tile will
- //:always be tangent to the upper-left edge
- //:of the on screen viewport ( rec_vp0 ).
- //:So all you need to do is modulate by the
- //:size of a tile as it appears in destination
- //:viewport pixel dimensions.
- //# MODULATING BY FRACTIONS == ASKING4TROUBLE#//
- //[ tinco_x = ldx % tp0_wid; ]//
- //[ tinco_y = ldy % tp0_hig; ]//
- //[ KEEP: tinco NON-normalized ------------- ]//
- tinco_x = ldx - ( tp0_wid * loc.s_x );
- tinco_y = ldy - ( tp0_hig * loc.s_y );
- if( tinco_x < 0 || tinco_x >= tp0_wid ||
- tinco_y < 0 || tinco_y >= tp0_hig
- ){
- throw("[tinco_(x|y):OOB]");
- };;
- //:B:Boarder: (1 pixel thick boarder)
- //:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB://
- if( tincox0 == tinco_x || tincox1 == tinco_x ){
- rgb_ref = rgb_black;
- }else
- if( tinco_x > tincox0 && tinco_x < tincox1 ){
- //:Do nothing.
- }else{
- var min = 0;
- var max = Math.ceil( tincox1 + 1 );
- if( tinco_x >= min && tinco_x <= max ){
- //:Partially on two different tiles.
- rgb_ref = rgb_grey;
- }else{
- throw("[tinco_x:OOB]");
- };;
- };;
- //:- - - - - - - - - - -- - - - - - - - - - -://
- if( tincoy0 == tinco_y || tincoy1 == tinco_y ){
- rgb_ref = rgb_black;
- }else
- if( tinco_y > tincoy0 && tinco_y < tincoy1 ){
- //:Do nothing.
- }else{
- var min = 0;
- var max = Math.ceil( tincoy1 + 1 );
- if( tinco_y >= min && tinco_y <= max ){
- //:Partially on two different tiles.
- rgb_ref = rgb_grey;
- }else{
- throw("[tinco_y:OOB]");
- };;
- };;
- //:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB://
- canvas_adapter.PutPix(d_x,d_y,rgb_ref);
- };; //:[ outside_destination_viewport ? ]
- };;};; //:Next[ d_x , d_y ]
- canvas_adapter.Apply();
- };;//://///////////////////////////////////////:DoMapping://
- //://////////////////////////////////////////////////////////://
- function attachCanvasToDom(
- ini_can_wid //: initial_canvas_wid //:In_Pixels
- , ini_can_hig //: initial_canvas_hig //:In_Pixels
- ){
- "use strict"
- //: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 = ini_can_wid; //:initial_canvas_wid;
- can.height = ini_can_hig; //: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 ){
- "use strict"
- var canvas_adapter =( new CanvasAdapterClass( can ) );
- return( canvas_adapter );
- };;
- //://////////////////////////////////////////////////////////://
- //:Canvas Adapter Class:
- function CanvasAdapterClass( can /** canvas **/ ){
- "use strict"
- 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
- ){
- "use strict"
- 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 ;
- var per_x =( p_x / til_map.map_wid );
- var 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