I'm building a photo frame with a infinite animation loop. It's a colored rectangle that goes on and on for each border. It's built using pure Javascript and the frame is drawn on canvas.
How can I recreate the effect permanently? I want this frame to go on and on forever.
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var objectSpeed = 8;
var recTop = {
x: -300,
y: 0,
width: 300,
height: 20,
isMoving: true
};
var recRight = {
x: 480,
y: -480,
width: 20,
height: 300,
isMoving:false
};
var recBottom = {
x: 500,
y: 480,
width: 300,
height: 20,
isMoving:false
};
var recLeft = {
x: 0,
y: 500,
width: 20,
height: 300,
isMoving:false
};
function drawRecTop(recTop, context) {
context.beginPath();
context.rect(recTop.x, recTop.y, recTop.width, recTop.height);
context.fillStyle = '#FB0202';
context.fill();
}
function drawRecRight(recRight, context) {
context.beginPath();
context.rect(recRight.x , recRight.y , recRight.width, recRight.height);
context.fillStyle = '#FB0202';
context.fill();
}
function drawRecBottom(recBottom, context) {
context.beginPath();
context.rect(recBottom.x , recBottom.y , recBottom.width, recBottom.height);
context.fillStyle = '#FB0202';
context.fill();
}
function drawRecLeft(recLeft, context) {
context.beginPath();
context.rect(recLeft.x , recLeft.y , recLeft.width, recLeft.height);
context.fillStyle = '#FB0202';
context.fill();
}
var squareXSpeed = objectSpeed;
var squareYSpeed = objectSpeed;
function animate(myRectangle, canvas, context, startTime) {
if(recTop.x > canvas.width - recTop.width && !recRight.isMoving){
//START RIGHT RECTANGLE MOVEMENT
recRight.isMoving = true;
recRight.y = - recRight.height + recRight.width;
}
if(recRight.y > canvas.height - recRight.height && !recBottom.isMoving){
//START BOTTOM RECTANGLE MOVEMENT
recBottom.isMoving = true;
recBottom.x = canvas.width-recBottom.height;
}
if(recBottom.x < 0 && !recLeft.isMoving){
//START LEFT RECTANGLE MOVEMENT
// recLeft.y = - recLeft.width + recLeft.height;
recLeft.isMoving = true;
recLeft.y = canvas.height-recLeft.width;
}
if(recLeft.y < 0 && !recTop.isMoving){
//START BOTTOM RECTANGLE MOVEMENT
recTop.isMoving = true;
recTop.x = -(canvas.width - recTop.width);
}
if(recTop.x > canvas.width && recLeft.isMoving){
//TOP RECTANGLE HAS LEFT THE STAGE
recTop.isMoving = false;
}
if(recTop.isMoving)recTop.x += objectSpeed;
if(recRight.isMoving)recRight.y += objectSpeed;
if(recBottom.isMoving)recBottom.x -= objectSpeed;
if(recLeft.isMoving)recLeft.y -= objectSpeed;
// clear
context.clearRect(0, 0, canvas.width, canvas.height);
drawRecTop(recTop, context);
drawRecRight(recRight, context);
drawRecBottom(recBottom, context);
drawRecLeft(recLeft, context);
// request new frame
requestAnimFrame(function() {
animate(recLeft, canvas, context, startTime);
});
}
Here's what I got so far: https://jsfiddle.net/jwp9ya5w/