Skip to content

Commit bc2f64c

Browse files
committed
translate()
1 parent 7b653fd commit bc2f64c

File tree

2 files changed

+338
-0
lines changed

2 files changed

+338
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,48 @@ public DOMMatrixReadOnly skewY(final Object alphaObj) {
945945
return multiplied;
946946
}
947947

948+
/**
949+
* Creates a new matrix being the result of the original matrix with a translation applied.
950+
*
951+
* @param xObj a number representing the abscissa (x-coordinate) of the translating vector
952+
* @param yObj a number representing the ordinate (y-coordinate) of the translating vector
953+
* @param zObj A number representing the z component of the translating vector. If not supplied,
954+
* this defaults to 0. If this is anything other than 0, the resulting matrix will be 3D
955+
* @return a DOMMatrix containing a new matrix being the result of the matrix being translated
956+
* by the given vector. The original matrix is not modified.
957+
* If a translation is applied about the z-axis, the resulting matrix will be a 4x4 3D matrix.
958+
*/
959+
@JsxFunction
960+
public DOMMatrixReadOnly translate(
961+
final Object xObj, final Object yObj, final Object zObj) {
962+
// Default values
963+
double x = 0;
964+
double y = 0;
965+
double z = 0;
966+
if (xObj != null && !JavaScriptEngine.isUndefined(xObj)) {
967+
x = JavaScriptEngine.toNumber(xObj);
968+
}
969+
if (yObj != null && !JavaScriptEngine.isUndefined(yObj)) {
970+
y = JavaScriptEngine.toNumber(yObj);
971+
}
972+
if (zObj != null && !JavaScriptEngine.isUndefined(zObj)) {
973+
z = JavaScriptEngine.toNumber(zObj);
974+
}
975+
976+
final DOMMatrixReadOnly translate = new DOMMatrixReadOnly();
977+
978+
translate.m41_ = x;
979+
translate.m42_ = y;
980+
translate.m43_ = z;
981+
982+
translate.is2D_ = false;
983+
984+
// Multiply this * rot
985+
final DOMMatrixReadOnly multiplied = multiply(translate);
986+
multiplied.is2D_ = is2D_ && (zObj == null || JavaScriptEngine.isUndefined(zObj) || z == 0);
987+
return multiplied;
988+
}
989+
948990
/**
949991
* @return a new Float32Array containing all 16 elements
950992
* (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: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,4 +2693,300 @@ public void skewY_16elements() throws Exception {
26932693

26942694
loadPageVerifyTitle2(html);
26952695
}
2696+
2697+
/**
2698+
* @throws Exception on test failure
2699+
*/
2700+
@Test
2701+
@Alerts({"[1, 0, 5, 6, 123, 134]",
2702+
"1[1, 0, 0, 1]",
2703+
"2[5, 6, 7, 8]",
2704+
"3[9, 10, 11, 12]",
2705+
"4[123, 134, 155, 186]",
2706+
"false"})
2707+
public void translate_16elements_positiveValues() throws Exception {
2708+
final String html = DOCTYPE_HTML
2709+
+ "<html>\n"
2710+
+ "<body>\n"
2711+
+ "<script>\n"
2712+
+ LOG_TITLE_FUNCTION
2713+
+ DUMP_FUNCTION
2714+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);\n"
2715+
+ "let translated = m.translate(10, 20);\n"
2716+
+ "dump(translated);\n"
2717+
+ "</script>\n"
2718+
+ "</body></html>";
2719+
2720+
loadPageVerifyTitle2(html);
2721+
}
2722+
2723+
/**
2724+
* @throws Exception on test failure
2725+
*/
2726+
@Test
2727+
@Alerts({"[1, 0, 0, 1, 15, 26]",
2728+
"1[1, 0, 0, 0]",
2729+
"2[0, 1, 0, 0]",
2730+
"3[0, 0, 1, 0]",
2731+
"4[15, 26, 0, 1]",
2732+
"true"})
2733+
public void translate_6elements_positiveValues() throws Exception {
2734+
final String html = DOCTYPE_HTML
2735+
+ "<html>\n"
2736+
+ "<body>\n"
2737+
+ "<script>\n"
2738+
+ LOG_TITLE_FUNCTION
2739+
+ DUMP_FUNCTION
2740+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6]);\n"
2741+
+ "let translated = m.translate(10, 20);\n"
2742+
+ "dump(translated);\n"
2743+
+ "</script>\n"
2744+
+ "</body></html>";
2745+
2746+
loadPageVerifyTitle2(html);
2747+
}
2748+
2749+
/**
2750+
* @throws Exception on test failure
2751+
*/
2752+
@Test
2753+
@Alerts({"[1, 0, 5, 6, -97, -106]",
2754+
"1[1, 0, 0, 1]",
2755+
"2[5, 6, 7, 8]",
2756+
"3[9, 10, 11, 12]",
2757+
"4[-97, -106, -125, -154]",
2758+
"false"})
2759+
public void translate_16elements_negativeValues() throws Exception {
2760+
final String html = DOCTYPE_HTML
2761+
+ "<html>\n"
2762+
+ "<body>\n"
2763+
+ "<script>\n"
2764+
+ LOG_TITLE_FUNCTION
2765+
+ DUMP_FUNCTION
2766+
+ "let m = new DOMMatrixReadOnly([1, 0, 0, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);\n"
2767+
+ "let translated = m.translate(-10, -20);\n"
2768+
+ "dump(translated);\n"
2769+
+ "</script>\n"
2770+
+ "</body></html>";
2771+
2772+
loadPageVerifyTitle2(html);
2773+
}
2774+
2775+
/**
2776+
* @throws Exception on test failure
2777+
*/
2778+
@Test
2779+
@Alerts({"[10, 11, 1, -5, -120, -3]",
2780+
"1[10, 11, 0, 0]",
2781+
"2[1, -5, 0, 0]",
2782+
"3[0, 0, 1, 0]",
2783+
"4[-120, -3, 0, 1]",
2784+
"true"})
2785+
public void translate_6elements_negativeValues() throws Exception {
2786+
final String html = DOCTYPE_HTML
2787+
+ "<html>\n"
2788+
+ "<body>\n"
2789+
+ "<script>\n"
2790+
+ LOG_TITLE_FUNCTION
2791+
+ DUMP_FUNCTION
2792+
+ "let m = new DOMMatrixReadOnly([10, 11, 1, -5, 0, 7]);\n"
2793+
+ "let translated = m.translate(-10, -20);\n"
2794+
+ "dump(translated);\n"
2795+
+ "</script>\n"
2796+
+ "</body></html>";
2797+
2798+
loadPageVerifyTitle2(html);
2799+
}
2800+
2801+
/**
2802+
* @throws Exception on test failure
2803+
*/
2804+
@Test
2805+
@Alerts({"[1, 0, 0, 1, 0, 0]",
2806+
"1[1, 0, 0, 0]",
2807+
"2[0, 1, 0, 0]",
2808+
"3[0, 0, 1, 0]",
2809+
"4[0, 0, 0, 1]",
2810+
"true"})
2811+
public void translate_zeroValues() throws Exception {
2812+
final String html = DOCTYPE_HTML
2813+
+ "<html>\n"
2814+
+ "<body>\n"
2815+
+ "<script>\n"
2816+
+ LOG_TITLE_FUNCTION
2817+
+ DUMP_FUNCTION
2818+
+ "let m = new DOMMatrixReadOnly();\n"
2819+
+ "let translated = m.translate(0, 0);\n"
2820+
+ "dump(translated);\n"
2821+
+ "</script>\n"
2822+
+ "</body></html>";
2823+
2824+
loadPageVerifyTitle2(html);
2825+
}
2826+
2827+
/**
2828+
* @throws Exception on test failure
2829+
*/
2830+
@Test
2831+
@Alerts({"[1, 0, 0, 1, NaN, NaN]",
2832+
"1[1, 0, 0, 0]",
2833+
"2[0, 1, 0, 0]",
2834+
"3[0, 0, 1, 0]",
2835+
"4[NaN, NaN, 0, 1]",
2836+
"true"})
2837+
@HtmlUnitNYI(
2838+
CHROME = {"[1, 0, 0, 1, NaN, NaN]",
2839+
"1[1, 0, 0, 0]",
2840+
"2[0, 1, 0, 0]",
2841+
"3[0, 0, 1, 0]",
2842+
"4[NaN, NaN, NaN, NaN]",
2843+
"true"},
2844+
EDGE = {"[1, 0, 0, 1, NaN, NaN]",
2845+
"1[1, 0, 0, 0]",
2846+
"2[0, 1, 0, 0]",
2847+
"3[0, 0, 1, 0]",
2848+
"4[NaN, NaN, NaN, NaN]",
2849+
"true"},
2850+
FF = {"[1, 0, 0, 1, NaN, NaN]",
2851+
"1[1, 0, 0, 0]",
2852+
"2[0, 1, 0, 0]",
2853+
"3[0, 0, 1, 0]",
2854+
"4[NaN, NaN, NaN, NaN]",
2855+
"true"},
2856+
FF_ESR = {"[1, 0, 0, 1, NaN, NaN]",
2857+
"1[1, 0, 0, 0]",
2858+
"2[0, 1, 0, 0]",
2859+
"3[0, 0, 1, 0]",
2860+
"4[NaN, NaN, NaN, NaN]",
2861+
"true"})
2862+
public void translate_withNaN() throws Exception {
2863+
final String html = DOCTYPE_HTML
2864+
+ "<html>\n"
2865+
+ "<body>\n"
2866+
+ "<script>\n"
2867+
+ LOG_TITLE_FUNCTION
2868+
+ DUMP_FUNCTION
2869+
+ "let m = new DOMMatrixReadOnly();\n"
2870+
+ "let translated = m.translate(NaN, NaN);\n"
2871+
+ "dump(translated);\n"
2872+
+ "</script>\n"
2873+
+ "</body></html>";
2874+
2875+
loadPageVerifyTitle2(html);
2876+
}
2877+
2878+
/**
2879+
* @throws Exception on test failure
2880+
*/
2881+
@Test
2882+
@Alerts(DEFAULT = {"[1, 0, 0, 1, Infinity, Infinity]",
2883+
"1[1, 0, 0, 0]",
2884+
"2[0, 1, 0, 0]",
2885+
"3[0, 0, 1, 0]",
2886+
"4[Infinity, Infinity, 0, 1]",
2887+
"true"},
2888+
FF = {"[1, 0, 0, 1, NaN, NaN]",
2889+
"1[1, 0, 0, 0]",
2890+
"2[0, 1, 0, 0]",
2891+
"3[0, 0, 1, 0]",
2892+
"4[NaN, NaN, 0, 1]",
2893+
"true"},
2894+
FF_ESR = {"[1, 0, 0, 1, NaN, NaN]",
2895+
"1[1, 0, 0, 0]",
2896+
"2[0, 1, 0, 0]",
2897+
"3[0, 0, 1, 0]",
2898+
"4[NaN, NaN, 0, 1]",
2899+
"true"})
2900+
@HtmlUnitNYI(
2901+
CHROME = {"[1, 0, 0, 1, NaN, NaN]",
2902+
"1[1, 0, 0, 0]",
2903+
"2[0, 1, 0, 0]",
2904+
"3[0, 0, 1, 0]",
2905+
"4[NaN, NaN, NaN, NaN]",
2906+
"true"},
2907+
EDGE = {"[1, 0, 0, 1, NaN, NaN]",
2908+
"1[1, 0, 0, 0]",
2909+
"2[0, 1, 0, 0]",
2910+
"3[0, 0, 1, 0]",
2911+
"4[NaN, NaN, NaN, NaN]",
2912+
"true"},
2913+
FF = {"[1, 0, 0, 1, NaN, NaN]",
2914+
"1[1, 0, 0, 0]",
2915+
"2[0, 1, 0, 0]",
2916+
"3[0, 0, 1, 0]",
2917+
"4[NaN, NaN, NaN, NaN]",
2918+
"true"},
2919+
FF_ESR = {"[1, 0, 0, 1, NaN, NaN]",
2920+
"1[1, 0, 0, 0]",
2921+
"2[0, 1, 0, 0]",
2922+
"3[0, 0, 1, 0]",
2923+
"4[NaN, NaN, NaN, NaN]",
2924+
"true"})
2925+
public void translate_withInfinity() throws Exception {
2926+
final String html = DOCTYPE_HTML
2927+
+ "<html>\n"
2928+
+ "<body>\n"
2929+
+ "<script>\n"
2930+
+ LOG_TITLE_FUNCTION
2931+
+ DUMP_FUNCTION
2932+
+ "let m = new DOMMatrixReadOnly();\n"
2933+
+ "let translated = m.translate(Infinity, Infinity);\n"
2934+
+ "dump(translated);\n"
2935+
+ "</script>\n"
2936+
+ "</body></html>";
2937+
2938+
loadPageVerifyTitle2(html);
2939+
}
2940+
2941+
/**
2942+
* @throws Exception on test failure
2943+
*/
2944+
@Test
2945+
@Alerts({"[1, 0, 0, 1, 10, 0]",
2946+
"1[1, 0, 0, 0]",
2947+
"2[0, 1, 0, 0]",
2948+
"3[0, 0, 1, 0]",
2949+
"4[10, 0, 0, 1]",
2950+
"true"})
2951+
public void translate_onlyX() throws Exception {
2952+
final String html = DOCTYPE_HTML
2953+
+ "<html>\n"
2954+
+ "<body>\n"
2955+
+ "<script>\n"
2956+
+ LOG_TITLE_FUNCTION
2957+
+ DUMP_FUNCTION
2958+
+ "let m = new DOMMatrixReadOnly();\n"
2959+
+ "let translated = m.translate(10);\n"
2960+
+ "dump(translated);\n"
2961+
+ "</script>\n"
2962+
+ "</body></html>";
2963+
2964+
loadPageVerifyTitle2(html);
2965+
}
2966+
2967+
/**
2968+
* @throws Exception on test failure
2969+
*/
2970+
@Test
2971+
@Alerts({"[1, 0, 0, 1, 10, 20]",
2972+
"1[1, 0, 0, 0]",
2973+
"2[0, 1, 0, 0]",
2974+
"3[0, 0, 1, 0]",
2975+
"4[10, 20, 30, 1]",
2976+
"false"})
2977+
public void translate_withZ() throws Exception {
2978+
final String html = DOCTYPE_HTML
2979+
+ "<html>\n"
2980+
+ "<body>\n"
2981+
+ "<script>\n"
2982+
+ LOG_TITLE_FUNCTION
2983+
+ DUMP_FUNCTION
2984+
+ "let m = new DOMMatrixReadOnly();\n"
2985+
+ "let translated = m.translate(10, 20, 30);\n"
2986+
+ "dump(translated);\n"
2987+
+ "</script>\n"
2988+
+ "</body></html>";
2989+
2990+
loadPageVerifyTitle2(html);
2991+
}
26962992
}

0 commit comments

Comments
 (0)