pixel-editor/js/_canvas.js
npalomba e68f495d50 Added functions for canvas management in order to create multiple underlying canvases for implementing layers.
Signed-off-by: npalomba <nicola.palomba@studenti.galileilivorno.gov.it>
2019-03-31 15:55:08 +02:00

34 lines
1.2 KiB
JavaScript

function Canvas(height, width, canvas) {
this.canvasSize = [width, height],
this.canvas = canvas,
this.initialize = function() {
var maxHorizontalZoom = Math.floor(window.innerWidth/canvasSize[0]*0.75);
var maxVerticalZoom = Math.floor(window.innerHeight/canvasSize[1]*0.75);
zoom = Math.min(maxHorizontalZoom,maxVerticalZoom);
if (zoom < 1) zoom = 1;
//resize canvas
this.canvas.width = canvasSize[0];
this.canvas.height = canvasSize[1];
this.canvas.style.width = (this.canvas.width*zoom)+'px';
this.canvas.style.height = (this.canvas.height*zoom)+'px';
//unhide canvas
this.canvas.style.display = 'block';
//center canvas in window
this.canvas.style.left = 64+canvasView.clientWidth/2-(canvasSize[0]*zoom/2)+'px';
this.canvas.style.top = 48+canvasView.clientHeight/2-(canvasSize[1]*zoom/2)+'px';
},
this.resize = function() {
let newWidth = (this.canvas.width*zoom)+'px';
let newHeight = (this.canvas.height*zoom)+'px';
this.canvas.style.width = newWidth;
this.canvas.style.height = newHeight;
this.width = newWidth;
this.height = newHeight;
}
}