1<html> 2<head> 3 <title>Canvas 3D</title> 4</head> 5<body> 6 <div style="position:relative; padding: 25px"> 7 <canvas id='canvas1' style="position:absolute;"></canvas> 8 </div> 9</body> 10<script type="text/javascript"> 11 var canvas = document.getElementById('canvas1'); 12 var ctx = canvas.getContext('webgl'); 13 14 // Some devices, e.g. krane, have a non-zero rotation in landscape mode. We 15 // precompensate this via the rotateZ CSS transform. See crbug.com/1046109. 16 const angle = screen.orientation.angle % 360; 17 canvas.style.transform = `rotateZ(${angle}deg)`; 18 19 // Make the canvas large but still falling inside the viewport; |height| 20 // also has to account for the Shelf (taskbar) at the bottom. 21 const integerWidth = Math.min(500, Math.floor(window.innerWidth * 0.9)); 22 const integerHeight = Math.min(300, Math.floor(window.innerHeight * 0.9)); 23 24 // We need subpixel accuracy for non-integer aspect ratios crbug.com/1042110. 25 const dpr = window.devicePixelRatio || 1; 26 canvas.style.border = `${1 / dpr}px solid black`; 27 if (angle % 180 == 90) { 28 canvas.width = integerHeight; 29 canvas.height = integerWidth; 30 canvas.style.width = `${integerHeight / dpr}px`; 31 canvas.style.height = `${integerWidth / dpr}px`; 32 33 // On krane, the canvas needs to be shifted "rightwards" when the screen is 34 // rotated 90 or 270 degrees, to bring it in view, see crbug.com/1046445/ 35 const offset = ((integerWidth / dpr) - (integerHeight / dpr)) / 2; 36 canvas.style.left = `${offset}px`; 37 //canvas.style.top = `-${offset}px`; 38 } else { 39 canvas.width = integerWidth; 40 canvas.height = integerHeight; 41 canvas.style.width = `${integerWidth / dpr}px`; 42 canvas.style.height = `${integerHeight / dpr}px`; 43 } 44 45 var draw_passes_count = 0; 46 function draw_pass() { 47 // Consider a seeded random number generator if there are reproducibility 48 // problems. 49 ctx.clearColor(0, Math.random(), 0, 1.0); 50 ctx.clear(ctx.COLOR_BUFFER_BIT); 51 draw_passes_count++; 52 } 53 setInterval(draw_pass, 1000); 54 55 function get_draw_passes_count() { 56 return draw_passes_count; 57 } 58 59</script> 60</html> 61