Jump to content

[Answered] Why is rescaling in standalone web app broken in iOS10?


trial
 Share

Recommended Posts

Using Animate CC, with publish settings set to make responsive = both, center = both, scale to fill visible area = fit in view, like this:

rescaling.png

In mobile Safari, everything works out well, the canvas resizes as normal when screen orientation changes. When saved to Home Screen as standalone web app though, the canvas isn't resizing after changing to a smaller size, like this:

rescalingscreen.gif

Here's the full html code generated by Animate CC:

<!DOCTYPE html>
<!--
	NOTES:
	1. All tokens are represented by '$' sign in the template.
	2. You can write your code only wherever mentioned.
	3. All occurrences of existing tokens will be replaced by their appropriate values.
	4. Blank lines will be removed automatically.
	5. Remove unnecessary comments before creating your template.
-->
<html>
<head>
<meta charset="UTF-8">
<meta name = "viewport" content = "width=device-width,initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="authoring-tool" content="Adobe_Animate_CC">
<title>testView</title>
<!-- write your code here -->
<style>
  #animation_container {
	position:absolute;
	margin:auto;
	left:0;right:0;
	top:0;bottom:0;
  }
</style>
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<script>
(function (cjs, an) {
var p; // shortcut to reference prototypes
var lib={};var ss={};var img={};
lib.ssMetadata = [];
// symbols:
// stage content:
(lib.testView = function(mode,startPosition,loop) {
	this.initialize(mode,startPosition,loop,{});
}).prototype = p = new cjs.MovieClip();
p.nominalBounds = null;
// library properties:
lib.properties = {
	id: '035086AC98814BD6A0BA64225202ABA0',
	width: 400,
	height: 550,
	fps: 24,
	color: "#3366FF",
	opacity: 1.00,
	manifest: [],
	preloads: []
};
// bootstrap callback support:
(lib.Stage = function(canvas) {
	createjs.Stage.call(this, canvas);
}).prototype = p = new createjs.Stage();
p.setAutoPlay = function(autoPlay) {
	this.tickEnabled = autoPlay;
}
p.play = function() { this.tickEnabled = true; this.getChildAt(0).gotoAndPlay(this.getTimelinePosition()) }
p.stop = function(ms) { if(ms) this.seek(ms); this.tickEnabled = false; }
p.seek = function(ms) { this.tickEnabled = true; this.getChildAt(0).gotoAndStop(lib.properties.fps * ms / 1000); }
p.getDuration = function() { return this.getChildAt(0).totalFrames / lib.properties.fps * 1000; }
p.getTimelinePosition = function() { return this.getChildAt(0).currentFrame / lib.properties.fps * 1000; }
an.bootcompsLoaded = an.bootcompsLoaded || [];
if(!an.bootstrapListeners) {
	an.bootstrapListeners=[];
}
an.bootstrapCallback=function(fnCallback) {
	an.bootstrapListeners.push(fnCallback);
	if(an.bootcompsLoaded.length > 0) {
		for(var i=0; i<an.bootcompsLoaded.length; ++i) {
			fnCallback(an.bootcompsLoaded[i]);
		}
	}
};
an.compositions = an.compositions || {};
an.compositions['035086AC98814BD6A0BA64225202ABA0'] = {
	getStage: function() { return exportRoot.getStage(); },
	getLibrary: function() { return lib; },
	getSpriteSheet: function() { return ss; },
	getImages: function() { return img; }
};
an.compositionLoaded = function(id) {
	an.bootcompsLoaded.push(id);
	for(var j=0; j<an.bootstrapListeners.length; j++) {
		an.bootstrapListeners[j](id);
	}
}
an.getComposition = function(id) {
	return an.compositions[id];
}
})(createjs = createjs||{}, AdobeAn = AdobeAn||{});
var createjs, AdobeAn;
</script>
<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation;
function init() {
	canvas = document.getElementById("canvas");
	anim_container = document.getElementById("animation_container");
	dom_overlay_container = document.getElementById("dom_overlay_container");
	var comp=AdobeAn.getComposition("035086AC98814BD6A0BA64225202ABA0");
	var lib=comp.getLibrary();
	handleComplete({},comp);
}
function handleComplete(evt,comp) {
	//This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
	var lib=comp.getLibrary();
	var ss=comp.getSpriteSheet();
	exportRoot = new lib.testView();
	stage = new lib.Stage(canvas);	
	//Registers the "tick" event listener.
	fnStartAnimation = function() {
		stage.addChild(exportRoot);
		createjs.Ticker.setFPS(lib.properties.fps);
		createjs.Ticker.addEventListener("tick", stage);
	}	    
	//Code to support hidpi screens and responsive scaling.
	function makeResponsive(isResp, respDim, isScale, scaleType) {		
		var lastW, lastH, lastS=1;		
		window.addEventListener('resize', resizeCanvas);		
		resizeCanvas();		
		function resizeCanvas() {			
			var w = lib.properties.width, h = lib.properties.height;			
			var iw = window.innerWidth, ih=window.innerHeight;			
			var pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;			
			if(isResp) {                
				if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                    
					sRatio = lastS;                
				}				
				else if(!isScale) {					
					if(iw<w || ih<h)						
						sRatio = Math.min(xRatio, yRatio);				
				}				
				else if(scaleType==1) {					
					sRatio = Math.min(xRatio, yRatio);				
				}				
				else if(scaleType==2) {					
					sRatio = Math.max(xRatio, yRatio);				
				}			
			}			
			canvas.width = w*pRatio*sRatio;			
			canvas.height = h*pRatio*sRatio;
			canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w*sRatio+'px';				
			canvas.style.height = anim_container.style.height = dom_overlay_container.style.height = h*sRatio+'px';
			stage.scaleX = pRatio*sRatio;			
			stage.scaleY = pRatio*sRatio;			
			lastW = iw; lastH = ih; lastS = sRatio;            
			stage.tickOnUpdate = false;            
			stage.update();            
			stage.tickOnUpdate = true;		
		}
	}
	makeResponsive(true,'both',true,1);	
	AdobeAn.compositionLoaded(lib.properties.id);
	fnStartAnimation();
}
</script>
<!-- write your code here -->
</head>
<body onload="init();" style="margin:0px;">
	<div id="animation_container" style="background-color:rgba(51, 102, 255, 1.00); width:400px; height:550px">
		<canvas id="canvas" width="400" height="550" style="position: absolute; display: block; background-color:rgba(51, 102, 255, 1.00);"></canvas>
		<div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:400px; height:550px; position: absolute; left: 0px; top: 0px; display: block;">
		</div>
	</div>
