Advertisement
Guest User

Map avec Canvas

a guest
Jan 29th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   var map = new Tileset("tileset.png");
  2.  
  3.   function Tileset(url) {
  4.     this.image = new Image();
  5.     this.image.referenceDuTileset = this;
  6.  
  7.     this.image.onload = function() {
  8.         this.referenceDuTileset.largeur = this.width / 32;
  9.     }
  10.  
  11.     this.image.src = url;
  12.  
  13.     Tileset.prototype.dessinerTile = function(numero, context, xDestination, yDestination) {
  14.       var xSourceEnTiles = numero % this.largeur;
  15.       if (xSourceEnTiles == 0) {
  16.         xSourceEnTiles = this.largeur;
  17.       }
  18.       var ySourceEnTiles = Math.ceil(numero / this.largeur);
  19.       var xSource = (xSourceEnTiles - 1) * 32;
  20.       var ySource = (ySourceEnTiles - 1) * 32;
  21.       context.drawImage(this.image, xSource, ySource, 32, 32, xDestination, yDestination, 32, 32);
  22.     }
  23.   }
  24.  
  25.   window.onload = function() {
  26.     var canvas = document.getElementById('canvas');
  27.     var ctx = canvas.getContext('2d');
  28.  
  29.     map.dessinerTile(2, ctx, 32 * c + 160 * ml, 32 * l + 128 * mc);
  30.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement