OLD | NEW |
1 <!doctype html> | 1 <script src="../../resources/testharness.js"></script> |
2 <html> | 2 <script src="../../resources/testharnessreport.js"></script> |
3 <head> | |
4 <script src="../../resources/js-test.js"></script> | |
5 </head> | |
6 <body> | 3 <body> |
7 <script src="script-tests/canvas-isPointInStroke.js"></script> | 4 <script> |
| 5 var ctx = document.createElement('canvas').getContext('2d'); |
| 6 document.body.appendChild(ctx.canvas); |
| 7 ctx.strokeStyle = '#0ff'; |
| 8 |
| 9 // Create new path. |
| 10 ctx.beginPath(); |
| 11 ctx.rect(20,20,100,100); |
| 12 |
| 13 function testPointInStroke(x, y, isPointInStroke) { |
| 14 if(isPointInStroke) |
| 15 assert_true(ctx.isPointInStroke(x, y)); |
| 16 else |
| 17 assert_false(ctx.isPointInStroke(x, y)); |
| 18 } |
| 19 |
| 20 testScenarios1 = |
| 21 [ |
| 22 ['TestScenario 1, Case 1', 20, 20, true], |
| 23 ['TestScenario 1, Case 2', 120, 20, true], |
| 24 ['TestScenario 1, Case 3', 20, 120, true], |
| 25 ['TestScenario 1, Case 4', 120, 120, true], |
| 26 ['TestScenario 1, Case 5', 70, 20, true], |
| 27 ['TestScenario 1, Case 6', 20, 70, true], |
| 28 ['TestScenario 1, Case 7', 120, 70, true], |
| 29 ['TestScenario 1, Case 8', 70, 120, true], |
| 30 |
| 31 ['TestScenario 1, Case 9', 22, 22, false], |
| 32 ['TestScenario 1, Case 10', 118, 22, false], |
| 33 ['TestScenario 1, Case 11', 22, 118, false], |
| 34 ['TestScenario 1, Case 12', 118, 118, false], |
| 35 ['TestScenario 1, Case 13', 70, 18, false], |
| 36 ['TestScenario 1, Case 14', 122, 70, false], |
| 37 ['TestScenario 1, Case 15', 70, 122, false], |
| 38 ['TestScenario 1, Case 16', 18, 70, false], |
| 39 ]; |
| 40 |
| 41 testScenarios2 = |
| 42 [ |
| 43 ['TestScenario 2, Case 1', 22, 22, true], |
| 44 ['TestScenario 2, Case 2', 118, 22, true], |
| 45 ['TestScenario 2, Case 3', 22, 118, true], |
| 46 ['TestScenario 2, Case 4', 118, 118, true], |
| 47 ['TestScenario 2, Case 5', 70, 18, true], |
| 48 ['TestScenario 2, Case 6', 122, 70, true], |
| 49 ['TestScenario 2, Case 7', 70, 122, true], |
| 50 ['TestScenario 2, Case 8', 18, 70, true], |
| 51 |
| 52 ['TestScenario 2, Case 9', 26, 70, false], |
| 53 ['TestScenario 2, Case 10', 70, 26, false], |
| 54 ['TestScenario 2, Case 11', 70, 114, false], |
| 55 ['TestScenario 2, Case 12', 114, 70, false], |
| 56 ]; |
| 57 |
| 58 generate_tests(testPointInStroke, testScenarios1); |
| 59 ctx.lineWidth = 10; |
| 60 generate_tests(testPointInStroke, testScenarios2); |
| 61 |
| 62 test(function(t) { |
| 63 ctx.beginPath(); |
| 64 ctx.moveTo(10,10); |
| 65 ctx.lineTo(110,20); |
| 66 ctx.lineTo(10,30); |
| 67 ctx.lineJoin = "bevel"; |
| 68 assert_false(ctx.isPointInStroke(113,20)); |
| 69 |
| 70 ctx.miterLimit = 40.0; |
| 71 ctx.lineJoin = "miter"; |
| 72 assert_true(ctx.isPointInStroke(113,20)); |
| 73 |
| 74 ctx.miterLimit = 2.0; |
| 75 assert_false(ctx.isPointInStroke(113,20)); |
| 76 |
| 77 ctx.beginPath(); |
| 78 ctx.moveTo(10,10); |
| 79 ctx.lineTo(110,10); |
| 80 ctx.lineCap = "butt"; |
| 81 assert_false(ctx.isPointInStroke(112,10)); |
| 82 |
| 83 ctx.lineCap = "round"; |
| 84 assert_true(ctx.isPointInStroke(112,10)); |
| 85 assert_false(ctx.isPointInStroke(117,10)); |
| 86 |
| 87 ctx.lineCap = "square"; |
| 88 assert_true(ctx.isPointInStroke(112,10)); |
| 89 assert_false(ctx.isPointInStroke(117,10)); |
| 90 |
| 91 ctx.lineCap = "butt"; |
| 92 ctx.setLineDash([10,10]); |
| 93 assert_true(ctx.isPointInStroke(15,10)); |
| 94 assert_false(ctx.isPointInStroke(25,10)); |
| 95 assert_true(ctx.isPointInStroke(35,10)); |
| 96 |
| 97 ctx.lineDashOffset = 10; |
| 98 assert_false(ctx.isPointInStroke(15,10)); |
| 99 assert_true(ctx.isPointInStroke(25,10)); |
| 100 assert_false(ctx.isPointInStroke(35,10)); |
| 101 |
| 102 ctx.save(); |
| 103 ctx.scale(Number.MAX_VALUE, Number.MAX_VALUE); |
| 104 ctx.beginPath(); |
| 105 ctx.moveTo(-10, -10); |
| 106 ctx.lineTo(10, 10); |
| 107 assert_true(ctx.isPointInStroke(0, 0)); |
| 108 ctx.restore(); |
| 109 |
| 110 ctx.save(); |
| 111 ctx.scale(0, 0); |
| 112 ctx.beginPath(); |
| 113 ctx.moveTo(-10, -10); |
| 114 ctx.lineTo(10, 10); |
| 115 assert_false(ctx.isPointInStroke(0, 0)); |
| 116 ctx.restore(); |
| 117 }, "Test the behavior of isPointInStroke in Canvas"); |
| 118 |
| 119 </script> |
8 </body> | 120 </body> |
OLD | NEW |