JS Snippets Thread

Discussion in 'JS Plugin Releases (RMMV)' started by mlogan, Mar 11, 2018.

    Tags:
  1. DK

    DK Veteran Veteran

    Messages:
    94
    Likes Received:
    87
    Location:
    Russia
    First Language:
    Russian
    Primarily Uses:
    RMMV
    Adding new methods to standard classes is a very bad practice

    The includes method already exists in the array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
     
    #21
  2. bblizzard

    bblizzard Veteran Veteran

    Messages:
    245
    Likes Received:
    253
    Location:
    Croatia
    First Language:
    Croatian
    Primarily Uses:
    RMMV
    #22
  3. Aloe Guvner

    Aloe Guvner Walrus Veteran

    Messages:
    1,399
    Likes Received:
    882
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    ArrayIncludes.png
     
    #23
    bblizzard and DK like this.
  4. DK

    DK Veteran Veteran

    Messages:
    94
    Likes Received:
    87
    Location:
    Russia
    First Language:
    Russian
    Primarily Uses:
    RMMV
    This function appeared in rpg maker with version 1.5 (when support for es6 appeared). Also, developers added their similar function, which is called contains (see rpg_core.js). It was from version 1.0.
    It is also important to know that the functions includes and indexOf work differently. It seems that indexOf does not work with NaN.
     
    #24
    bblizzard likes this.
  5. bblizzard

    bblizzard Veteran Veteran

    Messages:
    245
    Likes Received:
    253
    Location:
    Croatia
    First Language:
    Croatian
    Primarily Uses:
    RMMV
    That's quite weird, I kept getting errors when I tried using it. I think I started with 1.5.1 or 1.5.2 so it should have been there (last year in November or so). Oh well. ¯\_(ツ)_/¯ I'll switch out my own code.
     
    #25
  6. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,547
    Likes Received:
    1,292
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    load multi fonts example without api or plugin.
    We use here the native method `document.fonts.check()`
    ` this.load();` it for continue after all fonts are succeed to load.
    PHP:
        load_fonts(){
            const 
    fonts = [ // your fonts reference here
                
    {name:"ArchitectsDaughter"url:"fonts/ArchitectsDaughter.ttf"},
                {
    name:"zBirdyGame"url:"fonts/zBirdyGame.ttf"},
            ];
            
    fonts.forEach(font => { // for each fonts create @font-face rules
                
    const style document.createElement('style');
                
    style.appendChild(document.createTextNode(`
                    @font-face {
                        font-family: '
    ${font.name}';
                        font-style: normal;
                        font-weight: 700;
                        src: url("
    ${font.url}");
                    }
                
    `));
                
    document.getElementsByTagName('head').item(0).appendChild(style);
                const 
    div document.createElement('div'); // create a div for each fonts rule (yes it weird but it how web fonts are loaded)
                
    div.style.fontFamily font.name;
                
    document.body.appendChild(div);/* Initiates download in Firefox, IE 9+ */
                
    div.innerHTML 'Content.';/* Initiates download in WebKit/Blink */
            
    });
            
    let checkFonts =  setInterval(()=>{// check every ticks if fonts are available in cache
                
    if( fonts.every(=> document.fonts.check(`12px ${e.name}`) )){
                    
    this.fonts fonts;
                    
    clearInterval(checkFonts);
                    
    this.load();
                }
            },
    60);
        };
    ps: you can also remove div used for load fonts after, they take memory for nothing.
    PHP:
    function removeElement(id) {
        var 
    elem document.getElementById(id);
        return 
    elem.parentNode.removeChild(elem);
    }
     
    #26
  7. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,547
    Likes Received:
    1,292
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    This snippet allow you to: Add hashing method to your `String` for versioning or other purpose.


    PHP:
    String.prototype.hashCode = function() {
      var 
    hash 0ichr;
      if (
    this.length === 0) return hash;
      for (
    0this.lengthi++) {
        
    chr   this.charCodeAt(i);
        
    hash  = ((hash << 5) - hash) + chr;
        
    hash |= 0// Convert to 32bit integer
      
    }
      return 
    hash;
    };
    example, let versioning this obj data {a:123456, b:'abcde'}
    PHP:
    JSON.stringify({a:123456,b:'abcde'}, null'\t').hashCode(); // return alway 1776027141
     
    #27
  8. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,547
    Likes Received:
    1,292
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    This snippet allow you to: Compute memory your code take with nwjs

    PHP:
    var befor process.memoryUsage().heapUsed 1024 1024;
          
    // your code
    var after process.memoryUsage().heapUsed 1024 1024;
    var 
    = `code consume ~${after-befor} MB in memory`;
    console.log(r)
     
    #28
    Kvothe likes this.

Share This Page