puzzle/game/Camera.js

135 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-11-07 11:35:29 +01:00
export default class Camera {
2020-12-24 20:17:01 +01:00
constructor(canvas) {
this.x = 0
this.y = 0
// TODO: when canvas resizes, this should
// syncronize with the cam
this.width = canvas.width
this.height = canvas.height
2020-11-07 11:35:29 +01:00
2020-12-24 20:17:01 +01:00
this.zoom = 1
this.minZoom = .1
this.maxZoom = 6
this.zoomStep = .05
}
rect() {
return {
x: -this.x,
y: -this.y,
w: this.width / this.zoom,
h: this.height / this.zoom,
2020-11-07 11:35:29 +01:00
}
2020-12-24 20:17:01 +01:00
}
move(x, y) {
this.x += x / this.zoom
this.y += y / this.zoom
}
2020-11-07 17:49:42 +01:00
2021-04-15 10:01:27 +02:00
setZoom(newzoom, centerCoordViewport) {
2020-12-24 20:17:01 +01:00
const zoom = Math.min(Math.max(newzoom, this.minZoom), this.maxZoom)
if (zoom == this.zoom) {
return false
2020-11-07 11:35:29 +01:00
}
2021-04-15 10:01:27 +02:00
const zoomToCoord = center || {
x: this.width / 2,
y: this.height / 2
}
const zoomFactor = (1 / this.zoom) - (1 / zoom)
this.x -= Math.round(zoomToCoord.x * zoomFactor)
this.y -= Math.round(zoomToCoord.y * zoomFactor)
2020-11-12 19:19:02 +01:00
2020-12-24 20:17:01 +01:00
this.zoom = zoom
2021-04-15 10:01:27 +02:00
2020-12-24 20:17:01 +01:00
return true
}
2020-11-12 19:19:02 +01:00
2021-04-15 10:01:27 +02:00
zoomOut(centerCoordViewport) {
return this.setZoom(this.zoom - this.zoomStep * this.zoom, centerCoordViewport)
2020-12-24 20:17:01 +01:00
}
2021-04-15 10:01:27 +02:00
zoomIn(centerCoordViewport) {
return this.setZoom(this.zoom + this.zoomStep * this.zoom, centerCoordViewport)
2020-12-24 20:17:01 +01:00
}
/**
* Translate a coordinate in the viewport to a
* coordinate in the world, rounded
* @param {x, y} coord
*/
viewportToWorld(coord) {
const worldCoord = this.viewportToWorldRaw(coord)
return {
x: Math.round(worldCoord.x),
y: Math.round(worldCoord.y),
2020-11-07 11:35:29 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-07 11:35:29 +01:00
2020-12-24 20:17:01 +01:00
/**
* Translate a coordinate in the viewport to a
* coordinate in the world, not rounded
* @param {x, y} coord
*/
viewportToWorldRaw(coord) {
return {
x: (coord.x / this.zoom) - this.x,
y: (coord.y / this.zoom) - this.y,
2020-11-12 19:19:02 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-07 11:35:29 +01:00
2020-12-24 20:17:01 +01:00
/**
* Translate a coordinate in the world to a
* coordinate in the viewport, rounded
* @param {x, y} coord
*/
worldToViewport(coord) {
const viewportCoord = this.worldToViewportRaw(coord)
return {
x: Math.round(viewportCoord.x),
y: Math.round(viewportCoord.y),
2020-11-07 11:35:29 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-07 11:35:29 +01:00
2020-12-24 20:17:01 +01:00
/**
* Translate a coordinate in the world to a
* coordinate in the viewport, not rounded
* @param {x, y} coord
*/
worldToViewportRaw(coord) {
return {
x: (coord.x + this.x) * this.zoom,
y: (coord.y + this.y) * this.zoom,
2020-11-07 11:35:29 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-07 17:49:42 +01:00
2020-12-24 20:17:01 +01:00
/**
* Translate a 2d dimension (width/height) in the world to
* one in the viewport, rounded
* @param {x, y} coord
*/
worldDimToViewport(dim) {
const viewportDim = this.worldDimToViewportRaw(dim)
return {
w: Math.round(viewportDim.w),
h: Math.round(viewportDim.h),
2020-11-07 17:49:42 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-10 18:48:16 +01:00
2020-12-24 20:17:01 +01:00
/**
* Translate a 2d dimension (width/height) in the world to
* one in the viewport, not rounded
* @param {x, y} coord
*/
worldDimToViewportRaw(dim) {
return {
w: dim.w * this.zoom,
h: dim.h * this.zoom,
2020-11-10 18:48:16 +01:00
}
2020-12-24 20:17:01 +01:00
}
2020-11-07 17:49:42 +01:00
}