(function() {
	'use strict';

	var scaleFactor,
	    canvas = document.getElementById('canvas'),
	    ctx = canvas.getContext('2d'),
	    CANVAS_DEFAULT_WIDTH = 640,
	    CANVAS_DEFAULT_HEIGHT = 480;

	(function initCanvasResizing() {
		var debounceTmt = null;

		function setCanvasSize() {
			var wWidth = window.innerWidth,
			    wHeight = window.innerHeight;
			canvas.width = wWidth;
			canvas.height = wHeight;
			console.log('Canvas size is set to ' + wWidth + 'x' + wHeight);

			scaleFactor = Math.min(wWidth/CANVAS_DEFAULT_WIDTH, wHeight/CANVAS_DEFAULT_HEIGHT);
			console.log('Scale factor is set to ' + scaleFactor);
		}

		window.onresize = function() {
			clearTimeout(debounceTmt);
			debounceTmt = setTimeout(setCanvasSize, 100);
		};

		//initial canvas size setting
		setCanvasSize();
	})();
})();
(function() {
	'use strict';

	var canvas = document.getElementById('canvas'),
	    ctx = canvas.getContext('2d'),
	    canvasMetrics = {
	    	DEFAULT_WIDTH: 640,
	    	DEFAULT_HEIGHT: 480,
	    	width: null,
	    	height: null,
	    	scaleFactor: null,
	    	offsetX: null,
	    	offsetY: null,
	    };

	(function initCanvasResizing() {
		var debounceTmt = null;

		function setCanvasSize() {
			canvasMetrics.width = window.innerWidth;
			canvasMetrics.height = window.innerHeight;

			canvas.width = canvasMetrics.width;
			canvas.height = canvasMetrics.height;
			console.log('Canvas size is set to ' + canvasMetrics.width + 'x' + canvasMetrics.height);

			canvasMetrics.scaleFactor = Math.min(canvasMetrics.width/canvasMetrics.DEFAULT_WIDTH,
			                                     canvasMetrics.height/canvasMetrics.DEFAULT_HEIGHT);
			console.log('Scale factor is set to ' + canvasMetrics.scaleFactor);

			canvasMetrics.offsetX = Math.floor((canvasMetrics.width - canvasMetrics.DEFAULT_WIDTH*canvasMetrics.scaleFactor)/2);
			canvasMetrics.offsetY = Math.floor((canvasMetrics.height - canvasMetrics.DEFAULT_HEIGHT*canvasMetrics.scaleFactor)/2);
			console.log('Offsets: x=' + canvasMetrics.offsetX + ', y=' + canvasMetrics.offsetY);
		}

		window.onresize = function() {
			clearTimeout(debounceTmt);
			debounceTmt = setTimeout(setCanvasSize, 100);
		};

		//initial canvas size setting
		setCanvasSize();
	})();
})();
Add better canvas metrics calculation