JSFiddle - React, Tailwind, and code Playground
HTML
x =
<input type="text" id="x" value="-100" />
y =
<input type="text" id="y" value="-100" />
<br/>skew_x (alpha) =
<input type="text" id="alpha" value="10" />
skew_y (beta) =
<input type="text" id="beta" value="30" />
<br/>rotate (theta) =
<input type="text" id="theta" value="30" />
<br/>scale_x (sx) =
<input type="text" id="sx" value="1" />
scale_y (sy) =
<input type="text" id="sy" value="2" />
<hr/>
<input type="button" id="calc" value="Compute" />
<br/>
<div class="container">
<div id="fig"></div>
</div>
<br/>
<div class="container">
<div id="fig_transformed"></div>
</div>
CSS
.container {
margin-top: 20px;
margin-bottom: 100px;
position: relative;
height: 200px;
width: 200px;
border: 1px solid black;
}
input[type=text] {
width: 40px;
}
#fig, #fig_transformed {
position: absolute;
top : 0;
left: 0;
width: 200px;
height: 200px;
background-color: red;
}
JavaScript
function skew(p, alpha, beta) {
var tan_a = Math.tan(alpha * Math.PI / 180),
tan_b = Math.tan(beta * Math.PI / 180),
p_skewed = {};
p_skewed.x = p.x + p.y * tan_a;
p_skewed.y = p.x * tan_b + p.y;
return p_skewed;
}
function rotate(p, theta) {
var sin_th = Math.sin(theta * Math.PI / 180),
cos_th = Math.cos(theta * Math.PI / 180),
p_rot = {};
p_rot.x = p.x * cos_th - p.y * sin_th;
p_rot.y = p.x * sin_th + p.y * cos_th;
return p_rot;
}
function scale(p, sx, sy) {
var p_scaled = {};
p_scaled.x = p.x * sx;
p_scaled.y = p.y * sy;
return p_scaled;
}
function calc(x, y, alpha, beta, theta, sx, sy) {
var p_end, p = {
x: x,
y: y
};
p_end = skew(
rotate(
scale(p, sx, sy),
theta),
alpha, beta);
console.log(p_end.x);
console.log(p_end.y);
alert("x = " + Math.round(100 * p_end.x) / 100 + "\ny = " + Math.round(100 * p_end.y) / 100);
}
function getNum(id) {
var value = document.getElementById(id).value;
return parseInt(value, 10);
}
function calcVar() {
var x = getNum("x"),
y = getNum("y"),
alpha = getNum("alpha"),
beta = getNum("beta"),
theta = getNum("theta"),
sx = getNum("sx"),
sy = getNum("sy");
document.getElementById("fig_transformed").style.transform = "skew(" + alpha + "deg, " + beta + "deg) rotate(" + theta + "deg) scale(" + sx + ", " + sy + ")";
calc(x, y, alpha, beta, theta, sx, sy);
}
document.getElementById("calc").onclick = calcVar;