About Monkey 2 › Forums › Monkey 2 Code Library › Emscripten scale-to-fit canvas to window view script.
This topic contains 0 replies, has 1 voice, and was last updated by 
 Richard Betson
 8 months ago.
		Viewing 1 post (of 1 total)
	
	- 
		AuthorPosts
 - 
		
			
				
August 21, 2018 at 2:02 am #15316
Hi,
I came up with this to scale my apps canvas to fit the window view. Movement of the mouse, clicking on the canvas or changing focus will trigger the script and scale the canvas to fit. I use it in my web app entry page on my website.
Just slip this into your projects Emscripten launch HTML page.
Monkey123456789101112131415161718<script type="text/javascript">var canvas = document.querySelector('canvas');function resize() {canvas.style.width = window.innerWidth+'px';canvas.style.height = window.innerHeight+'px';}window.addEventListener('blur', resize, false);window.addEventListener('mousemove', resize, false);canvas.addEventListener('resize', resize, false);window.addEventListener('click', resize, false);</script>.
Here is my entire launch HTML file. You can just replace the “your whatever’s” with your own.
JavaScript123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131<!doctype html><html><head><title>your page</title><link rel="stylesheet" href="style.css"><style>html, body {width: 100%;height: 100%;margin: 0px;border: 0;position: absolute;overflow: hidden; /* Disable scrollbars */display: block; /* No floating content on sides */}</style></head><body style="background-image:url('http://your-website.com/your-image.jpg')"><div class="status"><span id="status"></span></div><div class="canvas"><div class="center"><canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas></div></div><script type="text/javascript">var canvas = document.querySelector('canvas');function resize() {canvas.style.width = window.innerWidth+'px';canvas.style.height = window.innerHeight+'px';}window.addEventListener('blur', resize, false);window.addEventListener('mousemove', resize, false);canvas.addEventListener('resize', resize, false);window.addEventListener('click', resize, false);</script><script type="text/javascript">var statusElement = document.getElementById('status');var Module = {preRun: [],postRun: [],print: (function() {var element = document.getElementById('output');if (element) element.value = ''; // clear browser cachereturn function(text) {if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');// These replacements are necessary if you render to raw HTML//text = text.replace(/&/g, "&");//text = text.replace(/</g, "<");//text = text.replace(/>/g, ">");//text = text.replace('\n', '<br>', 'g');console.log(text);if (element) {element.value += text + "\n";element.scrollTop = element.scrollHeight; // focus on bottom}};})(),canvas: (function() {var canvas = document.getElementById('canvas');// As a default initial behavior, pop up an alert when webgl context is lost. To make your// application robust, you may want to override this behavior before shipping!// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);return canvas;})(),setStatus: function(text) {if( !statusElement ) return;if( text ){statusElement.innerHTML=text;statusElement.hidden=false;}else{statusElement.hidden=true;}}};Module.setStatus( 'Downloading...' );window.resizeTo(500,500)window.onerror = function(event) {// TODO: do not warn on ok events like simulating an infinite loop or exitStatusModule.setStatus('Exception thrown, see JavaScript console');window.scrollTo(0,0);Module.setStatus = function(text) {if (text) Module.printErr('[post-exception status] ' + text);};};</script><script async type="text/javascript" src="your-webapp.js"></script></body></html> - 
		AuthorPosts
 
		Viewing 1 post (of 1 total)
	
	You must be logged in to reply to this topic.