define('DISALLOW_FILE_EDIT', true);{"id":4365,"date":"2014-03-03T19:25:12","date_gmt":"2014-03-04T00:25:12","guid":{"rendered":"http:\/\/www.unfocus.com\/?p=4365"},"modified":"2015-12-15T01:30:38","modified_gmt":"2015-12-15T06:30:38","slug":"hidpiretina-for-createjs-flash-pro-html5-canvas","status":"publish","type":"post","link":"http:\/\/www.unfocus.com\/2014\/03\/03\/hidpiretina-for-createjs-flash-pro-html5-canvas\/","title":{"rendered":"HiDPI\/Retina for CreateJS (Flash Pro HTML5 Canvas)"},"content":{"rendered":"

Adding HiDPI and Retina screen support to a CreateJS (Flash HTML5 canvas publish with EaselJS) is easy enough. Just add this code after where the stage is defined in your published html file (either inside the generated init function, or handleComplete if there are external assets to load):<\/p>\n

[js]
\nif (window.devicePixelRatio) {
\n\t\/\/ grab the width and height from canvas
\n\tvar height = canvas.getAttribute(‘height’);
\n\tvar width = canvas.getAttribute(‘width’);
\n\t\/\/ reset the canvas width and height with window.devicePixelRatio applied
\n\tcanvas.setAttribute(‘width’, Math.round(width * window.devicePixelRatio));
\n\tcanvas.setAttribute(‘height’, Math.round( height * window.devicePixelRatio));
\n\t\/\/ force the canvas back to the original size using css
\n\tcanvas.style.width = width+"px";
\n\tcanvas.style.height = height+"px";
\n\t\/\/ set CreateJS to render scaled
\n\tstage.scaleX = stage.scaleY = window.devicePixelRatio;
\n}
\n[\/js]<\/p>\n

IE 10 doesn’t support devicePixelRatio, but you can get a resonable devicePixelRatio with this<\/a> (include it before your CreateJS script, and call window.getDevicePixelRatio() instead of using the standard property):<\/p>\n

[js]
\n\/*! GetDevicePixelRatio | Author: Tyson Matanich, 2012 | License: MIT *\/
\n(function (window) {
\n\twindow.getDevicePixelRatio = function () {
\n\t\tvar ratio = 1;
\n\t\t\/\/ To account for zoom, change to use deviceXDPI instead of systemXDPI
\n\t\tif (window.screen.systemXDPI !== undefined &amp;&amp; window.screen.logicalXDPI !== undefined &amp;&amp; window.screen.systemXDPI &gt; window.screen.logicalXDPI) {
\n\t\t\t\/\/ Only allow for values &gt; 1
\n\t\t\tratio = window.screen.systemXDPI \/ window.screen.logicalXDPI;
\n\t\t}
\n\t\telse if (window.devicePixelRatio !== undefined) {
\n\t\t\tratio = window.devicePixelRatio;
\n\t\t}
\n\t\treturn ratio;
\n\t};
\n})(this);
\n[\/js]<\/p>\n

If you like to load your CreateJS based animation in an iframe (I do) and the canvas content is the only content, you may also want to add some styles to avoid scrollbars and extra padding:<\/p>\n

[css]
\nbody {
\n\tmargin:0;
\n\tpadding:0;
\n}
\ncanvas {
\n\tdisplay: block;
\n}
\n[\/css]<\/p>\n

Before making these edits, I recommend copying the main html page, so that you don’t have to worry about the publisher wiping out your changes when you publish again (if certain things change, you may need to reintegrate your changes).<\/p>\n

Some Notes:<\/p>\n