You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Texture.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Rect from "./Rect.js";
  2. import assets from "./assets.js";
  3. export default class Texture {
  4. constructor(nestedArr) {
  5. this.nestedArr = nestedArr;
  6. this.can = document.createElement("canvas");
  7. this.ctx = this.can.getContext("2d");
  8. this.width = 0;
  9. this.height = 0;
  10. assets.tiles.whenReady(() => this.init());
  11. }
  12. findWidthHeight(arr) {
  13. arr.forEach(e => {
  14. if (e instanceof Array) {
  15. this.findWidthHeight(e);
  16. } else {
  17. let right = (e.x + 1) * assets.tiles.tileWidth;
  18. let bottom = (e.y + 1) * assets.tiles.tileHeight;
  19. if (right > this.width)
  20. this.width = right;
  21. if (bottom > this.height)
  22. this.height = bottom;
  23. }
  24. });
  25. }
  26. fillCanvas(arr) {
  27. arr.forEach(e => {
  28. if (e instanceof Array) {
  29. this.fillCanvas(e);
  30. } else {
  31. assets.tiles.drawTile(this.ctx, e.tile, e.x, e.y);
  32. }
  33. });
  34. }
  35. init() {
  36. this.findWidthHeight(this.nestedArr);
  37. this.can.width = this.width;
  38. this.can.height = this.height;
  39. this.fillCanvas(this.nestedArr);
  40. }
  41. draw(ctx, x, y) {
  42. ctx.drawImage(this.can, x, y, this.width, this.height);
  43. }
  44. }
  45. Texture.fromLine = function(l, m, r, lr) {
  46. }