Skip to content

Commit 7b653fd

Browse files
committed
skewX(), skewY()
1 parent 3e723c1 commit 7b653fd

File tree

2 files changed

+315
-2
lines changed

2 files changed

+315
-2
lines changed

src/main/java/org/htmlunit/javascript/host/dom/DOMMatrixReadOnly.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,6 @@ public DOMMatrixReadOnly rotateAxisAngle(
852852

853853
// Convert angle to radians
854854
final double angle2 = Math.toRadians(alpha) / 2;
855-
final double angle1 = alpha * (Math.PI / 360);
856855

857856
// Compute rotation matrix
858857
final double sc = Math.sin(angle2) * Math.cos(angle2);
@@ -892,6 +891,60 @@ public DOMMatrixReadOnly rotateAxisAngle(
892891
return multiplied;
893892
}
894893

894+
/**
895+
* @param alphaObj the angle, in degrees, by which to skew the matrix along the x-axis
896+
* @return returns a new DOMMatrix created by applying the specified skew transformation
897+
* to the source matrix along its x-axis. The original matrix is not modified.
898+
*/
899+
@JsxFunction
900+
public DOMMatrixReadOnly skewX(final Object alphaObj) {
901+
902+
// Default values
903+
double alpha = 0;
904+
if (alphaObj != null && !JavaScriptEngine.isUndefined(alphaObj)) {
905+
alpha = JavaScriptEngine.toNumber(alphaObj);
906+
}
907+
908+
// Convert angle to radians
909+
final double angle = Math.toRadians(alpha);
910+
911+
// Compute rotation matrix
912+
final DOMMatrixReadOnly rot = new DOMMatrixReadOnly();
913+
914+
rot.m21_ = Math.tan(angle);
915+
916+
// Multiply this * rot
917+
final DOMMatrixReadOnly multiplied = multiply(rot);
918+
return multiplied;
919+
}
920+
921+
/**
922+
* @param alphaObj the angle, in degrees, by which to skew the matrix along the y-axis
923+
* @return returns a new DOMMatrix created by applying the specified skew transformation
924+
* to the source matrix along its x-axis. The original matrix is not modified.
925+
*/
926+
@JsxFunction
927+
public DOMMatrixReadOnly skewY(final Object alphaObj) {
928+
929+
// Default values
930+
double alpha = 0;
931+
if (alphaObj != null && !JavaScriptEngine.isUndefined(alphaObj)) {
932+
alpha = JavaScriptEngine.toNumber(alphaObj);
933+
}
934+
935+
// Convert angle to radians
936+
final double angle = Math.toRadians(alpha);
937+
938+
// Compute rotation matrix
939+
final DOMMatrixReadOnly rot = new DOMMatrixReadOnly();
940+
941+
rot.m12_ = Math.tan(angle);
942+
943+
// Multiply this * rot
944+
final DOMMatrixReadOnly multiplied = multiply(rot);
945+
return multiplied;
946+
}
947+
895948
/**
896949
* @return a new Float32Array containing all 16 elements
897950
* (m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44)

src/test/java/org/htmlunit/javascript/host/dom/DOMMatrixReadOnlyTest.java

Lines changed: 261 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2267,7 +2267,7 @@ public void toFloat32Array_customValues6() throws Exception {
22672267
+ LOG_TITLE_FUNCTION
22682268
+ "let m = new DOMMatrixReadOnly([1, 2, 3, 4, 5, 6]);\n"
22692269
+ "let array = m.toFloat32Array();\n"
2270-
+ "log(array.join());\n"
2270+
+ "log(array);\n"
22712271
+ "</script>\n"
22722272
+ "</body></html>";
22732273

@@ -2433,4 +2433,264 @@ public void toFloat64Array_withInfinity() throws Exception {
24332433

24342434
loadPageVerifyTitle2(html);
24352435
}
2436+
2437+
/**
2438+
* @throws Exception on test failure
2439+
*/
2440+
@Test
2441+
@Alerts({"[1, 0, 0.577, 1, 0, 0]",
2442+
"1[1, 0, 0, 0]",
2443+
"2[0.577, 1, 0, 0]",
2444+
"3[0, 0, 1, 0]",
2445+
"4[0, 0, 0, 1]",
2446+
"true"})
2447+
public void skewX_30degrees() throws Exception {
2448+
final String html = DOCTYPE_HTML
2449+
+ "<html>\n"
2450+
+ "<body>\n"
2451+
+ "<script>\n"
2452+
+ LOG_TITLE_FUNCTION
2453+
+ DUMP_FUNCTION
2454+
+ "let m = new DOMMatrixReadOnly();\n"
2455+
+ "let skewed = m.skewX(30);\n"
2456+
+ "dump(skewed);\n"
2457+
+ "</script>\n"
2458+
+ "</body></html>";
2459+
2460+
loadPageVerifyTitle2(html);
2461+
}
2462+
2463+
/**
2464+
* @throws Exception on test failure
2465+
*/
2466+
@Test
2467+
@Alerts({"[1, 0, 0, 1, 0, 0]",
2468+
"1[1, 0, 0, 0]",
2469+
"2[0, 1, 0, 0]",
2470+
"3[0, 0, 1, 0]",
2471+
"4[0, 0, 0, 1]",
2472+
"true"})
2473+
public void skewX_0degrees() throws Exception {
2474+
final String html = DOCTYPE_HTML
2475+
+ "<html>\n"
2476+
+ "<body>\n"
2477+
+ "<script>\n"
2478+
+ LOG_TITLE_FUNCTION
2479+
+ DUMP_FUNCTION
2480+
+ "let m = new DOMMatrixReadOnly();\n"
2481+
+ "let skewed = m.skewX(0);\n"
2482+
+ "dump(skewed);\n"
2483+
+ "</script>\n"
2484+
+ "</body></html>";
2485+
2486+
loadPageVerifyTitle2(html);
2487+
}
2488+
2489+
/**
2490+
* @throws Exception on test failure
2491+
*/
2492+
@Test
2493+
@Alerts({"[1, 0, 0, 1, 0, 0]",
2494+
"1[1, 0, 0, 0]",
2495+
"2[0, 1, 0, 0]",
2496+
"3[0, 0, 1, 0]",
2497+
"4[0, 0, 0, 1]",
2498+
"true"})
2499+
public void skewX_noParam() throws Exception {
2500+
final String html = DOCTYPE_HTML
2501+
+ "<html>\n"
2502+
+ "<body>\n"
2503+
+ "<script>\n"
2504+
+ LOG_TITLE_FUNCTION
2505+
+ DUMP_FUNCTION
2506+
+ "let m = new DOMMatrixReadOnly();\n"
2507+
+ "let skewed = m.skewX();\n"
2508+
+ "dump(skewed);\n"
2509+
+ "</script>\n"
2510+
+ "</body></html>";
2511+
2512+
loadPageVerifyTitle2(html);
2513+
}
2514+
2515+
/**
2516+
* @throws Exception on test failure
2517+
*/
2518+
@Test
2519+
@Alerts({"[1, 0.577, 0, 1, 0, 0]",
2520+
"1[1, 0.577, 0, 0]",
2521+
"2[0, 1, 0, 0]",
2522+
"3[0, 0, 1, 0]",
2523+
"4[0, 0, 0, 1]",
2524+
"true"})
2525+
public void skewY_30degrees() throws Exception {
2526+
final String html = DOCTYPE_HTML
2527+
+ "<html>\n"
2528+
+ "<body>\n"
2529+
+ "<script>\n"
2530+
+ LOG_TITLE_FUNCTION
2531+
+ DUMP_FUNCTION
2532+
+ "let m = new DOMMatrixReadOnly();\n"
2533+
+ "let skewed = m.skewY(30);\n"
2534+
+ "dump(skewed);\n"
2535+
+ "</script>\n"
2536+
+ "</body></html>";
2537+
2538+
loadPageVerifyTitle2(html);
2539+
}
2540+
2541+
/**
2542+
* @throws Exception on test failure
2543+
*/
2544+
@Test
2545+
@Alerts({"[1, 0, 0, 1, 0, 0]",
2546+
"1[1, 0, 0, 0]",
2547+
"2[0, 1, 0, 0]",
2548+
"3[0, 0, 1, 0]",
2549+
"4[0, 0, 0, 1]",
2550+
"true"})
2551+
public void skewY_0degrees() throws Exception {
2552+
final String html = DOCTYPE_HTML
2553+
+ "<html>\n"
2554+
+ "<body>\n"
2555+
+ "<script>\n"
2556+
+ LOG_TITLE_FUNCTION
2557+
+ DUMP_FUNCTION
2558+
+ "let m = new DOMMatrixReadOnly();\n"
2559+
+ "let skewed = m.skewY(0);\n"
2560+
+ "dump(skewed);\n"
2561+
+ "</script>\n"
2562+
+ "</body></html>";
2563+
2564+
loadPageVerifyTitle2(html);
2565+
}
2566+
2567+
/**
2568+
* @throws Exception on test failure
2569+
*/
2570+
@Test
2571+
@Alerts({"[1, 0, 0, 1, 0, 0]",
2572+
"1[1, 0, 0, 0]",
2573+
"2[0, 1, 0, 0]",
2574+
"3[0, 0, 1, 0]",
2575+
"4[0, 0, 0, 1]",
2576+
"true"})
2577+
public void skewY_noParam() throws Exception {
2578+
final String html = DOCTYPE_HTML
2579+
+ "<html>\n"
2580+
+ "<body>\n"
2581+
+ "<script>\n"
2582+
+ LOG_TITLE_FUNCTION
2583+
+ DUMP_FUNCTION
2584+
+ "let m = new DOMMatrixReadOnly();\n"
2585+
+ "let skewed = m.skewY();\n"
2586+
+ "dump(skewed);\n"
2587+
+ "</script>\n"
2588+
+ "</body></html>";
2589+
2590+
loadPageVerifyTitle2(html);
2591+
}
2592+
2593+
/**
2594+
* @throws Exception on test failure
2595+
*/
2596+
@Test
2597+
@Alerts({"[1, 0, 0.9, 1, 5, 6]",
2598+
"1[1, 0, 0, 0]",
2599+
"2[0.9, 1, 0, 0]",
2600+
"3[0, 0, 1, 0]",
2601+
"4[5, 6, 0, 1]",
2602+
"true"})
2603+
public void skewX_6elements() throws Exception {
2604+
final String html = DOCTYPE_HTML
2605+
+ "<html>\n"
2606+
+ "<body>\n"
2607+
+ "<script>\n"
2608+
+ LOG_TITLE_FUNCTION
2609+
+ DUMP_FUNCTION
2610+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6]);\n"
2611+
+ "let skewed = m.skewX(42);\n"
2612+
+ "dump(skewed);\n"
2613+
+ "</script>\n"
2614+
+ "</body></html>";
2615+
2616+
loadPageVerifyTitle2(html);
2617+
}
2618+
2619+
/**
2620+
* @throws Exception on test failure
2621+
*/
2622+
@Test
2623+
@Alerts({"[1, 0.9, 0, 1, 5, 6]",
2624+
"1[1, 0.9, 0, 0]",
2625+
"2[0, 1, 0, 0]",
2626+
"3[0, 0, 1, 0]",
2627+
"4[5, 6, 0, 1]",
2628+
"true"})
2629+
public void skewY_6elements() throws Exception {
2630+
final String html = DOCTYPE_HTML
2631+
+ "<html>\n"
2632+
+ "<body>\n"
2633+
+ "<script>\n"
2634+
+ LOG_TITLE_FUNCTION
2635+
+ DUMP_FUNCTION
2636+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6]);\n"
2637+
+ "let skewed = m.skewY(42);\n"
2638+
+ "dump(skewed);\n"
2639+
+ "</script>\n"
2640+
+ "</body></html>";
2641+
2642+
loadPageVerifyTitle2(html);
2643+
}
2644+
2645+
/**
2646+
* @throws Exception on test failure
2647+
*/
2648+
@Test
2649+
@Alerts({"[1, 0, 5.9, 6, 13, 14]",
2650+
"1[1, 0, 0, 1]",
2651+
"2[5.9, 6, 7, 8.9]",
2652+
"3[9, 10, 11, 12]",
2653+
"4[13, 14, 15, 16]",
2654+
"false"})
2655+
public void skewX_16elements() throws Exception {
2656+
final String html = DOCTYPE_HTML
2657+
+ "<html>\n"
2658+
+ "<body>\n"
2659+
+ "<script>\n"
2660+
+ LOG_TITLE_FUNCTION
2661+
+ DUMP_FUNCTION
2662+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);\n"
2663+
+ "let skewed = m.skewX(42);\n"
2664+
+ "dump(skewed);\n"
2665+
+ "</script>\n"
2666+
+ "</body></html>";
2667+
2668+
loadPageVerifyTitle2(html);
2669+
}
2670+
2671+
/**
2672+
* @throws Exception on test failure
2673+
*/
2674+
@Test
2675+
@Alerts({"[5.502, 5.402, 5, 6, 13, 14]",
2676+
"1[5.502, 5.402, 6.303, 8.203]",
2677+
"2[5, 6, 7, 8]",
2678+
"3[9, 10, 11, 12]",
2679+
"4[13, 14, 15, 16]",
2680+
"false"})
2681+
public void skewY_16elements() throws Exception {
2682+
final String html = DOCTYPE_HTML
2683+
+ "<html>\n"
2684+
+ "<body>\n"
2685+
+ "<script>\n"
2686+
+ LOG_TITLE_FUNCTION
2687+
+ DUMP_FUNCTION
2688+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);\n"
2689+
+ "let skewed = m.skewY(42);\n"
2690+
+ "dump(skewed);\n"
2691+
+ "</script>\n"
2692+
+ "</body></html>";
2693+
2694+
loadPageVerifyTitle2(html);
2695+
}
24362696
}

0 commit comments

Comments
 (0)