OLD | NEW |
(Empty) | |
| 1 <canvas id="canvas" width="270" height="420"></canvas> |
| 2 <canvas id="pattern" width="20" height="20"></canvas> |
| 3 <script type="text/javascript"> |
| 4 if (window.testRunner) |
| 5 testRunner.dumpAsTextWithPixelResults(); |
| 6 |
| 7 var canvas = document.getElementById("canvas"); |
| 8 var ctx = canvas.getContext("2d"); |
| 9 ctx.strokeRect(0, 0, canvas.width, canvas.height); |
| 10 |
| 11 var pCanvas = document.getElementById("pattern"); |
| 12 var pctx = pCanvas.getContext("2d"); |
| 13 pctx.fillStyle = "red"; |
| 14 pctx.fillRect(0, 0, 20, 20); |
| 15 pctx.fillStyle = "green"; |
| 16 pctx.fillRect(1, 1, 18, 18); |
| 17 var pattern = pctx.createPattern(pCanvas, 'no-repeat'); |
| 18 ctx.fillStyle = pattern; |
| 19 ctx.strokeStyle = "blue"; |
| 20 |
| 21 function test(testPosX, testPosY, transformCallback) |
| 22 { |
| 23 ctx.save(); |
| 24 ctx.beginPath(); |
| 25 ctx.translate(testPosX, testPosY); |
| 26 ctx.rect(0, 0, 100, 100); |
| 27 // After transformCallback, no-repeat pattern should follow the new |
| 28 // position; but rect will stay at wherever it was. |
| 29 transformCallback(); |
| 30 ctx.fill(); // See the pattern |
| 31 ctx.stroke(); // See the rect |
| 32 ctx.restore(); |
| 33 } |
| 34 |
| 35 function rotateCallback() { ctx.rotate(Math.PI / 180 * 25); } |
| 36 function translateCallback() { ctx.translate(50, 50); } |
| 37 function scaleCallback() { ctx.scale(2, 2); } |
| 38 function transformCallback() { ctx.transform(1, 1, 0, 1, 0, 0); } |
| 39 function resetTransformCallback() { ctx.resetTransform() } |
| 40 function multipleTransformCallback() |
| 41 { |
| 42 ctx.translate(20, 20); |
| 43 ctx.rotate(Math.PI / 180 * 10); |
| 44 } |
| 45 |
| 46 // Rotate the canvas by 90 degrees |
| 47 // This is to test whether after save(), restore(), the canvas can return to |
| 48 // this 90-degree-rotated state instead of its very initial state |
| 49 ctx.translate(canvas.width/2, canvas.height/2); |
| 50 ctx.rotate(Math.PI/2); |
| 51 ctx.translate(-canvas.height/2, -canvas.width/2); |
| 52 |
| 53 // Since the canvas is rotated 90 degrees, these three rectangles will appear |
| 54 // on right side, from top to bottom. |
| 55 test(10, 10, scaleCallback); |
| 56 test(160, 10, rotateCallback); |
| 57 test(310, 10, translateCallback); |
| 58 |
| 59 // These three rectangles will appear on left side, from top to bottom. |
| 60 test(10, 160, resetTransformCallback); |
| 61 test(160, 160, transformCallback); |
| 62 test(310, 160, multipleTransformCallback); |
| 63 |
| 64 </script> |
OLD | NEW |