Skip to content

Instantly share code, notes, and snippets.

@Luftare
Last active November 9, 2017 18:34
Show Gist options
  • Save Luftare/a08c94a4af0e9543b91e647be74c44ff to your computer and use it in GitHub Desktop.
Save Luftare/a08c94a4af0e9543b91e647be74c44ff to your computer and use it in GitHub Desktop.
html canvas rendering class to handle virtual camera and rotation
class View {
constructor({
canvas = document.querySelector("canvas"),
imageNames = [],
cameraAnchor = new Vector(0, 0),
cameraPosition = new Vector(0, 0),
cameraAngle = 0,
cameraScale = 1,
cameraHeight = 1000,
cameraZoom = 1,
imagesPath = "/images/",
}) {
this.camera = {
position: cameraPosition,
anchor: cameraAnchor,
angle: cameraAngle,
scale: cameraScale,
height: cameraHeight,
zoom: cameraZoom,
};
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.handleResize();
this.fillScreen();
this.imageNames = imageNames;
this.images = this.loadImages();
}
preDraw() {
this.canvas.width = this.canvas.width;
this.ctx.setTransform(
this.camera.zoom, 0, 0, this.camera.zoom, -(this.camera.zoom - 1) * this.canvas.width * this.camera.anchor.x,
-(this.camera.zoom - 1) * this.canvas.height * this.camera.anchor.y
);
this.ctx.translate(this.canvas.width * this.camera.anchor.x,this.canvas.height * this.camera.anchor.y);
this.ctx.rotate(this.camera.angle);
this.ctx.translate(- this.camera.position.x, - this.camera.position.y);
}
drawImage(img, x, y, angle = 0, scale = 1) {
if(!img.width && !img.height) img = this.images[img];
const ctx = this.ctx;
ctx.save();
ctx.translate(x + img.width * scale / 2, y + img.height * scale / 2);
ctx.rotate(angle);
ctx.translate(- x - img.width * scale / 2, - y - img.height * scale / 2);
ctx.drawImage(img, x, y, img.width * scale, img.height * scale);
ctx.restore();
}
loadImages() {
const images = {};
this.imageNames.forEach(n => {
images[n] = new Image();
images[n].src = `${this.imagesPath}${n}.png`;
});
return images;
}
fillScreen() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
handleResize() {
window.onresize = this.fillScreen.bind(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment