123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="canvas"></canvas>
<style>
  canvas {
    display: block;
    margin: 40px auto 20px;
    border: 1px solid #333;
    box-shadow: 0 0 16px 2px rgba(0, 0, 0, 1);
  }
</style>

<script>
  /* Setup of the engine */
  window.requestAnimFrame =
    window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback) {
      return window.setTimeout(callback, 1000 / 60);
    };
  var canvas = document.getElementById('canvas'),
    ctx = canvas.getContext('2d');
  canvas.width = 400;
  canvas.height = 400;
  var game = new Vas();
  //game.format = "tilemap";
  var Loop = function() {
    game.rect(2, 2, 20, 20, 'violet');
    game.circle(35, 35, 12, 'green');
    game.line(5, 45, 45, 90, 90, 'blue');
    game.image(100, 100, "sprite1.png");
    game.rect(200, 200, 20, 20, 'red', 45);
    game.triangle(270, 310, 320, 320, 260, 260, '#777');
    game.penta(30, 300, 20, 'blue');
    game.polygon(100, 300, 8, 20, 'green');
    game.polygon(300, 50, 6, 20, 'orange');
    game.polygon(300, 200, 7, 20, 'violet');
    // game.move_Player();
    if (game.key.up) {
      alert("key up");
      game.key.up = false;
    }
    window.requestAnimFrame(Loop);
  };
  Loop();
</script>
            
          
!
            
              /* Vas Gaming Engine v0.0.52
*
* Developed by Malmadork
*
* So far I have added simple geometry functions that can be used with canvas:
*
* 1. Define your canvas's context as 'ctx', and canvas.width/canvas.height
* 2. Define Vas: [var game = new Vas();]
* 3. Use functions listed below. Functions must be used with Vas; Used like this Vas.rect();
* [Example: Vas.rect( 4, 4, 2, 2, 'red'); This makes a red 2x2 rectangle at (4,4).]
*
*
* Changelog:
* Most Recent Change: v5.0: Keypress events; v5.1: more keys added, specific controls for different types of developing; v5.2: clear canvas function, still needs some fixin.
* Biggest Change: New variable system, Polygons, support for different styles of developing (2d, webGL)
*
*
* How to use keypress events: Currently there are no predefined keypress events.
* To use them in development, just use [if(this.key.up) { doThis(); this.key.up = false; }] in which this equals
* your defined Vas(); [var game = new Vas(); ]
*/

var Vas = function() {
  this.alert_errors = false;
  this.log_info = false;
  this.format = "still";
  this.numberOfElements = 0;

  this.key = {
    left: false,
    right: false,
    up: false,
    down: false,
    _a: false,
    _b: false,
    _c: false,
    _d: false,
    _e: false,
    _f: false,
    _g: false,
    _h: false,
    _i: false,
    _j: false,
    _k: false,
    _l: false,
    _m: false,
    _n: false,
    _o: false,
    _p: false,
    _q: false,
    _r: false,
    _s: false,
    _t: false,
    _u: false,
    _v: false,
    _w: false,
    _x: false,
    _y: false,
    _z: false,
    _1: false,
    _2: false,
    _3: false,
    _4: false,
    _5: false,
    _6: false,
    _7: false,
    _8: false,
    _9: false,
    _0: false,
    enter: false,
    shift: false
  };

  this.confirmOnClear = false;
  //this.cleared = false; possible fix for loop clearing

  this.controls = "WASD_Arrow";

  window.onkeydown = this.keydown.bind(this);
  window.onkeyup = this.keyup.bind(this);
};

/* End of Engine setup */

//Used for alerting errors or messages

Vas.prototype.error = function(message) {
  if (this.alert_errors) alert(message);
  if (this.log_info) console.log(message);
};

//Used to log errors or messages

Vas.prototype.log = function(message) {
  if (this.log_info) console.log(message);
};

//Keypress Events - Key Pressed

Vas.prototype.keydown = function(e) {
  var _this = this;

  if (this.controls == "WASD_Arrow") {
    switch (e.keyCode) {
      case 37:
        _this.key.left = true;
        break;
      case 38:
        _this.key.up = true;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = true;
        break;

      case 40:
        _this.key.down = true;
        e.preventDefault;
        break;

      case 65:
        _this.key.left = true;
        break;
      case 87:
        _this.key.up = true;
        e.preventDefault;

        break;
      case 68:
        _this.key.right = true;
        break;
      case 83:
        _this.key.down = true;
        e.preventDefault;
        break;
    }
  } else if (this.controls == "WASD") {
    switch (e.keyCode) {
      case 37:
        _this.key.left = true;
        break;
      case 38:
        _this.key.up = true;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = true;
        break;

      case 40:
        _this.key.down = true;
        e.preventDefault;
        break;
    }
  } else if (this.controls == "custom") {
    switch (e.keyCode) {
      case 65: //a
        _this.key._a = true;
        e.preventDefault;
        break;
      case 66:
        _this.key._b = true;

        break;
      case 67:
        _this.key._c = true;
        e.preventDefault;
        break;
      case 68:
        _this.key._d = true;
        e.preventDefault;
        break;
      case 69: //e
        _this.key._e = true;
        e.preventDefault;
        break;
      case 70:
        _this.key._f = true;
        e.preventDefault;
        break;
      case 71:
        _this.key._g = true;
        e.preventDefault;
        break;
      case 72:
        _this.key._h = true;
        e.preventDefault;
        break;
      case 73:
        _this.key._i = true;
        e.preventDefault;
        break;
      case 74:
        _this.key._j = true;
        e.preventDefault;
        break;
      case 75: //k
        _this.key._k = true;
        e.preventDefault;
        break;
      case 76:
        _this.key._l = true;
        e.preventDefault;
        break;
      case 77:
        _this.key._m = true;
        e.preventDefault;
        break;
      case 78:
        _this.key._n = true;
        e.preventDefault;
        break;
      case 79:
        _this.key._o = true;
        e.preventDefault;
        break;
      case 80: //p
        _this.key._p = true;
        e.preventDefault;
        break;
      case 81:
        _this.key._q = true;
        e.preventDefault;
        break;
      case 82:
        _this.key._r = true;
        e.preventDefault;
        break;
      case 83:
        _this.key._s = true;
        e.preventDefault;
        break;
      case 84:
        _this.key._t = true;
        e.preventDefault;
        break;
      case 85: //u
        _this.key._u = true;
        e.preventDefault;
        break;
      case 86:
        _this.key._v = true;
        e.preventDefault;
        break;
      case 87:
        _this.key._w = true;
        e.preventDefault;
        break;
      case 88:
        _this.key._x = true;
        e.preventDefault;
        break;
      case 89:
        _this.key._y = true;
        e.preventDefault;
        break;
      case 90:
        _this.key._z = true;
        e.preventDefault;
        break;
      case 49:
        _this.key._1 = true;
        e.preventDefault;
        break;
      case 50:
        _this.key._2 = true;
        e.preventDefault;
        break;
      case 51:
        _this.key._3 = true;
        e.preventDefault;
        break;
      case 52:
        _this.key._4 = true;
        e.preventDefault;
        break;
      case 53:
        _this.key._5 = true;
        e.preventDefault;
        break;
      case 54:
        _this.key._6 = true;
        e.preventDefault;
        break;
      case 55:
        _this.key._7 = true;
        e.preventDefault;
        break;
      case 56:
        _this.key._8 = true;
        e.preventDefault;
        break;
      case 57:
        _this.key._9 = true;
        e.preventDefault;
        break;
      case 48:
        _this.key._0 = true;
        e.preventDefault;
        break;

      case 37:
        _this.key.left = true;
        break;
      case 38:
        _this.key.up = true;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = true;
        break;

      case 40:
        _this.key.down = true;
        e.preventDefault;
        break;
    }
  }
};

//Keypress events - Key Released

Vas.prototype.keyup = function(e) {
  var _this = this;

  if (this.controls == "WASD_Arrow") {
    switch (e.keyCode) {
      case 37:
        _this.key.left = false;
        break;
      case 38:
        _this.key.up = false;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = false;
        break;

      case 40:
        _this.key.down = false;
        e.preventDefault;
        break;

      case 65:
        _this.key.left = false;
        break;
      case 87:
        _this.key.up = false;
        e.preventDefault;

        break;
      case 68:
        _this.key.right = false;
        break;
      case 83:
        _this.key.down = false;
        e.preventDefault;
        break;
    }
  } else if (this.controls == "WASD") {
    switch (e.keyCode) {
      case 37:
        _this.key.left = false;
        break;
      case 38:
        _this.key.up = false;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = false;
        break;

      case 40:
        _this.key.down = false;
        e.preventDefault;
        break;
    }
  } else if (this.controls == "custom") {
    switch (e.keyCode) {
      case 65: //a
        _this.key._a = false;
        e.preventDefault;
        break;
      case 66:
        _this.key._b = false;
        e.preventDefault;
        break;
      case 67:
        _this.key._c = false;
        e.preventDefault;
        break;
      case 68:
        _this.key._d = false;
        e.preventDefault;
        break;
      case 69: //e
        _this.key._e = false;
        e.preventDefault;
        break;
      case 70:
        _this.key._f = false;
        e.preventDefault;
        break;
      case 71:
        _this.key._g = false;
        e.preventDefault;
        break;
      case 72:
        _this.key._h = false;
        e.preventDefault;
        break;
      case 73:
        _this.key._i = false;
        e.preventDefault;
        break;
      case 74:
        _this.key._j = false;
        e.preventDefault;
        break;
      case 75: //k
        _this.key._k = false;
        e.preventDefault;
        break;
      case 76:
        _this.key._l = false;
        e.preventDefault;
        break;
      case 77:
        _this.key._m = false;
        e.preventDefault;
        break;
      case 78:
        _this.key._n = false;
        e.preventDefault;
        break;
      case 79:
        _this.key._o = false;
        e.preventDefault;
        break;
      case 80: //p
        _this.key._p = false;
        e.preventDefault;
        break;
      case 81:
        _this.key._q = false;
        e.preventDefault;
        break;
      case 82:
        _this.key._r = false;
        e.preventDefault;
        break;
      case 83:
        _this.key._s = false;
        e.preventDefault;
        break;
      case 84:
        _this.key._t = false;
        e.preventDefault;
        break;
      case 85: //u
        _this.key._u = false;
        e.preventDefault;
        break;
      case 86:
        _this.key._v = false;
        e.preventDefault;
        break;
      case 87:
        _this.key._w = false;
        e.preventDefault;
        break;
      case 88:
        _this.key._x = false;
        e.preventDefault;
        break;
      case 89:
        _this.key._y = false;
        e.preventDefault;
        break;
      case 90:
        _this.key._z = false;
        e.preventDefault;
        break;
      case 49:
        _this.key._1 = false;
        e.preventDefault;
        break;
      case 50:
        _this.key._2 = false;
        e.preventDefault;
        break;
      case 51:
        _this.key._3 = false;
        e.preventDefault;
        break;
      case 52:
        _this.key._4 = false;
        e.preventDefault;
        break;
      case 53:
        _this.key._5 = false;
        e.preventDefault;
        break;
      case 54:
        _this.key._6 = false;
        e.preventDefault;
        break;
      case 55:
        _this.key._7 = false;
        e.preventDefault;
        break;
      case 56:
        _this.key._8 = false;
        e.preventDefault;
        break;
      case 57:
        _this.key._9 = false;
        e.preventDefault;
        break;
      case 48:
        _this.key._0 = false;
        e.preventDefault;
        break;

      case 37:
        _this.key.left = false;
        break;
      case 38:
        _this.key.up = false;
        e.preventDefault;

        break;
      case 39:
        _this.key.right = false;
        break;

      case 40:
        _this.key.down = false;
        e.preventDefault;
        break;
    }
  }
};

//This creates a rectangle at (x,y) with dimensions of width * height and a color of 'color'

// @param {number} x: The top left x coordinate
// @param {number} y: The top left y coordinate
// @param {number} width: The width of the rectangle
// @param {number} height: The height of the rectangle
// @param {number} width: The width of the rectangle
// @param {color value} color: The color of the rectangle (use '')

Vas.prototype.rect = function(x, y, width, height, color) {
  if (this.format == "still") {
    ctx.fillStyle = color;
    ctx.fillRect(x, y, width, height);
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Rectangle could not be drawn. ");
    return;
  }
};

//This creates a circle at (x,y) with a radius of [radius] and a color of 'color'

// @param {number} x: The center x coordinate
// @param {number} y: The center y coordinate
// @param {number} radius: The radius of the circle
// @param {color value} color: The color of the circle (use '')

Vas.prototype.circle = function(x, y, radius, color) {
  if (this.format == "still") {
    ctx.fillStyle = color;
    ctx.beginPath();
    ctx.arc(x, y, radius, 0, Math.PI * 2, true);
    ctx.fill();
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Circle could not be drawn. ");
    return;
  }
};

//This creates a line that is [width] thick, starts at (startX, startY) and ends at (endX, endY), and is colored 'color'

// @param {number} width: The thickness/width of the line
// @param {number} x1: The first(beginning) x coordinate of the line
// @param {number} y1: The first(beginning) y coordinate of the line
// @param {number} x2: The second(end) x coordinate of the line
// @param {number} y2: The second(end) y coordinate of the line
// @param {color value} color: The color of the line (use '')

Vas.prototype.line = function(width, x1, y1, x2, y2, color) {
  if (this.format == "still") {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.strokeStyle = color;
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Line could not be drawn. ");
    return;
  }
};

