From 9873f74cd1a5514e126d25cc256aaaf66859e075 Mon Sep 17 00:00:00 2001
From: MoyuScript <i@moyu.moe>
Date: Mon, 11 Dec 2017 20:17:20 +0800
Subject: [PATCH] Add support for rendering webgl canvas content (#646)

---
 src/Clone.js                      | 14 ++++++-------
 tests/reftests/images/canvas.html | 33 +++++++++++++++++--------------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/src/Clone.js b/src/Clone.js
index 53e94bd..e944fad 100644
--- a/src/Clone.js
+++ b/src/Clone.js
@@ -346,13 +346,13 @@ const cloneCanvasContents = (canvas: HTMLCanvasElement, clonedCanvas: HTMLCanvas
         if (clonedCanvas) {
             clonedCanvas.width = canvas.width;
             clonedCanvas.height = canvas.height;
-            clonedCanvas
-                .getContext('2d')
-                .putImageData(
-                    canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height),
-                    0,
-                    0
-                );
+            const ctx = canvas.getContext('2d');
+            const clonedCtx = clonedCanvas.getContext('2d');
+            if (ctx) {
+                clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0);
+            } else {
+                clonedCtx.drawImage(canvas, 0, 0);
+            }
         }
     } catch (e) {}
 };
diff --git a/tests/reftests/images/canvas.html b/tests/reftests/images/canvas.html
index 2a876c4..f601d54 100644
--- a/tests/reftests/images/canvas.html
+++ b/tests/reftests/images/canvas.html
@@ -1,27 +1,30 @@
 <!DOCTYPE html>
 <html>
-  <head>
+<head>
     <title>Image tests</title>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
     <script type="text/javascript" src="../../test.js"></script>
     <script type="text/javascript">
-      function setUp() {
-        if ($('#testcanvas')[0].getContext) {
-          var ctx = $('#testcanvas')[0].getContext('2d');
+        function setUp() {
+            var ctx = document.querySelector('#testcanvas').getContext('2d');
 
-          ctx.fillStyle = "rgb(200,0,0)";
-          ctx.fillRect (10, 10, 55, 50);
+            ctx.fillStyle = "rgb(200,0,0)";
+            ctx.fillRect (10, 10, 55, 50);
 
-          ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
-          ctx.fillRect (30, 30, 55, 50);
-        } else {
-          $('#testcanvas').remove();
+            ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
+            ctx.fillRect (30, 30, 55, 50);
+
+            var gl = document.querySelector('#webglcanvas').getContext('webgl', {preserveDrawingBuffer: true});
+            if (gl) {
+                gl.clearColor(0.0, 1.0, 0.0, 1.0);
+                gl.clear(gl.COLOR_BUFFER_BIT);
+            }
         }
-      };
     </script>
-  </head>
-  <body>
-    <canvas id="testcanvas" style="width:700px;height:300px;"></canvas>
-  </body>
+</head>
+<body>
+<canvas id="testcanvas" style="width:700px;height:300px;"></canvas>
+<canvas id="webglcanvas" style="width:300px;height:300px;"></canvas>
+</body>
 </html>