1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73

content / test / data / gpu / pixel_offscreenCanvas_2d_resize_on_worker.html [blame]

<!DOCTYPE HTML>

<html>
<head>
<title>OffscreenCanvas2d with resizing: red rectangle in the middle.</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>
<script id="myWorker" type="text/worker">
self.onmessage = function(e) {
  var transferred = e.data;
  var ctx2d = transferred.getContext('2d');
  ctx2d.fillStyle = "purple";
  ctx2d.fillRect(0, 0, transferred.width, transferred.height);
  // The placeholder canvas should initially display a 200X200 purple image and
  // then display a 40X50 green image containing a red rectangle in the middle.
  requestAnimationFrame(() => {
      // Resize offscreenCanvas from 200X200 to 40X50.
      transferred.width = 40;
      transferred.height = 50;

      ctx2d.fillStyle = "green";
      ctx2d.fillRect(0, 0, transferred.width, transferred.height);
      ctx2d.fillStyle = "red";
      ctx2d.fillRect(10, 10, 20, 20);
      requestAnimationFrame(() => {
        self.postMessage("");
      });
   });
};
</script>
<script>
var g_swapsBeforeAck = 15;

function makeWorker(script)
{
   var blob = new Blob([script]);
   return new Worker(URL.createObjectURL(blob));
}

function waitForFinish()
{
  if (g_swapsBeforeAck == 0) {
    domAutomationController.send("SUCCESS");
  } else {
    g_swapsBeforeAck--;
    document.getElementById('container').style.zIndex = g_swapsBeforeAck + 1;
    window.requestAnimationFrame(waitForFinish);
  }
}

function main()
{
  var canvas2D = document.getElementById("c");
  var offscreenCanvas = canvas2D.transferControlToOffscreen();
  var worker = makeWorker(document.getElementById("myWorker").textContent);
  worker.onmessage = function (e) {
      waitForFinish();
  };
  worker.postMessage(offscreenCanvas, [offscreenCanvas]);
}
</script>
</head>
<body onload="main()">
<div style="position:relative; width:200px; height:200px; background-color:white">
</div>
<div id="container" style="position:absolute; top:0px; left:0px">
<canvas id="c" width="200" height="200" class="nomargin"></canvas>
</div>
</body>
</html>