UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

54 lines (53 loc) 2.15 kB
import { CanvasSequence } from 'canvas-sequencer-ts'; import { blobToDataURL } from "./blobToDataURL.js"; import { createCanvas, createImageBitmap } from "./offscreenCanvasPonyfill.js"; export async function renderToAbstractCanvas(width, height, opts, cb) { const { exportSVG, highResolutionScaling = 1 } = opts; const safeWidth = Math.max(1, width); const safeHeight = Math.max(1, height); if (exportSVG) { if (!exportSVG.rasterizeLayers) { const fakeCtx = new CanvasSequence(); const callbackResult = await cb(fakeCtx); return { ...callbackResult, canvasRecordedData: fakeCtx.toJSON(), }; } else { const s = exportSVG.scale || highResolutionScaling; const canvas = createCanvas(Math.ceil(safeWidth * s), Math.ceil(safeHeight * s)); const ctx = canvas.getContext('2d'); if (!ctx) { throw new Error('2d canvas rendering not supported on this platform'); } if (s !== 1) { ctx.scale(s, s); } const callbackResult = await cb(ctx); return { ...callbackResult, html: `<image width="${width}" height="${height}" href="${'convertToBlob' in canvas ? await blobToDataURL(await canvas.convertToBlob({ type: 'image/png', })) : canvas.toDataURL('image/png')}" />`, }; } } else { const canvas = createCanvas(Math.ceil(safeWidth * highResolutionScaling), Math.ceil(safeHeight * highResolutionScaling)); const ctx = canvas.getContext('2d'); if (!ctx) { throw new Error('2d canvas rendering not supported on this platform'); } if (highResolutionScaling !== 1) { ctx.scale(highResolutionScaling, highResolutionScaling); } const callbackResult = await cb(ctx); return { ...callbackResult, imageData: await createImageBitmap(canvas), }; } }