UNPKG

molstar

Version:

A comprehensive macromolecular library.

64 lines 2.66 kB
/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __awaiter, __generator } from "tslib"; /** Set canvas size taking `devicePixelRatio` into account */ export 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" }); } /** Resize canvas to container element taking `devicePixelRatio` into account */ export 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); } 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' })); } export function canvasToBlob(canvas, type, quality) { return __awaiter(this, void 0, void 0, function () { return __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); } })]; }); }); } //# sourceMappingURL=util.js.map