| OLD | NEW |
| (Empty) |
| 1 description("Ensure correct behavior of canvas with fillPath using a gradient fi
llStyle and a shadow"); | |
| 2 | |
| 3 function print(message, color) | |
| 4 { | |
| 5 var paragraph = document.createElement("div"); | |
| 6 paragraph.appendChild(document.createTextNode(message)); | |
| 7 paragraph.style.fontFamily = "monospace"; | |
| 8 if (color) | |
| 9 paragraph.style.color = color; | |
| 10 document.getElementById("console").appendChild(paragraph); | |
| 11 } | |
| 12 | |
| 13 function shouldBeAround(a, b) | |
| 14 { | |
| 15 var evalA; | |
| 16 try { | |
| 17 evalA = eval(a); | |
| 18 } catch(e) { | |
| 19 evalA = e; | |
| 20 } | |
| 21 | |
| 22 if (Math.abs(evalA - b) < 15) | |
| 23 print("PASS " + a + " is around " + b , "green") | |
| 24 else | |
| 25 print("FAIL " + a + " is not around " + b + " (actual: " + evalA + ")",
"red"); | |
| 26 } | |
| 27 | |
| 28 var canvas = document.createElement('canvas'); | |
| 29 document.body.appendChild(canvas); | |
| 30 canvas.setAttribute('width', '600'); | |
| 31 canvas.setAttribute('height', '1100'); | |
| 32 var ctx = canvas.getContext('2d'); | |
| 33 | |
| 34 var gradient = ctx.createLinearGradient(0, 0, 300, 0); | |
| 35 gradient.addColorStop(0, 'rgba(0, 0, 255, 0.5)'); | |
| 36 gradient.addColorStop(1, 'rgba(0, 0, 255, 0.5)'); | |
| 37 | |
| 38 ctx.save(); | |
| 39 ctx.fillStyle = gradient; | |
| 40 ctx.shadowColor = 'rgba(255, 0, 0, 0.5)'; | |
| 41 ctx.shadowOffsetX = 250; | |
| 42 | |
| 43 function fillShape(x, y) { | |
| 44 ctx.beginPath(); | |
| 45 ctx.arc(x, y, 100, 0, Math.PI*2, true); | |
| 46 ctx.arc(x, y, 50, 0, Math.PI*2, false); | |
| 47 ctx.fill(); | |
| 48 } | |
| 49 | |
| 50 // Alpha shadow. | |
| 51 ctx.shadowBlur = 0; | |
| 52 fillShape(150, 150); | |
| 53 | |
| 54 // Blurry shadow. | |
| 55 ctx.shadowBlur = 10; | |
| 56 fillShape(150, 400); | |
| 57 | |
| 58 ctx.rotate(Math.PI/2); | |
| 59 | |
| 60 // Rotated alpha shadow. | |
| 61 ctx.shadowBlur = 0; | |
| 62 fillShape(650, -150); | |
| 63 | |
| 64 // Rotated blurry shadow. | |
| 65 ctx.shadowBlur = 10; | |
| 66 fillShape(900, -150); | |
| 67 | |
| 68 ctx.restore(); | |
| 69 | |
| 70 var imageData, data; | |
| 71 ctx.fillStyle = 'black'; | |
| 72 | |
| 73 function test(alphaTestFunction, x, y, r, g, b, a) { | |
| 74 // Get pixel. | |
| 75 imageData = ctx.getImageData(x, y, 1, 1); | |
| 76 data = imageData.data; | |
| 77 // Test pixel color components. | |
| 78 shouldBe('data[0]', r+''); | |
| 79 shouldBe('data[1]', g+''); | |
| 80 shouldBe('data[2]', b+''); | |
| 81 alphaTestFunction('data[3]', a+''); | |
| 82 // Plot test point. | |
| 83 ctx.fillRect(x, y, 3, 3); | |
| 84 } | |
| 85 | |
| 86 print('Verifying alpha shadow...'); | |
| 87 test(shouldBe, 400, 150, 0, 0, 0, 0); | |
| 88 test(shouldBeAround, 400, 75, 255, 0, 0, 64); | |
| 89 test(shouldBeAround, 400, 225, 255, 0, 0, 64); | |
| 90 test(shouldBeAround, 325, 150, 255, 0, 0, 64); | |
| 91 test(shouldBeAround, 475, 150, 255, 0, 0, 64); | |
| 92 | |
| 93 print(' '); | |
| 94 print('Verifying blurry shadow...'); | |
| 95 test(shouldBe, 400, 400, 0, 0, 0, 0); | |
| 96 test(shouldBeAround, 400, 300, 255, 0, 0, 31); | |
| 97 test(shouldBeAround, 400, 500, 255, 0, 0, 31); | |
| 98 test(shouldBeAround, 300, 400, 255, 0, 0, 31); | |
| 99 test(shouldBeAround, 500, 400, 255, 0, 0, 31); | |
| 100 | |
| 101 print(' '); | |
| 102 print('Verifying rotated alpha shadow...'); | |
| 103 test(shouldBe, 400, 650, 0, 0, 0, 0); | |
| 104 test(shouldBeAround, 400, 575, 255, 0, 0, 64); | |
| 105 test(shouldBeAround, 400, 725, 255, 0, 0, 64); | |
| 106 test(shouldBeAround, 325, 650, 255, 0, 0, 64); | |
| 107 test(shouldBeAround, 475, 650, 255, 0, 0, 64); | |
| 108 | |
| 109 print(' '); | |
| 110 print('Verifying rotated blurry shadow...'); | |
| 111 test(shouldBe, 400, 900, 0, 0, 0, 0); | |
| 112 test(shouldBeAround, 400, 800, 255, 0, 0, 31); | |
| 113 test(shouldBeAround, 400, 1000, 255, 0, 0, 31); | |
| 114 test(shouldBeAround, 300, 900, 255, 0, 0, 31); | |
| 115 test(shouldBeAround, 500, 900, 255, 0, 0, 31); | |
| 116 | |
| 117 print(' '); | |
| OLD | NEW |