Advertisement
jargon

Story05Js :: "canvasRenderCommands.js"

Jul 6th, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var imageCache = [];
  3. var missingCache = [];
  4.  
  5. var frontCanvas = false;
  6. var frontContext = false;
  7.  
  8. var backCanvas = false
  9. var backContext = false;
  10.  
  11. makeCanvas ( );
  12.  
  13. function makeCanvas ( ) {
  14.  
  15.     //document.addEventListener("DOMContentLoaded",  ( ) => {
  16.  
  17.     let fragment;
  18.  
  19.     // Create an on-screen canvas
  20.  
  21.     fragment = document.createDocumentFragment ( );
  22.  
  23.     frontCanvas = document.createElement('canvas');
  24.  
  25.     fragment.appendChild(frontCanvas);
  26.  
  27.     frontCanvas.style.zIndex = -1;
  28.    
  29.     frontCanvas.width = 320;
  30.     frontCanvas.height = 240;
  31.    
  32.     frontCanvas.style.width = `320px`;
  33.     frontCanvas.style.height = `240px`;
  34.  
  35.     frontCanvas.id = `frontCanvas`;
  36.  
  37.     frontContext = frontCanvas.getContext('2d');
  38.  
  39.     frontContext.width = 320;
  40.     frontContext.height = 200;
  41.    
  42.     // Create an off-screen canvas
  43.  
  44.     backCanvas = document.createElement('canvas');
  45.  
  46.     fragment.appendChild(backCanvas);
  47.  
  48.     // Append all changes at once to reduce reflows
  49.     document.body.appendChild(fragment);
  50.  
  51.     backCanvas.style.width = frontCanvas.style.width;
  52.  
  53.     backCanvas.style.height = frontCanvas.style.height;
  54.    
  55.     backCanvas.width = frontCanvas.width;
  56.     backCanvas.height = frontCanvas.height;
  57.  
  58.     backContext.width = frontContext.width;
  59.  
  60.     backContext.height = frontContext.height;
  61.  
  62.     backCanvas.style.zIndex = -2;
  63.  
  64.     backCanvas.style.visibility = 'hidden';
  65.  
  66.     backCanvas.id = `backCanvas`;
  67.  
  68.     backContext = backCanvas.getContext('2d');
  69.  
  70. }
  71.    
  72. function selectCanvas(id){
  73.     workCanvas = document.getElementById(id);
  74. }
  75.  
  76. function drawRectangle(x, y, width, height, color = 'black',fill = 'gray'){
  77.    
  78.     backContext.strokeStyle = color;
  79.    
  80.     if(fill === ''){
  81.         fill = 'rgba(0,0,0,0.0)';
  82.     }
  83.     backContext.fillStyle = fill;
  84.    
  85.     backContext.fillRect(x, y, width, height);
  86.  
  87.     backContext.strokeRect(x, y, width, height);
  88. }
  89.  
  90.  
  91. function drawLine(x,y,x2,y2,color = 'black',width = 5){
  92.     backContext.strokeStyle = color;
  93.     backContext.lineWidth = width;
  94.     backContext.beginPath ( );
  95.     backContext.moveTo(x, y);
  96.     backContext.lineTo(x2, y2);
  97.     backContext.stroke ( );
  98. }
  99.  
  100.  
  101. function drawCircle(x,y,radius = 50, color = 'black',fill = '#00FF00', width = 5){
  102.     backContext.beginPath ( );
  103.     backContext.arc(x, y, radius, 0, 2 * Math.PI);
  104.     backContext.fillStyle = '#00FF00';
  105.     backContext.fill ( );
  106.     backContext.strokeStyle = '#000000';
  107.     backContext.lineWidth = width;
  108.     backContext.stroke ( );
  109. }
  110.    
  111.  
  112. function drawText(x,y,font = '30px Arial',fill = '#000000',text = 'Hello backcanvas!'){
  113.     backContext.font = font;
  114.     backContext.fillStyle = fill;
  115.     backContext.fillText(text, x, y);
  116. }
  117.  
  118. function drawImageStack(imageStack, x, y, width = 24, height = 24) {
  119.    
  120.     // console.log({imageStack: imageStack});
  121.    
  122.     for(index = 0; index < imageStack.length; index++){
  123.        
  124.         if( imageStack[index] === undefined ){
  125.             continue;
  126.         }  
  127.        
  128.         imageStack[index] = basename(imageStack[index],`.png`);
  129.         imageStack[index] = left( imageStack[index] + `________`, 8 );
  130.        
  131.         // console.log(imageStack[index]);
  132.        
  133.         drawImage( imageStack[index], x, y, width, height );
  134.        
  135.     }          
  136.    
  137.     if(DEBUG.GUI){
  138.         if( index > 0 ){
  139.            
  140.             // drawImage(`dooropen`, x, y, width, height);
  141.            
  142.             // drawRectangle(x, y, width, height, 'rgba(255,0,0,0.7)','');
  143.    
  144.             // drawText(x,y+16,'12px Impact', 'rgba(255,0,0,0.7)','TEST');
  145.         }
  146.     }
  147. }
  148.  
  149. function drawImage(imagePath, x, y, width = 24, height = 24) {
  150.    
  151.     var cachedImage = loadImage(imagePath);
  152.    
  153.     if (cachedImage === undefined) {
  154.         return;
  155.     }
  156.  
  157.     backContext.drawImage(cachedImage, x, y, width, height);
  158. }
  159.  
  160. function loadImageStack(imageStack, width = 24, height = 24) {
  161.    
  162.     if(!Array.isArray(imageStack)) {
  163.         imageStack = [imageStack];
  164.     }  
  165.    
  166.     for(let index = 0; index < imageStack.length; index++){
  167.         imageStack[index] = loadImage(imageStack[index], width, height);
  168.     }
  169.    
  170.     return imageStack;
  171.    
  172. }
  173.  
  174. function loadImage(imagePath, width = 24, height = 24) {
  175.    
  176.     if (imagePath === undefined) {
  177.         return undefined;
  178.     }
  179.    
  180.     if(isObject(imagePath)) {
  181.         return undefined;
  182.     }
  183.    
  184.     if(Array.isArray(imagePath)) {
  185.         return loadImageStack(imagePath, width, height)[0];
  186.     }  
  187.    
  188.     if ( right ( imagePath, 4 )  === `.png` ) {
  189.         imagePath = left( imagePath, len(imagePath) - 4 );
  190.     }
  191.    
  192.     var fullPath = ``;
  193.     if ( left ( imagePath, len ( `{{ project root }` ) ) === `{{ project root }}` ) {
  194.         imagePath = mid ( imagePath, len( `{{ project root }` ) + 1 );
  195.     }
  196.         fullPath = `{{ project root }}${imagePath}.png`;
  197.     if ( len ( imagePath ) === 4 || len ( imagePath ) === 8 ) {
  198.         imagePath = left(imagePath + `________`, 8);
  199.        
  200.         if (imagePath === `________`) {
  201.             // logFeats(`Placeholder Image: ${fullPath}`);
  202.             return undefined;
  203.         }
  204.        
  205.         fullPath = `${host}/GFX/${imagePath}.png`;
  206.        
  207.     } else {       
  208.        
  209.         fullPath = `{{ project root }}${imagePath}.png`;
  210.    
  211.     }
  212.    
  213.     let cachedImage = imageCache.find(img => img.src === fullPath);
  214.  
  215.     if (!cachedImage) {
  216.         cachedImage = new Image(width, height);
  217.         cachedImage.src = fullPath;
  218.        
  219.         cachedImage.onload = function ( ) {
  220.             imageCache.push(cachedImage);
  221.             logFeats(`Loaded: ${cachedImage.src}`);
  222.             // backContext.drawImage(cachedImage, x, y, width, height); // Draw image after loading
  223.         };
  224.  
  225.         cachedImage.onerror = function ( ) {
  226.             logFeats(`Failed to load: ${cachedImage.src}`);
  227.             cachedImage = undefined;
  228.         };
  229.     } else {
  230.         // logFeats(`Drawing Image: ${cachedImage.src}`);
  231.         return cachedImage;
  232.     }
  233. }
  234.  
  235.  
  236. function doubleBuffer ( ) {
  237.    
  238.     frontContext.clearRect(0, 0, frontCanvas.width, frontCanvas.height);
  239.    
  240.     frontContext.drawImage(backCanvas, 0, 0);
  241. }
  242.  
  243. function getColorAtPoint(x, y) {
  244.     const imageData = backCanvas.getImageData(x, y, 1, 1); // Step 4
  245.     const data = imageData.data;
  246.     const rgba = `rgba(${data[0]}, ${data[1]}, ${data[2]}, ${data[3] / 255})`; // Step 5
  247.     return rgba;
  248. }
  249.    
  250. function grabColor(fg){
  251.     if(left(fg,1) === '#'){
  252.         fg = mid(fg,2);
  253.         fg = sections(fg,2);
  254.     }else if( ( left( fg, 5 ) === 'rgba(' ) && ( right( fg, 1 ) === ')' ) ){
  255.         fg = mid(fg,6);
  256.         fg = left(fg,len(fg)-1);
  257.         fg = fg.replace(' ','');
  258.         fg = fg.split(',');
  259.     }else{
  260.         fg = grabColor(fg);
  261.     }
  262.  
  263.     return fg;
  264. }
  265.  
  266. function calcTextWidth( font, text ){
  267.    
  268.     let fontTemp = backContext.font;
  269.    
  270.     backContext.font = font;
  271.    
  272.     const textMetrics = backContext.measureText(text);
  273.     const textWidth = textMetrics.width;
  274.    
  275.     backContext.font = fontTemp;
  276.  
  277.     return textWidth;
  278. }
  279.  
  280. function wrapText(x, y, maxWidth, lineHeight, font, fill, text) {
  281.    
  282.     const words = text.split(' ');
  283.     let buffer = '';
  284.  
  285.     backContext.font = font;
  286.     backContext.fillStyle = fill;
  287.    
  288.     for (let index = 0; index < words.length; index++) {
  289.        
  290.         const testLine = buffer + (buffer.length > 0 ? ' ' : '') + words[index];
  291.        
  292.         const metrics = backContext.measureText(testLine);
  293.        
  294.         if (metrics.width > maxWidth) {
  295.             backContext.fillText(buffer, x, y);
  296.             buffer = words[index];
  297.             y += lineHeight;
  298.         } else {
  299.             buffer = testLine;
  300.         }
  301.     }
  302.    
  303.     if (buffer.length > 0) {
  304.         backContext.fillText(buffer, x, y);
  305.     }
  306.    
  307.     // Copy the contents of backContext to frontContext
  308.     frontContext.clearRect(0, 0, frontContext.canvas.width, frontContext.canvas.height);
  309.     frontContext.drawImage(backContext.canvas, 0, 0);
  310. }
  311.  
  312. /* {{ #/{{ project root }}/scripts/js/canvas/canvas3dRenderCommands.js }} */
  313.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement