offscreencanvas.getcontext.html (3300B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="/html/canvas/resources/canvas-tests.js"></script> 5 <link rel="help" href="https://html.spec.whatwg.org/#dom-offscreencanvas-getcontext"> 6 <script> 7 8 test(function() { 9 var offscreenCanvas = new OffscreenCanvas(1, 1); 10 assert_throws_js(TypeError, function() { offscreenCanvas.getContext('3d'); }); 11 }, "Test that getContext with un-supported string throws a TypeError."); 12 13 test(function() { 14 var offscreenCanvas1 = new OffscreenCanvas(1, 1); 15 var ctx1 = offscreenCanvas1.getContext('2d'); 16 assert_true(ctx1 instanceof OffscreenCanvasRenderingContext2D); 17 18 var offscreenCanvas2 = new OffscreenCanvas(1, 1); 19 var ctx2 = offscreenCanvas2.getContext('webgl'); 20 assert_true(ctx2 instanceof WebGLRenderingContext); 21 22 var offscreenCanvas3 = new OffscreenCanvas(1, 1); 23 var ctx3 = offscreenCanvas3.getContext('webgl2'); 24 assert_true(ctx3 instanceof WebGL2RenderingContext); 25 }, "Test that getContext with supported string returns correct results"); 26 27 test(function() { 28 var offscreenCanvas1 = new OffscreenCanvas(1, 1); 29 var ctx1 = offscreenCanvas1.getContext('2d'); 30 var ctx2 = offscreenCanvas1.getContext('webgl'); 31 assert_equals(ctx2, null); 32 33 var offscreenCanvas2 = new OffscreenCanvas(1, 1); 34 var ctx3 = offscreenCanvas2.getContext('webgl'); 35 var ctx4 = offscreenCanvas2.getContext('2d'); 36 assert_equals(ctx4, null); 37 }, "Test that getContext twice with different context type returns null the second time"); 38 39 test(function() { 40 var offscreenCanvas = new OffscreenCanvas(1, 2); 41 var ctx = offscreenCanvas.getContext('2d'); 42 var dstCanvas = ctx.canvas; 43 assert_equals(dstCanvas.width, 1); 44 assert_equals(dstCanvas.height, 2); 45 }, "Test that 2dcontext.canvas should return the original OffscreenCanvas"); 46 47 test(function() { 48 var offscreenCanvas = new OffscreenCanvas(1, 2); 49 var ctx = offscreenCanvas.getContext('webgl'); 50 var dstCanvas = ctx.canvas; 51 assert_equals(dstCanvas.width, 1); 52 assert_equals(dstCanvas.height, 2); 53 }, "Test that webglcontext.canvas should return the original OffscreenCanvas"); 54 55 test(function() { 56 var offscreenCanvas = new OffscreenCanvas(10, 10); 57 var ctx = offscreenCanvas.getContext('2d', {alpha: false}); 58 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 59 ctx.fillRect(0, 0, 10, 10); 60 _assertPixelApprox(offscreenCanvas, 5,5, 0,127,0,255, 2); 61 }, "Test that OffscreenCanvasRenderingContext2D with alpha disabled makes the OffscreenCanvas opaque"); 62 63 test(function() { 64 var offscreenCanvas = new OffscreenCanvas(10, 10); 65 var ctx = offscreenCanvas.getContext('2d', {alpha: true}); 66 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 67 ctx.fillRect(0, 0, 10, 10); 68 _assertPixelApprox(offscreenCanvas, 5,5, 0,255,0,127, 2); 69 }, "Test that OffscreenCanvasRenderingContext2D with alpha enabled preserves the alpha"); 70 71 test(function() { 72 var offscreenCanvas = new OffscreenCanvas(10, 10); 73 var ctx = offscreenCanvas.getContext('2d'); 74 ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; 75 ctx.fillRect(0, 0, 10, 10); 76 _assertPixelApprox(offscreenCanvas, 5,5, 0,255,0,127, 2); 77 }, "Test that 'alpha' context creation attribute is true by default"); 78 79 </script>