Autodesk Forge 获取带有标记的屏幕截图 [英] Autodesk Forge get screenshot with markups

查看:22
本文介绍了Autodesk Forge 获取带有标记的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 Autodesk forge 查看器的标记扩展.我想获得带有标记的屏幕截图,但只获得了没有标记的屏幕截图.我尝试使用此代码获取屏幕截图

I have a markup extension for autodesk forge viewer. I want to get screenshot with markups, but only got screenshot without markups. I try to get screenshot with this code

          getScreenShoot.addEventListener('click', () => {
            let screenshot = new Image();
            markup.leaveEditMode();
            markupsData = JSON.parse(localStorage.getItem('markupsData'));
            markupsData.map((mark, index) => {
              markup.loadMarkups(mark, `markups-svg + ${index}`);
            });
            let canvas = document.getElementById('snapshot');
            canvas.width = viewer.container.clientWidth;
            canvas.height = viewer.container.clientHeight;
            let ctx = canvas.getContext('2d');
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.drawImage(screenshot, 0, 0, canvas.width, canvas.height);
            markup.renderToCanvas(ctx);

            viewer.getScreenShot(viewer.container.clientWidth, viewer.container.clientHeight, function (blobURL) {
              screenshot.src = blobURL;
              var saveImg = document.createElement('a');
              screenshot.appendChild(saveImg);
              saveImg.style.display = 'none';
              saveImg.href = blobURL;
              saveImg.download = "screenshot.jpg";
              document.body.appendChild(saveImg);
              saveImg.click();
              markup.hide();
              markup.leaveEditMode();
              markupTools.style.display = 'none';
            });
    })````
Please help me to find a trouble in this code, maybe it's not supported in viewer 7.*

推荐答案

请使用以下方式调用 renderToCanvas,因为重新渲染作业内部是异步过程,图像加载也是异步的.

Please use the following way to call renderToCanvas, since the rerender job is async process internally and the image loading is async, too.

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;
});

这篇关于Autodesk Forge 获取带有标记的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