<small id='vQstP'></small><noframes id='vQstP'>

    <bdo id='vQstP'></bdo><ul id='vQstP'></ul>

  1. <legend id='vQstP'><style id='vQstP'><dir id='vQstP'><q id='vQstP'></q></dir></style></legend>
    1. <tfoot id='vQstP'></tfoot>
      <i id='vQstP'><tr id='vQstP'><dt id='vQstP'><q id='vQstP'><span id='vQstP'><b id='vQstP'><form id='vQstP'><ins id='vQstP'></ins><ul id='vQstP'></ul><sub id='vQstP'></sub></form><legend id='vQstP'></legend><bdo id='vQstP'><pre id='vQstP'><center id='vQstP'></center></pre></bdo></b><th id='vQstP'></th></span></q></dt></tr></i><div id='vQstP'><tfoot id='vQstP'></tfoot><dl id='vQstP'><fieldset id='vQstP'></fieldset></dl></div>
    2. 无法克隆 OffscreenCanvas,因为它已分离

      An OffscreenCanvas could not be cloned because it was detached(无法克隆 OffscreenCanvas,因为它已分离)
        <i id='YpPIM'><tr id='YpPIM'><dt id='YpPIM'><q id='YpPIM'><span id='YpPIM'><b id='YpPIM'><form id='YpPIM'><ins id='YpPIM'></ins><ul id='YpPIM'></ul><sub id='YpPIM'></sub></form><legend id='YpPIM'></legend><bdo id='YpPIM'><pre id='YpPIM'><center id='YpPIM'></center></pre></bdo></b><th id='YpPIM'></th></span></q></dt></tr></i><div id='YpPIM'><tfoot id='YpPIM'></tfoot><dl id='YpPIM'><fieldset id='YpPIM'></fieldset></dl></div>

          <tbody id='YpPIM'></tbody>
        <tfoot id='YpPIM'></tfoot>

        1. <legend id='YpPIM'><style id='YpPIM'><dir id='YpPIM'><q id='YpPIM'></q></dir></style></legend>

          • <small id='YpPIM'></small><noframes id='YpPIM'>

              • <bdo id='YpPIM'></bdo><ul id='YpPIM'></ul>
              • 本文介绍了无法克隆 OffscreenCanvas,因为它已分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                昨天回到了我对HTML画布的爱好,并试图在一个单独的线程中进行渲染,只是为了在控制台中遇到以下错误:

                yesterday returned to my hobby with HTML canvas and tried to do a rendering in a separate thread, just to face the following error in the console:

                未捕获的 DOMException:无法在Worker"上执行postMessage":无法克隆 OffscreenCanvas,因为它已分离.在主要(http://localhost:8000/responsivetemplate/:47:14)

                Uncaught DOMException: Failed to execute 'postMessage' on 'Worker': An OffscreenCanvas could not be cloned because it was detached. at main (http://localhost:8000/responsivetemplate/:47:14)

                [index.html]

                [index.html]

                <!DOCTYPE HTML>
                <html>
                <head>
                <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
                <meta http-equiv="cache-control" content="max-age=0" />
                <meta http-equiv="cache-control" content="no-cache" />
                <meta http-equiv="cache-control" content="must-revalidate" />
                <meta http-equiv="expires" content="0" />
                <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
                <meta http-equiv="pragma" content="no-cache" />
                <meta charset="UTF-8" />
                <title>OffscreenCanvas</title>
                </head>
                <body>
                  <div id='wrapper'>
                    <div id='content'>
                      <canvas id="testcanvas" width="512" height="512"></canvas>
                    </div>
                  </div>
                </body>
                <script>
                'use strict';
                
                document.body.onload = function () {
                  var canvas = document.getElementById('testcanvas');
                  canvas.imageSmoothingEnabled = false;
                
                  const offscreenCanvas = canvas.transferControlToOffscreen();
                
                  const worker = new Worker('render.js');
                
                  var speed = 100;
                
                  var currentTime = 0; var timeDiff = 0; var lastTime = 0;
                  var timeProduct = 0; var dTime = 0; var timeScale = 1; var timeStep = 0.01;
                
                  var posX = 10; var posY = 10;
                
                  function main() {
                    currentTime = performance.now();
                    timeDiff = (Math.abs(currentTime - lastTime) * 0.001);
                    dTime += Math.min(timeDiff, 1);
                    timeProduct = timeStep * timeScale;
                
                    while (dTime > timeProduct) {
                      //draw();
                      worker.postMessage({canvas: offscreenCanvas, posX: posX, posY: posY}, [offscreenCanvas]);
                      dTime -= timeProduct;
                    }
                
                    lastTime = currentTime;
                
                    posX += speed * timeDiff;
                    posY += speed * timeDiff;
                
                    if (posX > 500) posX = 10;
                    if (posY > 500) posY = 10;
                
                    requestAnimationFrame(main);
                  }
                
                  requestAnimationFrame(main);
                };
                </script>
                </html>
                

                [worker.js]

                [worker.js]

                'use strict';
                
                var canvas;
                var ctx;
                
                function draw(posX, posY) {
                  //clear
                  ctx.setTransform(1,0,0,1,0,0);
                  ctx.clearRect(0, 0, canvas.width, canvas.height);
                  ctx.fillStyle = "#000000";
                  ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                  //draw
                  ctx.beginPath();
                  ctx.moveTo(posX, posY);
                  ctx.ellipse(posX,
                              posY,
                              5,
                              5,
                              0.7854,
                              0,
                              2 * Math.PI,
                              false);
                  ctx.strokeStyle = "white";
                  ctx.stroke();
                }
                
                onmessage = function(ev) {
                  if(ev.data) {
                    if (!canvas) canvas = ev.data.canvas;
                    if (!ctx) ctx = canvas.getContext('2d');
                    var posX = ev.data.posX; var posY = ev.data.posY;
                    draw(posX, posY);
                  }
                }
                

                我不知道导致此错误的原因 - 代码仅在主线程中运行时工作正常,还检查了一些站点,我确定控件已发送到屏幕外:

                I don't know what causes this error - code works fine when run only in the main thread, also already checked some sites and I'm sure the controll was sent to offscreen :

                推荐答案

                你应该只向worker发送一次OffscreenCanvas:

                You should send OffscreenCanvas to worker only once:

                worker.postMessage({canvas: offscreenCanvas}, [offscreenCanvas]);
                
                while (dTime > timeProduct) {
                    worker.postMessage({ posX, posY });
                    dTime -= timeProduct;
                }
                

                这篇关于无法克隆 OffscreenCanvas,因为它已分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                <i id='D6Pol'><tr id='D6Pol'><dt id='D6Pol'><q id='D6Pol'><span id='D6Pol'><b id='D6Pol'><form id='D6Pol'><ins id='D6Pol'></ins><ul id='D6Pol'></ul><sub id='D6Pol'></sub></form><legend id='D6Pol'></legend><bdo id='D6Pol'><pre id='D6Pol'><center id='D6Pol'></center></pre></bdo></b><th id='D6Pol'></th></span></q></dt></tr></i><div id='D6Pol'><tfoot id='D6Pol'></tfoot><dl id='D6Pol'><fieldset id='D6Pol'></fieldset></dl></div>
                  • <tfoot id='D6Pol'></tfoot>
                  • <small id='D6Pol'></small><noframes id='D6Pol'>

                      • <bdo id='D6Pol'></bdo><ul id='D6Pol'></ul>

                      • <legend id='D6Pol'><style id='D6Pol'><dir id='D6Pol'><q id='D6Pol'></q></dir></style></legend>

                            <tbody id='D6Pol'></tbody>