summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_transform.xhtml
blob: 6a139d3580ef5f907b8e2bfa7b72d1611d88b49e (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=512636
-->
<head>
  <title>Test SVGTransform behavior</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="matrixUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=512636">Mozilla Bug 512636</a>
<p id="display"></p>
<div id="content">

  <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="1" id="svg">
    <g id="g" transform="translate(10, 20)"/>
  </svg>

</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

function run() {
  var g, t, m, m2;

  g = $("g");

  t = g.transform.baseVal.getItem(0);
  m = t.matrix;

  // test that the SVGTransform correctly reflects the translate()
  checkTransform(t, SVGTransform.SVG_TRANSFORM_TRANSLATE,
                 {a: 1, b: 0, c: 0, d: 1, e: 10, f: 20},
                 0, "translate");

  // set the SVGTransform to be a scale()
  t.setScale(2, 3);

  // test that the matrix is live and now reflects the scale()
  checkTransform(t, SVGTransform.SVG_TRANSFORM_SCALE,
                 {a: 2, b: 0, c: 0, d: 3, e: 0, f: 0},
                 0, "scale");

  // set the SVGTransform to be a matrix()
  m2 = createMatrix(1, 2, 3, 4, 5, 6);
  t.setMatrix(m2);

  // check that setMatrix() took a copy of m
  ok(m != m2, "t.matrix identity");

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_MATRIX,
                 {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6},
                 0, "matrix");

  m2 = {m11: 6, m12: 5, m21: 4, m22: 3, m41: 2, m42: 1};
  t.setMatrix(m2);
  checkTransform(t, SVGTransform.SVG_TRANSFORM_MATRIX,
                 {a: 6, b: 5, c: 4, d: 3, e: 2, f: 1},
                 0, "matrix");

  // set the SVGTransform to be a translate() then convert to a matrix
  t.setTranslate(0, 10);
  m.a = 2;

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_MATRIX,
                 {a: 2, b: 0, c: 0, d: 1, e: 0, f: 10},
                 0, "matrix");

  // If ty is not supplied it is assumed to be zero
  g.setAttribute("transform", "translate(5)");

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_TRANSLATE,
                 {a: 1, b: 0, c: 0, d: 1, e: 5, f: 0},
                 0, "transform");

  // set the SVGTransform to be a rotate()
  t.setRotate(90, 0, 0);

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_ROTATE,
                 {a: Math.cos(Math.PI / 2), b: Math.sin(Math.PI / 2),
                  c: -Math.sin(Math.PI / 2), d: Math.cos(Math.PI / 2),
                  e: 0, f: 0},
                 90, "rotate");

  // set the SVGTransform to be a skewX()
  t.setSkewX(45);

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_SKEWX,
                 {a: 1, b: 0,
                  c: Math.tan(Math.PI / 4), d: Math.tan(Math.PI / 4),
                  e: 0, f: 0},
                 45, "skewX");

  // set the SVGTransform to be a skewY()
  t.setSkewY(45);

  // test that the SVGTransform now reflects the matrix value
  checkTransform(t, SVGTransform.SVG_TRANSFORM_SKEWY,
                 {a: Math.tan(Math.PI / 4), b: Math.tan(Math.PI / 4),
                  c: 0, d: 1,
                  e: 0, f: 0},
                 45, "skewY");

  // check angle is reset after changing type
  t.setTranslate(10, 20);
  is(t.angle, 0, "Angle not reset after changing to translate type");

  // check read-only properties
  t.angle = 40;
  is(t.angle, 0, "t.angle should be read-only");
  t.type = 7;
  is(t.type, SVGTransform.SVG_TRANSFORM_TRANSLATE,
     "t.type should be read-only");
  t.matrix = m2;
  ok(t.matrix != m2 && t.matrix.b == 0, "t.matrix should be read-only");

  // check transform object identity after manipulation
  ok(t === g.transform.baseVal.getItem(0),
     "Got different transform objects after manipulation");
  ok(t.matrix === m,
     "Got different matrix objects after manipulation");

  testCreateTransform();
  testMatrixTransform();

  SimpleTest.finish();
}

function testMatrixTransform() {
  let svg = $("svg");
  const epsilon = 1 / 65536;

  let point = svg.createSVGPoint();
  point.x = 5;
  point.y = 4;
  let matrix = createMatrix(2, 0, 0, 2, 10, 10);
  let result = point.matrixTransform(matrix);
  let expected = DOMPoint.fromPoint(point).matrixTransform(matrix);
  isfuzzy(result.x, expected.x, epsilon, "matrix transformation x");
  isfuzzy(result.y, expected.y, epsilon, "matrix transformation y");

  svg.currentTranslate.x = 5;
  svg.currentTranslate.y = 4;
  result = svg.currentTranslate.matrixTransform(matrix);
  isfuzzy(result.x, expected.x, epsilon, "svg matrix transformation x");
  isfuzzy(result.y, expected.y, epsilon, "svg matrix transformation y");
  svg.currentTranslate.x = 0;
  svg.currentTranslate.y = 0;
}

function testCreateTransform() {
  let svg = $("svg");
  let t = svg.createSVGTransform();
  ok(t != svg.createSVGTransform(),
     "Got identical objects when creating new transform");
  checkTransform(t, SVGTransform.SVG_TRANSFORM_MATRIX,
                 createMatrix(1, 0, 0, 1, 0, 0), 0, "createSVGTransform");

  let m = createMatrix(1, 2, 3, 4, 5, 6);
  t = svg.createSVGTransformFromMatrix(m);
  ok(t.matrix != m,
     "createSVGTransformFromMatrix should copy matrix not adopt it");
  m.a = 7; // Just to be sure, changing m should not affect t
  checkTransform(t, SVGTransform.SVG_TRANSFORM_MATRIX,
                 {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6},
                 0, "createSVGTransformFromMatrix");
}

function checkTransform(transform, type, matrix, angle, forWhat) {
  roughCmpMatrix(transform.matrix, matrix, forWhat);
  is(transform.type, type, `transform.type for ${forWhat}`);
  is(transform.angle, angle, `transform.angle for ${forWhat}`);
}

window.addEventListener("load", run);

]]>
</script>
</pre>
</body>
</html>