<canvas id="canvas"></canvas>
html { overflow: hidden; }
// This is part of a tutorial called Canvas 101: Rotating Shape
// https://dev.to/brianmontanaweb/canvas-101-rotating-shape-28la
const canvas = document.querySelector("#canvas");
const context = canvas.getContext("2d");
let canvasWidth = canvas.width = window.innerWidth;
let canvasHeight = canvas.height = window.innerHeight;
class Square {
constructor({
width,
height,
rotate = 0,
xPosition = canvasWidth / 2,
yPosition = canvasHeight / 2
}) {
this.width = width;
this.height = height;
this.rotate = rotate;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
}
const square = new Square({ width: 50, height: 50 });
function movement(shape) {
shape.rotate += 0.01;
shape.xPosition += 0.1;
}
function render() {
context.fillStyle = "lightsalmon";
context.fillRect(0, 0, canvasWidth, canvasHeight);
movement(square);
context.save();
context.fillStyle = "salmon";
context.translate(square.xPosition, square.yPosition);
context.rotate(square.rotate);
context.fillRect(-square.width/2, -square.height/2, square.width, square.height);
context.restore();
window.requestAnimationFrame(render);
}
window.requestAnimationFrame(render);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.