I have this piece of code that I adjusted to a triangle shape. It uses javascript animation to render the triangle, but I can not have it move to the location the image is set, it instead moves too much, and if I adjust the numbers, it does not reach the triangle in the image well and is not in the right angle.
At the moment, I need the black animation triangle to be on the same angle as the white image and the black triangle also has to move to the same location, meaning the triangle has to be exactly the same as the white on the image.
Also, I have the whole function on a button press, so how could I clear the javascript shape and then when invoked, the shape would make the same animation again? The remove button has to remove the black triangle, and when "start animation" button is pressed again, the triangle would invoke the StartAnimation(); function.
How must I do this, to reach the perfect result?
function StartAnimation() {
let
canvas = document.getElementById('canvas');
let
ctx = canvas.getContext('2d');
let
initCoor = {
x1 : ctx.canvas.width,
y1 : 0,
x2 : ctx.canvas.width,
y2 : ctx.canvas.height / 2,
x3 : ctx.canvas.width,
y3 : ctx.canvas.height / 2,
x4 : (ctx.canvas.width - ctx.canvas.width / 5),
y4 : 0,
};
function initCanvas() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = 'transparent';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function draw_triangle(x1, y1, x2, y2, x3, y3, x4, y4, color) {
ctx.save();
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x4, y4)
ctx.fillStyle = color;
ctx.fill();
}
function setAnimate() {
initCoor.x4 <= 0 ? initCoor.x4 = 0 : initCoor.x4 -= 10;
a = true;
if (a) {
if (initCoor.y2 >= ctx.canvas.height
&& initCoor.y3 >= ctx.canvas.height) {
initCoor.y2 = ctx.canvas.height;
initCoor.y3 = ctx.canvas.height;
if (initCoor.x3 <= 300) {
initCoor.x3 = 300;
window.clearInterval(int);
} else {
initCoor.x3 -= 10;
}
} else {
initCoor.y2 += 40;
initCoor.y3 += 40;
}
draw_triangle(initCoor.x1, initCoor.y1, initCoor.x2,
initCoor.y2, initCoor.x3, initCoor.y3, initCoor.x4,
initCoor.y4, '#000');
}
}
initCanvas();
draw_triangle(initCoor.x1, initCoor.y1, initCoor.x2, initCoor.y2,
initCoor.x3, initCoor.y3, initCoor.x4, initCoor.y4, '#000');
let
int = window.setInterval(setAnimate, 50);
}
body {
background-color: lightblue;
}
#canvascontainer
{
position: absolute;
width: 360px;
height: 360px;
z-index: 2;
}
#image
{
position: absolute;
width: 360px;
height: 360px;
z-index: 3;
}
#canvascontainer2
{
position: absolute;
width: 360px;
left: 300px;
height: 360px;
z-index: 2;
}
<html>
<body>
<h2>This is the problem</h2>
<button type="button" onclick="StartAnimation();">Start animation</button>
<button type="button">Remove shape(how to)</button>
<div id="image">
<img src="https://cdn1.imggmi.com/uploads/2019/10/23/8148aef0f880ccf35a2a78c083c40383-full.png">
</div>
<div id="canvascontainer">
<canvas id="canvas" width="360" height="360">
</canvas>
</div>
<div id="canvascontainer2">
<canvas id="canvas2" width="360" height="360">
</canvas>
</div>
</body>
</html>