Transforms are added…endlessly

前端 未结 3 1756
灰色年华
灰色年华 2020-12-06 20:24

I\'m creating a simple asteroids-like game in CSS and JS using the DOM over canvas for...experimentation purposes.

My code is pretty small in this example to make i

3条回答
  •  没有蜡笔的小新
    2020-12-06 21:16

    For more info and a demo of the answer below see https://stackoverflow.com/a/43744006/3877726


    Using CSS transform : matrix function

    If given a object position, scale and rotation the quickest way to set the transform is to do it as a single matrix element.style.transform = "matrix(a,b,c,d,e,f)";

    The 6 values represent the direction and scale of the X axis (a,b), Y axis (c,d) , and the local origin (e,f)

    As most of the time you don't want to skew and the scale is uniform (x and y scale the same) the function to create and set the transform is quick. All you do is pass the position, scale and rotation.

    const setElementTransform = (function(){
        const matrix = [1,0,0,1,0,0]; // predefine the array (helps ease the GC load
        const m = matrix; // alias for code readability.
        return function(element, x, y, scale, rotation);
            m[3] = m[0] = Math.cos(rotation) * scale;     // set rotation and scale
            m[2] = -(m[1] = Math.sin(rotation) * scale);  // set rotation and scale
            m[4] = x;
            m[5] = y;
            element.style.transform = `matrix(${m.join(",")})`;
        }
    }());
    

    Don't use keyboardEvent.keyCode it has depreciated.

    Rather than use the old (and obscure key values) keyCode property to read the keys you should use the code property that has a string representing which key is down or up.

    const keys = {
        ArrowLeft : false,  // add only the named keys you want to listen to.
        ArrowRight: false,  
        ArrowUp   : false,  
        ArrowDown : false,  
        stopKeyListener : (function(){  // adds a listener and returns function to stop key listener if needed.
            function keyEvent(e){
                if(keys[e.code] !== undefined){ // is the key on the named list
                    keys[e.code] = e.type === "keydown"; // set true if down else false
                    e.preventDefault(); // prevent the default Browser action for this key.
            }
            addEventListener("keydown",keyEvent);
            addEventListener("keyup",keyEvent);
            return function(){
                removeEventListener("keydown",keyEvent);
                removeEventListener("keyup",keyEvent);
            }
        }()) //
    }
    

    Now at any time you can just check if a key is down with if(keys.ArrowLeft){


    Updating the DOM regularly? use requestAnimationFrame

    If you are making many changes to the DOM at regular intervals you should use requestAnimationFrame and it tells the browser your intention and will cause all DOM changes made from within the callback to sync with the display hardware and the DOM's own compositing and rendering.

    requestAnimationFrame(mainLoop);  // will start the animation once code below has been parse and executed.
    var player = {  // the player
        x : 0,
        y : 0,
        scale : 1,
        rotate : 0,
        speed : 0,
        element : document.getElementById("thePlayer")
    }
    function mainLoop(time){ // requestAnimationFrame adds the time as the first argument for the callback
         if(keys.ArrowLeft){ player.rotate -= 1 }
         if(keys.ArrowRight){ player.rotate += 1 }
         if(keys.ArrowUp){ player.speed  += 1 }
         if(keys.ArrowRight){ player.speed -= 1 }
         player.x += Math.cos(player.rotate) * player.speed;
         player.y += Math.sin(player.rotate) * player.speed;
         setElementTransform(
             player.element,
             player.x, player.y,
             player.scale,
             player.rotate
         );
         requestAnimationFrame(mainLoop);
    }
          
    

    For a demo https://stackoverflow.com/a/43744006/3877726 (same link as at top of answer)

提交回复
热议问题