Advertisement
jargon

Roe2Js :: "canvasRenderCommands.js"

Jun 30th, 2024 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var imageCache = [];
  2. var missingCache = [];
  3.  
  4. var frontCanvas = false;
  5. var frontContext = false;
  6.  
  7. var backCanvas = false
  8. var backContext = false;
  9.  
  10. makeCanvas ( );
  11.  
  12. function makeCanvas ( ) {
  13.  
  14.     //document.addEventListener("DOMContentLoaded", () => {
  15.  
  16.     let fragment;
  17.  
  18.     // Create an on-screen canvas
  19.  
  20.     fragment = document.createDocumentFragment();
  21.  
  22.     frontCanvas = document.createElement('canvas');
  23.  
  24.     fragment.appendChild(frontCanvas);
  25.  
  26.     frontCanvas.style.zIndex = -1;
  27.  
  28.     frontCanvas.id = `frontCanvas`;
  29.  
  30.     frontContext = frontCanvas.getContext('2d');
  31.    
  32.     // Create an off-screen canvas
  33.  
  34.     backCanvas = document.createElement('canvas');
  35.  
  36.     fragment.appendChild(backCanvas);
  37.  
  38.     // Append all changes at once to reduce reflows
  39.     document.body.appendChild(fragment);
  40.  
  41.     backCanvas.style.width = frontCanvas.style.width;
  42.  
  43.     backCanvas.style.height = frontCanvas.style.height;
  44.  
  45.     backCanvas.style.zIndex = -2;
  46.  
  47.     backCanvas.style.visibility = 'hidden';
  48.  
  49.     backCanvas.id = `backCanvas`;
  50.  
  51.     backContext = backCanvas.getContext('2d');
  52.  
  53. }
  54.    
  55. function selectCanvas(id){
  56.     workCanvas = document.getElementById(id);
  57. }
  58.  
  59. function drawRectangle(x, y, width, height, color = 'black',fill = 'gray'){
  60.    
  61.     backContext.strokeStyle = color;
  62.    
  63.     if(fill === ''){
  64.         fill = 'rgba(0,0,0,0.0)';
  65.     }
  66.     backContext.fillStyle = fill;
  67.    
  68.     backContext.fillRect(x, y, width, height);
  69.  
  70.     backContext.strokeRect(x, y, width, height);
  71. }
  72.  
  73.  
  74. function drawLine(x,y,x2,y2,color = 'black',width = 5){
  75.     backContext.strokeStyle = color;
  76.     backContext.lineWidth = width;
  77.     backContext.beginPath();
  78.     backContext.moveTo(x, y);
  79.     backContext.lineTo(x2, y2);
  80.     backContext.stroke();
  81. }
  82.  
  83.  
  84. function drawCircle(x,y,radius = 50, color = 'black',fill = '#00FF00', width = 5){
  85.     backContext.beginPath();
  86.     backContext.arc(x, y, radius, 0, 2 * Math.PI);
  87.     backContext.fillStyle = '#00FF00';
  88.     backContext.fill();
  89.     backContext.strokeStyle = '#000000';
  90.     backContext.lineWidth = width;
  91.     backContext.stroke();
  92. }
  93.    
  94.  
  95. function drawText(x,y,font = '30px Arial',fill = '#000000',text = 'Hello backcanvas!'){
  96.     backContext.font = font;
  97.     backContext.fillStyle = fill;
  98.     backContext.fillText(text, x, y);
  99. }
  100.  
  101. function drawImageStack(imageStack, x, y, width = 24, height = 24) {
  102.    
  103.     // console.log({imageStack: imageStack});
  104.    
  105.     for(index = 0; index < imageStack.length; index++){
  106.        
  107.         if( imageStack[index] === undefined ){
  108.             continue;
  109.         }  
  110.        
  111.         imageStack[index] = basename(imageStack[index],`.png`);
  112.         imageStack[index] = left( imageStack[index] + `________`, 8 );
  113.        
  114.         console.log(imageStack[index]);
  115.        
  116.         drawImage( imageStack[index], x, y, width, height );
  117.        
  118.     }          
  119.    
  120.     if(DEBUG.GUI){
  121.         if( index > 0 ){
  122.            
  123.             drawImage(`dooropen`, x, y, width, height);
  124.            
  125.             // drawRectangle(x, y, width, height, 'rgba(255,0,0,0.7)','');
  126.    
  127.             // drawText(x,y+16,'12px Impact', 'rgba(255,0,0,0.7)','TEST');
  128.         }
  129.     }
  130. }
  131.  
  132. function drawImage(imagePath, x, y, width = 24, height = 24) {
  133.    
  134.     if( imagePath === undefined ) {
  135.         return;
  136.     }
  137.    
  138.     imagePath = basename( imagePath, `.png` );
  139.     imagePath = left( imagePath + `________`, 8 );
  140.    
  141.     let fullPath = `${host}/GFX/${imagePath}.png`;
  142.    
  143.     if( imagePath === `________` ) {
  144.         logFeats(`Placeholder Image: ${fullPath}`);
  145.         return
  146.    
  147.     };
  148.    
  149.     /*
  150.     if( !fileExists( fullPath ) ) {
  151.         logFeats(`Missing Image: ${fullPath}`);
  152.         return;
  153.     }
  154.     */
  155.    
  156.     let cachedImage = imageCache.find(img => img.src === fullPath);
  157.    
  158.     if ( !cachedImage ) {
  159.                    
  160.         cachedImage = new Image(width, height);
  161.         cachedImage.src = fullPath;
  162.         imageCache.push(cachedImage);
  163.        
  164.         logFeats(`Loaded: ${cachedImage.src}`);
  165.     }
  166.    
  167.     logFeats(`Drawing Image: ${cachedImage.src}`);
  168.    
  169.     backContext.drawImage(cachedImage, x, y, width, height);
  170. }
  171.  
  172. function doubleBuffer ( ) {
  173.    
  174.     frontContext.clearRect(0, 0, frontCanvas.width, frontCanvas.height);
  175.    
  176.     frontContext.drawImage(backCanvas, 0, 0);
  177. }
  178.  
  179. function getColorAtPoint(x, y) {
  180.     const imageData = backCanvas.getImageData(x, y, 1, 1); // Step 4
  181.     const data = imageData.data;
  182.     const rgba = `rgba(${data[0]}, ${data[1]}, ${data[2]}, ${data[3] / 255})`; // Step 5
  183.     return rgba;
  184. }
  185.    
  186. function grabColor(fg){
  187.     if(left(fg,1) === '#'){
  188.         fg = mid(fg,2);
  189.         fg = sections(fg,2);
  190.     }else if( ( left( fg, 5 ) === 'rgba(' ) && ( right( fg, 1 ) === ')' ) ){
  191.         fg = mid(fg,6);
  192.         fg = left(fg,len(fg)-1);
  193.         fg = fg.replace(' ','');
  194.         fg = fg.split(',');
  195.     }else{
  196.         fg = grabColor(fg);
  197.     }
  198.  
  199.     return fg;
  200. }
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement