This is going to be a short one. Basically, I am trying to recreate the Y as seen on the homepage of this site - https://yalantis.com/.
I've never used Canvas before and am not very proficient with JS.
I've managed to draw a canvas and got as far as getting the shape I want and a rotate animation but I cannot seem to find a source that explains how the canvas is moving on the yalantis site.
This is what I have done so far:
var canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
// create mouse event listener
const mouse = { x: 0, y: 0 };
function mouseEvents(e) {
const bounds = canvas.getBoundingClientRect();
mouse.x = e.pageX - bounds.left - scrollX;
mouse.y = e.pageY - bounds.top - scrollY;
}
document.addEventListener("mousemove", mouseEvents);
// draw design at x,y and rotated by angle
function drawRotated(x, y, angle) {
ctx.setTransform(1, 0, 0, 1, x, y);
ctx.rotate(angle);
ctx.beginPath();
// ctx.arc(0, 0, 100, 0, Math.PI * 2);
// ctx.moveTo(-100, 0);
// ctx.lineTo(100, 0);
// ctx.lineTo(60, -80);
// ctx.stroke();
ctx.font = '600pt Rig Sans';
ctx.lineWidth = 1;
ctx.strokeStyle = 'blue';
ctx.strokeText('W', x, y);
ctx.closePath();
ctx.stroke();
}
// render loop called 60 times a second
function update(timer) {
ctx.setTransform(1, 0, 0, 1, 0, 0); // reset transform
ctx.clearRect(0, 0, 300, 300);
// get angle from center to mouse
var angle = Math.atan2(mouse.y - 150, mouse.x - 150);
// draw rotated design
drawRotated(150, 150, angle);
requestAnimationFrame(update);
}
requestAnimationFrame(update);
$('#canvas').attr('height', $(document).height());
$('#canvas').attr('width', $(document).width());
Although this is also a bit of a bust since it's duplicating itself on every mousemove and rotating on the spot in a weird position.
Any help would be appreciated. Thanks.