| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | import Display from './display.js'; |
| 7 | |
| 8 | export default class CageBrowserDisplay extends Display { |
| 9 | autoscale() { |
| 10 | |
| 11 | |
| 12 | this._target.style.width = '100%'; |
| 13 | this._target.style.height = '100%'; |
| 14 | } |
| 15 | |
| 16 | absX(x) { |
| 17 | const width = this._target.getBoundingClientRect().width; |
| 18 | return width ? Math.floor(x * this.width / width) : 0; |
| 19 | } |
| 20 | |
| 21 | absY(y) { |
| 22 | const height = this._target.getBoundingClientRect().height; |
| 23 | return height ? Math.floor(y * this.height / height) : 0; |
| 24 | } |
| 25 | |
| 26 | resize(width, height) { |
| 27 | if (!this.width || !this.height) { |
| 28 | super.resize(width, height); |
| 29 | return; |
| 30 | } |
| 31 | if (this.width === width && this.height === height) return; |
| 32 | |
| 33 | const previous = this._backbuffer; |
| 34 | const next = document.createElement('canvas'); |
| 35 | next.width = width; |
| 36 | next.height = height; |
| 37 | const ctx = next.getContext('2d'); |
| 38 | |
| 39 | |
| 40 | ctx.drawImage(previous, 0, 0); |
| 41 | |
| 42 | |
| 43 | |
| 44 | this._drawCtx.globalCompositeOperation = 'copy'; |
| 45 | this._drawCtx.drawImage(this._target, 0, 0); |
| 46 | this._target.width = width; |
| 47 | this._target.height = height; |
| 48 | this._targetCtx.drawImage(previous, 0, 0, width, height); |
| 49 | previous.width = 0; |
| 50 | |
| 51 | this._backbuffer = next; |
| 52 | this._drawCtx = ctx; |
| 53 | this._prevDrawStyle = ''; |
| 54 | this._fbWidth = this._viewportLoc.w = width; |
| 55 | this._fbHeight = this._viewportLoc.h = height; |
| 56 | } |
| 57 | } |