UNPKG

konva

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/konvajs/konvajs.github.io/master/apple-touch-icon-180x180.png" alt="Konva logo" height="180" /> </p>

133 lines (132 loc) 5.01 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Util_1 = require("./Util"); var Context_1 = require("./Context"); var Global_1 = require("./Global"); var Factory_1 = require("./Factory"); var Validators_1 = require("./Validators"); var _pixelRatio; function getDevicePixelRatio() { if (_pixelRatio) { return _pixelRatio; } var canvas = Util_1.Util.createCanvasElement(); var context = canvas.getContext('2d'); _pixelRatio = (function () { var devicePixelRatio = Global_1.Konva._global.devicePixelRatio || 1, backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; return devicePixelRatio / backingStoreRatio; })(); return _pixelRatio; } var Canvas = (function () { function Canvas(config) { this.pixelRatio = 1; this.width = 0; this.height = 0; this.isCache = false; var conf = config || {}; var pixelRatio = conf.pixelRatio || Global_1.Konva.pixelRatio || getDevicePixelRatio(); this.pixelRatio = pixelRatio; this._canvas = Util_1.Util.createCanvasElement(); this._canvas.style.padding = '0'; this._canvas.style.margin = '0'; this._canvas.style.border = '0'; this._canvas.style.background = 'transparent'; this._canvas.style.position = 'absolute'; this._canvas.style.top = '0'; this._canvas.style.left = '0'; } Canvas.prototype.getContext = function () { return this.context; }; Canvas.prototype.getPixelRatio = function () { return this.pixelRatio; }; Canvas.prototype.setPixelRatio = function (pixelRatio) { var previousRatio = this.pixelRatio; this.pixelRatio = pixelRatio; this.setSize(this.getWidth() / previousRatio, this.getHeight() / previousRatio); }; Canvas.prototype.setWidth = function (width) { this.width = this._canvas.width = width * this.pixelRatio; this._canvas.style.width = width + 'px'; var pixelRatio = this.pixelRatio, _context = this.getContext()._context; _context.scale(pixelRatio, pixelRatio); }; Canvas.prototype.setHeight = function (height) { this.height = this._canvas.height = height * this.pixelRatio; this._canvas.style.height = height + 'px'; var pixelRatio = this.pixelRatio, _context = this.getContext()._context; _context.scale(pixelRatio, pixelRatio); }; Canvas.prototype.getWidth = function () { return this.width; }; Canvas.prototype.getHeight = function () { return this.height; }; Canvas.prototype.setSize = function (width, height) { this.setWidth(width); this.setHeight(height); }; Canvas.prototype.toDataURL = function (mimeType, quality) { try { return this._canvas.toDataURL(mimeType, quality); } catch (e) { try { return this._canvas.toDataURL(); } catch (err) { Util_1.Util.error('Unable to get data URL. ' + err.message); return ''; } } }; return Canvas; }()); exports.Canvas = Canvas; Factory_1.Factory.addGetterSetter(Canvas, 'pixelRatio', undefined, Validators_1.getNumberValidator()); var SceneCanvas = (function (_super) { __extends(SceneCanvas, _super); function SceneCanvas(config) { if (config === void 0) { config = { width: 0, height: 0 }; } var _this = _super.call(this, config) || this; _this.context = new Context_1.SceneContext(_this); _this.setSize(config.width, config.height); return _this; } return SceneCanvas; }(Canvas)); exports.SceneCanvas = SceneCanvas; var HitCanvas = (function (_super) { __extends(HitCanvas, _super); function HitCanvas(config) { if (config === void 0) { config = { width: 0, height: 0 }; } var _this = _super.call(this, config) || this; _this.hitCanvas = true; _this.context = new Context_1.HitContext(_this); _this.setSize(config.width, config.height); return _this; } return HitCanvas; }(Canvas)); exports.HitCanvas = HitCanvas;