//This loads an image sprite at (x,y) from an image source of 'src'

// @param {number} x: The top left x coordinate of the image
// @param {number} y: The top left y coordinate of the image
// @param {file} src: The src file of the image (use '')

Vas.prototype.image = function(x, y, src) {
  if (this.format == "still") {
    var img = new Image();
    img.src = src;
    img.addEventListener("load", function() {
      ctx.drawImage(img, x, y);
    });
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Image could not be drawn. ");
    return;
  }
};

//This creates a background with color 'color'

// @param {color value} color: The color of the background (use '')

Vas.prototype.background = function(color) {
  if (this.format == "still") {
    ctx.fillStyle = color;
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Background could not be drawn. ");
    return;
  }
};

//This creates a custom triangle with 3 coordinates and a color

// @param {number} ax: The first(beginning) x coordinate of the triangle
// @param {number} ay: The first(beginning) y coordinate of the triangle
// @param {number} bx: The second(middle) x coordinate of the triangle
// @param {number} by: The second(middle) y coordinate of the triangle
// @param {number} cx: The third(final) x coordinate of the triangle
// @param {number} cy: The third(final) y coordinate of the triangle
// @param {color value} color: The color of the triangle (use '')

Vas.prototype.triangle = function(ax, ay, bx, by, cx, cy, color) {
  if (this.format == "still") {
    ctx.beginPath();
    //ctx.lineWidth = 1;
    ctx.fillStyle = color;
    ctx.moveTo(ax, ay);
    ctx.lineTo(bx, by);
    ctx.lineTo(cx, cy);
    ctx.fill();
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Triangle could not be drawn. ");
    return;
  }
};

//This creates a perfect pentagon with the top vertex at ( x, y ), a side length of [length], and a color of [color]

// @param {number} x: The top x coordinate of the pentagon
// @param {number} y: The top y coordinate of the pentagon
// @param {number} length: The length of each side of the pentagon
// @param {color value} color: The color of the polygon (use '')

Vas.prototype.penta = function(x, y, length, color) {
  if (this.format == "still") {
    ctx.beginPath();
    ctx.fillStyle = color;

    ctx.moveTo(x, y);

    var axi = length * Math.sin(0.9424777961);
    var ayi = length * Math.sin(0.6283185307);

    var ax = x + axi;
    var ay = y + ayi;

    ctx.lineTo(ax, ay);

    var bxi = length * Math.sin(0.3141592654);
    var byi = length * Math.sin(1.256637061);

    var bx = ax - bxi;
    var by = ay + byi;

    ctx.lineTo(bx, by);

    var cx = bx - length;
    var cy = by;

    ctx.lineTo(cx, cy);

    var dxi = length * Math.sin(0.3141592654);
    var dyi = length * Math.sin(1.256637061);

    var dx = cx - dxi;
    var dy = cy - dyi;

    ctx.lineTo(dx, dy);

    ctx.fill();
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Pentagon could not be drawn. ");
    return;
  }
};

//This creates a perfect polygon with number of sides [N] with the top vertex at ( x, y ), a radius length of [r], and a color of [color]

// @param {number} x: The top x coordinate of the polygon
// @param {number} y: The top y coordinate of the polygon
// @param {number} N: The number of sides of the polygon
// @param {number} r: The radius of the polygon
// @param {color value} color: The color of the polygon (use '')

Vas.prototype.polygon = function(x, y, N, r, color) {
  if (this.format == "still") {
    ctx.beginPath();
    ctx.moveTo(x + r * Math.cos(0), y + r * Math.sin(0));

    for (var i = 1; i <= N; i += 1) {
      ctx.lineTo(
        x + r * Math.cos(i * 2 * Math.PI / N),
        y + r * Math.sin(i * 2 * Math.PI / N)
      );
    }

    ctx.strokeStyle = color;
    ctx.lineWidth = 1;
    ctx.stroke();
    ctx.save();
    ctx.fillStyle = color;
    ctx.fill();
    ctx.restore();
    this.numberOfElements += 1;
  } else {
    alert("Invalid Format: Polygon could not be drawn.");
    return;
  }
};

//Takes no parameter, just be wary when using in loops, best to not change this.confirmOnClear, or set it to false.

Vas.prototype.clear = function() {
  if (this.format == "still") {
    if (this.confirmOnClear) {
      var confirmClear = confirm("Are you sure you want to clear the canvas?");
      if (confirmClear) {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        var today = new Date();
        var time =
          today.getHours() +
          ":" +
          today.getMinutes() +
          ":" +
          today.getSeconds();
        console.log("Canvas cleared at: " + time);
      }
    } else {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
  }
};

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console