|
| 1 | +function findNode(root, w, h) { |
| 2 | + if (root.used) { |
| 3 | + return findNode(root.right, w, h) || findNode(root.down, w, h); |
| 4 | + } else if ((w <= root.w) && (h <= root.h)) { |
| 5 | + return root; |
| 6 | + } else { |
| 7 | + return null; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +function splitNode(node, w, h) { |
| 12 | + node.used = true; |
| 13 | + node.down = {x: node.x, y: node.y + h, w: node.w, h: node.h - h}; |
| 14 | + node.right = {x: node.x + w, y: node.y, w: node.w - w, h: h}; |
| 15 | + return node; |
| 16 | +} |
| 17 | + |
1 | 18 | export default class TexturePacker {
|
2 | 19 |
|
3 |
| - constructor(canvas) { |
| 20 | + constructor(canvas, {padding, prefix, path}) { |
4 | 21 |
|
5 | 22 | this.canvas = canvas;
|
6 | 23 | this.textures = [];
|
7 | 24 |
|
| 25 | + this.root = { |
| 26 | + x: 0, // origin x |
| 27 | + y: 0, // origin y |
| 28 | + w: 256 - padding, // width |
| 29 | + h: 256 - padding, // height |
| 30 | + p: padding |
| 31 | + }; |
| 32 | + |
| 33 | + this.prefix = prefix; |
| 34 | + this.path = path; |
| 35 | + |
| 36 | + console.log('packer', this); |
| 37 | + |
8 | 38 | }
|
9 | 39 |
|
10 | 40 | addTexture(texture) {
|
11 | 41 | this.textures.push(texture);
|
12 | 42 | }
|
13 | 43 |
|
14 | 44 | sort() {
|
15 |
| - console.log('sort', this.textures); |
| 45 | + this.textures.sort((a, b) => { |
| 46 | + if (a.h < b.h) { |
| 47 | + return 1; |
| 48 | + } |
| 49 | + if (a.h > b.h) { |
| 50 | + return -1; |
| 51 | + } |
| 52 | + return 0; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + fit() { |
| 57 | + let i, node, texture, pad = this.root.p; |
| 58 | + for (i = 0; i < this.textures.length; i++) { |
| 59 | + texture = this.textures[i]; |
| 60 | + texture.fit = false; |
| 61 | + node = findNode(this.root, texture.w + pad, texture.h + pad); |
| 62 | + if (node) { |
| 63 | + texture.fit = splitNode(node, texture.w + pad, texture.h + pad); |
| 64 | + } |
| 65 | + if (!texture.fit) { |
| 66 | + this.resize(); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + resize() { |
| 73 | + let w, h, pad = this.root.p; |
| 74 | + if (this.root.w > this.root.h) { |
| 75 | + w = (this.root.w + pad); |
| 76 | + h = (this.root.h + pad) * 2; |
| 77 | + } else { |
| 78 | + w = (this.root.w + pad) * 2; |
| 79 | + h = (this.root.h + pad); |
| 80 | + } |
| 81 | + this.root = { |
| 82 | + x: 0, // origin x |
| 83 | + y: 0, // origin y |
| 84 | + w: w - pad, // width |
| 85 | + h: h - pad, // height |
| 86 | + p: pad |
| 87 | + }; |
| 88 | + this.fit(); |
| 89 | + } |
| 90 | + |
| 91 | + draw() { |
| 92 | + |
| 93 | + // TODO: Calc CSS output |
| 94 | + |
| 95 | + let canvas = this.canvas; |
| 96 | + let ctx = canvas.getContext('2d'); |
| 97 | + let pad = this.root.p; |
| 98 | + let width = this.root.w + pad; |
| 99 | + let height = this.root.h + pad; |
| 100 | + |
| 101 | + canvas.width = width; |
| 102 | + canvas.height = height; |
| 103 | + |
| 104 | + ctx.clearRect(0, 0, width, height); |
| 105 | + |
| 106 | + this.textures.sort((a, b) => { |
| 107 | + let nameA = a.name.toUpperCase(); |
| 108 | + let nameB = b.name.toUpperCase(); |
| 109 | + return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; |
| 110 | + }); |
| 111 | + |
| 112 | + for(let i = 0; i < this.textures.length; i++) { |
| 113 | + let texture = this.textures[i]; |
| 114 | + if(texture.fit) { |
| 115 | + |
| 116 | + // turn on for testing |
| 117 | + ctx.fillRect(texture.fit.x + pad, texture.fit.y + pad, texture.w, texture.h); |
| 118 | + ctx.stroke(); |
| 119 | + |
| 120 | + ctx.drawImage(texture.img, texture.fit.x + pad, texture.fit.y + pad); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + update() { |
| 127 | + // TODO: Update texture packer when settings change or images are added/removed |
16 | 128 | }
|
17 | 129 |
|
18 | 130 | }
|
0 commit comments