</body>
</html>

On an iOS9 device, it seems to be working well. How to do update Animate CC's rescaling code so that it works in iOS10 (and above)?

Link to comment
Share on other sites

Strangely enough, when I added this code, and my iOS device has that blue header (for personal hotspot), the standalone web app scales back bigger as normal when the screen orientation changes. It only works in hotspot mode though.

window.addEventListener("orientationchange", function () {
			document.width = window.screen.width;
		});

 

Link to comment
Share on other sites

  • 2 weeks later...

Anyone has any ideas why the responsive code doesn't work?

Because Safari is the new IE6.

Seriously, I've had a group of developers waste a load of time on inaccuracies in Safari like this one, with very mixed success. There are so many little problems with developing for Safari.

The blue hotspot 'banner' is often invisible to the web, ditto for the task bar at the bottom. Dealing with either is not good, in fact, I'm not convinced you _can_ even deal with the hotspot bar effectively, although, as you've noted, different versions manifest different bugs/inaccuracies so maybe it is more accurate to say that you can't fix things for all your users all in one go; i.e. you have to pick the _least_ annoying problem and some users will just have to live with it, either the least annoying problem or the smallest sub-set of users.

Something you could check though, that might help:

*Check when resize fires*

I think that it can be a little inconsistent (across versions) i.e. sometimes it fires early and you'll be measuring the screen _before_ the resize, sometimes it'll fire late, in your case it sounds like maybe it is firing early and you aren't measuring the screen dimensions you think (and want to be) you are.

The only solution for this _early_ firing is to use a small timeout. Of course you don't want to fire it too early and you don't know when the screen will _actually_ rotate so you have to give it a bit more delay than you think, which will be 'flashy' for users, and using timeouts for this stuff is always dangerous because there might be some processing that delays rotation and you get it wrong. You _could_ try to work out the ratio and try to keep track of landscape/portrait to see if you're checking the correct orientation change, but, if you get this wrong (i.e. when the user rotates a lot) then you could get really stuck.

Link to comment
Share on other sites

I see, perhaps a delay would be a good idea. Using this?

setTimeout(function(){
        <code here>
    }, 5000);

Should I put it inside function resizeCanvas()? With the rest of it's code inside the setTimeout function?

Update: You were right, mattstyles. After putting a setTimeout function to delay the resizeCanvas function, the canvas is now scaling properly. Instead of 5000, I put 20, because it seems to work instantaneously at that lower value.

If anyone has a better idea how to implement it, please let me know. Thanks so much!

Link to comment
Share on other sites

@trial asked me on PM how we manage to solve this issue on our games.  I've replied here so anyone else experiencing the same issue may benefit.  Simple answer is to run the resizeCanvas check almost continuously (on update / RAF tick).

Compare a concatenation of window.innerWidth and window.innerHeight to the previous state, if it's the same ignore, if it's different then recompute the resize / css scaling and offsets.  We make no use of any Events for this, which may seem less than efficient but we found Events to be inconsistent (and the overhead of continuous comparison to be minimal).  This analysis was originally done ~2012 so things may have improved since (and other things may have worsened) - but the code from our framework written mostly at that time can be seen here:

https://github.com/hypersurge/awe6/blob/master/lib/awe6/core/drivers/createjs/Kernel.hx#L133

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...