UNPKG

molstar

Version:

A comprehensive macromolecular library.

70 lines 2.93 kB
"use strict"; /** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.canvasToBlob = exports.resizeCanvas = exports.setCanvasSize = void 0; var tslib_1 = require("tslib"); /** Set canvas size taking `devicePixelRatio` into account */ function setCanvasSize(canvas, width, height, scale) { if (scale === void 0) { scale = 1; } canvas.width = Math.round(window.devicePixelRatio * scale * width); canvas.height = Math.round(window.devicePixelRatio * scale * height); Object.assign(canvas.style, { width: width + "px", height: height + "px" }); } exports.setCanvasSize = setCanvasSize; /** Resize canvas to container element taking `devicePixelRatio` into account */ function resizeCanvas(canvas, container, scale) { if (scale === void 0) { scale = 1; } var width = window.innerWidth; var height = window.innerHeight; if (container !== document.body) { // fixes issue #molstar/molstar#147, offsetWidth/offsetHeight is correct size when css transform:scale is used width = container.offsetWidth; height = container.offsetHeight; } setCanvasSize(canvas, width, height, scale); } exports.resizeCanvas = resizeCanvas; function _canvasToBlob(canvas, callback, type, quality) { var bin = atob(canvas.toDataURL(type, quality).split(',')[1]); var len = bin.length; var len32 = len >> 2; var a8 = new Uint8Array(len); var a32 = new Uint32Array(a8.buffer, 0, len32); var j = 0; for (var i = 0; i < len32; ++i) { a32[i] = bin.charCodeAt(j++) | bin.charCodeAt(j++) << 8 | bin.charCodeAt(j++) << 16 | bin.charCodeAt(j++) << 24; } var tailLength = len & 3; while (tailLength--) a8[j] = bin.charCodeAt(j++); callback(new Blob([a8], { type: type || 'image/png' })); } function canvasToBlob(canvas, type, quality) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { var callback = function (blob) { if (blob) resolve(blob); else reject('no blob returned'); }; if (!HTMLCanvasElement.prototype.toBlob) { _canvasToBlob(canvas, callback, type, quality); } else { canvas.toBlob(callback, type, quality); } })]; }); }); } exports.canvasToBlob = canvasToBlob; //# sourceMappingURL=util.js.map