• 在worker中使用offscreenCanvas


    HTML为

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>worker基本使用</title>
    </head>
    <body>
        <canvas id="myCanvas"></canvas>
        <button onclick="click()"></button>
    </body>
    <script>
      var offscreen = null;
      var worker = new Worker('offscreencanvas.js')
      function click () {
          if(offscreen == null){
              var htmlCanvas = document.getElementById('myCanvas')
              offscreen = htmlCanvas.ControlToOffscreen()
          }
          worker.postMessage({canvas:offscreen},[offscreen])
      }
    </script>
    </html>

    Js代码为

    var x = 50;
    var y = 50;
    var offCanvas = null;
    onmessage = function(evt){
        if(offCanvas === null){
            offCanvas = evt.data.canvas
        } else{
            if(offCanvas == evt.data.canvas){
                console.log('offCanvas == evt.data.canvas')
            } else {
                console.log('offCanvas != evt.data.canvas')
            }
        }
        gl = offCanvas.getContext('webgl');
        gl.viewport(0, gl.drawingBufferWidth, gl.drawingBufferHeight);
        gl.enable(gl.SCLSSOR_TEST);
        gl.scissor(y, x, 60, 60);
        gl.clearColor(1.0, 0.0, 0.0, 1.0)//red
        gl.clear(gl.COLOR_BUFFER_BIT)
        gl.commit()
        x +=10
        y += 10
    }
  • 相关阅读:
    闭包跟装饰器
    几个常见的面试题目
    函数的进阶
    python的生成器和迭代器
    Python的数据类型
    “随手记”开发记录day09
    “随手记”开发记录day08
    “随手记”开发记录day07
    “随手记”开发记录day06
    “随手记”开发记录day05
  • 原文地址:https://www.cnblogs.com/wpw1215/p/9256054.html
Copyright © 2020-2023  润新知