the API returns some random coordinates that will be the container of the image, and these coordinates will always be a rectangle but it may not have angles of 90 degrees so I have to deform the image to adapt it to this container.
I think the solution would be transoforming the coordinates to css-transform. Can anyone think of something?
Thank you.
var app = {
init: function() {
let coords = this.getDataFromApi(); // getting coords
this.reDrawPolygon(coords);
this.positionateImg(coords);
},
getDataFromApi: () => {
/* http request with random points*/
return {
"p1": {
"x": 28,
"y": 51
},
"p2": {
"x": 393,
"y": 21
},
"p3": {
"x": 401,
"y": 170
},
"p4": {
"x": 40,
"y": 135
}
};
},
reDrawPolygon: (coords) => {
let svg = document.querySelector('svg');
let points = [];
for (let i in coords) {
points.push(coords[i].x + ' ' + coords[i].y)
}
if (!svg.querySelector('polygon').getAttribute('points')) {
svg.querySelector('polygon').setAttribute('points', points.join())
} else {
svg.querySelector('polygon').setAttribute('points', svg.querySelector('animate').getAttribute('to'))
}
svg.querySelector('animate').setAttribute('to', points.join())
let anim = document.getElementById('animation-to-click');
anim['beginElement']()
},
positionateImg: (coords) => {
let img = document.querySelector('img');
img.style.top = coords.p1.y + 'px';
img.style.left = coords.p1.x + 'px';
img.style.width = (coords.p2.x - coords.p1.x) + 'px';
img.style.height = (coords.p4.y - coords.p1.y) + 'px';
}
}
app.init();
div {
position: relative
width:100vw;
height:100vh;
}
svg {
width: 100%;
height: 100%;
position: relative;
z-index: 1;
}
img {
position: absolute;
transition: all 600ms;
}
<div>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTYOz3O64CoAaQUStkjHStEJZVADvfOlbVzjPok68XTeT-ytdGH" />
<svg>
<polygon style="stroke:#b5bf03;fill:transparent;stroke-width:3" points="0,0,0,0">
<animate id="animation-to-click" fill="freeze" attributeName="points" dur="100ms" />
</polygon>
</svg>
</div>