(tiles)
| 57 | } |
| 58 | |
| 59 | async draw(tiles) { |
| 60 | this.tempBuffer = null; |
| 61 | if (!this.image) { |
| 62 | // Generate baseimage |
| 63 | const baselayer = sharp({ |
| 64 | create: { |
| 65 | width: this.width, |
| 66 | height: this.height, |
| 67 | channels: 4, |
| 68 | background: { |
| 69 | r: 0, g: 0, b: 0, alpha: 0, |
| 70 | }, |
| 71 | }, |
| 72 | }); |
| 73 | // Save baseImage as buffer |
| 74 | this.tempBuffer = await baselayer.png().toBuffer(); |
| 75 | } else { |
| 76 | this.tempBuffer = this.image; |
| 77 | } |
| 78 | |
| 79 | // Prepare tiles for composing baselayer |
| 80 | const tileParts = []; |
| 81 | tiles.forEach((tile, i) => { |
| 82 | tileParts.push(this.prepareTileParts(tile, i)); |
| 83 | }); |
| 84 | |
| 85 | const preparedTiles = (await Promise.all(tileParts)).filter((v) => v.success); |
| 86 | |
| 87 | // Compose all prepared tiles to the baselayer |
| 88 | const preparedTilesForSharp = preparedTiles |
| 89 | .filter((preparedTile) => !!preparedTile) // remove non-existing tiles |
| 90 | .map((preparedTile) => { |
| 91 | const { position, data } = preparedTile; |
| 92 | position.top = Math.round(position.top); |
| 93 | position.left = Math.round(position.left); |
| 94 | return { input: data, ...position }; |
| 95 | }); |
| 96 | |
| 97 | this.tempBuffer = await sharp(this.tempBuffer) |
| 98 | .composite(preparedTilesForSharp) |
| 99 | .toBuffer(); |
| 100 | |
| 101 | this.image = this.tempBuffer; |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Save image to file |
no test coverage detected