Пожалуйста, используйте следующий способ для вызова renderToCanvas
, так как задание повторного рендеринга выполняется асинхронно c, и загрузка изображения также выполняется асинхронно c.
markup.renderToCanvas(ctx, function() {
var canvas = document.getElementById('snapshot');
const a = document.createElement('a');
//a.style = 'display: none';
document.body.appendChild(a);
a.href = canvas.toDataURL();
a.download = 'markup.png';
a.click();
document.body.removeChild(a);
}, true);
Полный код:
function snaphot() {
var screenshot = new Image();
screenshot.onload = function () {
viewer.loadExtension('Autodesk.Viewing.MarkupsCore').then(function (markupCore) {
// load the markups
markupCore.show();
markupCore.loadMarkups(markupSVG, "layerName");
// ideally should also restore the state of Viewer for this markup
// prepare to render the markups
var canvas = document.getElementById('snapshot');
canvas.width = viewer.container.clientWidth;
canvas.height = viewer.container.clientHeight;
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(screenshot, 0, 0, canvas.width, canvas.height);
markupCore.renderToCanvas(ctx, function() {
// hide the markups
//markupCore.hide();
var canvas = document.getElementById('snapshot');
const a = document.createElement('a');
//a.style = 'display: none';
document.body.appendChild(a);
a.href = canvas.toDataURL();
a.download = 'markup.png';
a.click();
document.body.removeChild(a);
}, true);
// hide the markups
markupCore.hide();
});
};
// Get the full image
viewer.getScreenShot(viewer.container.clientWidth, viewer.container.clientHeight, function (blobURL) {
screenshot.src = blobURL;
});