123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { Global } from "./Global.js";
- import { Rectangle } from "./spatial/Rectangle.js";
- export class ScaledCanvas {
- constructor(container) {
- this.canvas = null;
- this.context = null;
- this.container = container;
- }
-
- clearFrame() {
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
- }
-
- init() {
- this.canvas = document.createElement("canvas");
- this.container.appendChild(this.canvas);
- this.context = this.canvas.getContext('2d');
-
- window.addEventListener("resize", this.onResize);
- this.onResize();
- }
-
- getPixelRatio() {
- let dpr = window.devicePixelRatio || 1;
- let bsr = this.context.webkitBackingStorePixelRatio ||
- this.context.mozBackingStorePixelRatio ||
- this.context.msBackingStorePixelRatio ||
- this.context.oBackingStorePixelRatio ||
- this.context.backingStorePixelRatio || 1;
-
- return dpr / bsr;
- }
-
- onResize = () => {
- let ratio = this.getPixelRatio();
- let canvasBounds = this.canvas.getBoundingClientRect();
- this.canvas.width = canvasBounds.width * ratio;
- this.canvas.height = canvasBounds.height * ratio;
- this.context.scale(ratio, ratio);
- Global.screenBounds = this.bounds;
- }
- getContext() {
- return this.context;
- }
- get pixelWidth() {
- return this.canvas.width;
- }
- get pixelHeight() {
- return this.canvas.height;
- }
- get center() {
- return this.bounds.center;
- }
- get bounds() {
- return new Rectangle(0,0, this.canvas.width / this.getPixelRatio(), this.canvas.height / this.getPixelRatio());
- }
- }
|