summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/canvas/element/manual/image-smoothing/imagesmoothing.html
blob: 1a86a8f2015aba3f68ebec7cd4227127e744f133 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<meta charset="utf-8">
<title>CanvasRenderingContext2D imageSmoothingEnabled test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#image-smoothing">
<script>
function createTestImage() {
  var image = document.createElement('canvas');
  var imgctx = image.getContext('2d');
  imgctx.fillStyle = "#F00";
  imgctx.fillRect(0, 0, 2, 2);
  imgctx.fillStyle = "#0F0";
  imgctx.fillRect(0, 0, 1, 1);
  return image;
}

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  assert_true(ctx.imageSmoothingEnabled);
}, "When the canvas context is created, imageSmoothingEnabled must be set to true.");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  ctx.imageSmoothingEnabled = false;
  assert_false(ctx.imageSmoothingEnabled);
}, "On getting imageSmoothingEnabled, the user agent must return the last value it was set to.");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  var image = createTestImage();
  ctx.scale(10, 10);
  ctx.drawImage(image, 0, 0);
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_not_equals(pixels[0], 0);
  assert_not_equals(pixels[1], 255);
}, "Test that image smoothing is actually on by default and just the attribute value.");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  ctx.imageSmoothingEnabled = true;
  var image = createTestImage();
  ctx.scale(10, 10);
  ctx.drawImage(image, 0, 0);
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_not_equals(pixels[0], 0);
  assert_not_equals(pixels[1], 255);
}, "Test that image smoothing works when imageSmoothingEnabled is set to true");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  var image = createTestImage();
  ctx.imageSmoothingEnabled = false;
  ctx.scale(10, 10);
  ctx.drawImage(image, 0, 0);
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with drawImage().");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  var image = createTestImage();
  ctx.imageSmoothingEnabled = false;
  ctx.scale(10, 10);
  ctx.fillStyle = ctx.createPattern(image, 'repeat');
  ctx.fillRect(0, 0, 10, 10);
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with fillRect and createPattern().");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  var image = createTestImage();
  ctx.imageSmoothingEnabled = false;
  ctx.fillStyle = ctx.createPattern(image, 'repeat');
  ctx.scale(10, 10);
  ctx.rect(0, 0, 10, 10);
  ctx.fill();
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with fill() and createPattern().");

test(function() {
  var ctx = document.createElement('canvas').getContext('2d');
  var image = createTestImage();
  ctx.strokeStyle = ctx.createPattern(image, 'repeat');
  ctx.lineWidth = 5;
  ctx.imageSmoothingEnabled = false;
  ctx.scale(10, 10);
  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(10, 10);
  ctx.stroke();
  var pixels = ctx.getImageData(9, 9, 1, 1).data;
  assert_array_equals(pixels, [0, 255, 0, 255]);
}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) works with stroke() and createPattern().");

test(function() {
  var repaints = 5;
  var ctx = document.createElement('canvas').getContext('2d');

  function draw() {
    ctx.clearRect(0, 0, 10, 10);
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    var image = createTestImage();
    ctx.imageSmoothingEnabled = false;
    ctx.scale(10, 10);
    ctx.drawImage(image, 0, 0);
    var pixels = ctx.getImageData(9, 9, 1, 1).data;
    assert_array_equals(pixels, [0, 255, 0, 255]);
  }

  while (repaints > 0) {
    draw();
    repaints = repaints - 1;
  }

}, "Test that imageSmoothingEnabled = false (nearest-neighbor interpolation) still works after repaints.");
</script>