multi resolution support for all android devices - cocos2d-x

I am porting my cocos2d iPhone game to android using cocos2d-x. I am now facing a problem with screen resolution: I want to use one high resolution image in my game which should be supportable by all screens lower then a given resolution.
I read this nice tutorial of multiple resolution on forum . It's really helpful, but I am not achieving my solution. There is explanation of scale-factor of resource of Design Resolution & Resource Resolution.
But, in my case, it scales either height wise or width wise. Not a perfect scaling of my image. Can someone clarify why for me?

In AppDeligate.cpp add the following lines to
bool AppDelegate::applicationDidFinishLaunching() after the glview is set.
CCEGLView *ev = CCEGLView::sharedOpenGLView();
ev->setDesignResolutionSize(480, 320, kResolutionShowAll);
480, 320 being the resolution you designed your app for. If you want portrait use 320, 480 instead.
This solution will show black borders if the phone aspect ratio doesn't match the 480/320 aspect ratio.

In AppDelegate.cpp
This is for landscape Mode
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
director = CCDirector::sharedDirector();
EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
if(screenSize.height > 480 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}
return true;
}

Here is some code that might help You make following folders in "Resource" folder
ipadhd
ipad
iphone
Use this code in Appdelegate.cpp in applicationdidfinishing method
CCSize screenSize = pEGLView->getFrameSize();
//set design size for iPad retina
CCSize designSize = CCSize(2048, 1536);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionExactFit);
if (screenSize.height > 768) {
CCFileUtils::sharedFileUtils()->setResourceDirectory("ipadhd");
} else if (screenSize.height > 320) {
CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad");
} else {
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
}
pDirector->setContentScaleFactor(screenSize.height/designSize.height)
Hope this helps.Put your images accordingly for iphone in iphone folder,ipad images in ipad folder and hd images in ipadhd folder. pDirector here is CCDirector variable.

i followed this nice tutorial http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Multi_resolution_support
This way it worked for me. i used one high Resolution image
AppDelegate.cpp
typedef struct tagResource
{
cocos2d::CCSize size;
char directory[100];
}Resource;
static Resource smallResource = { cocos2d::CCSizeMake(320,480), "iphone" };
static Resource mediumResource = { cocos2d::CCSizeMake(768,1024), "ipad" };
static Resource largeResource = { cocos2d::CCSizeMake(1536,2048), "ipadhd" };
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(640,1024);
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
CCSize frameSize = pDirector->getOpenGLView()->getFrameSize();
pEGLView->setDesignResolutionSize( designResolutionSize.width, designResolutionSize.height, kResolutionExactFit);
if ((frameSize.height > mediumResource.size.height))
{
pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
}
// if the frame's height is larger than the height of small resource size, select medium resource.
else if ((frameSize.height > smallResource.size.height))
{
pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
}
// if the frame's height is smaller than the height of medium resource size, select small resource.
else
{
pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height);
}
CCDirector::sharedDirector()->setContentScaleFactor(1.f);

Related

Display pixel-perfect canvas on all devices

I have some canvases that I want to be displayed pixel-perfect in every (modern) browser. By default, devices with high-DPI screens are scaling my page so that everything looks the right size, but it's breaking* the appearance of my canvases.
How can I ensure that one pixel in my canvas = one pixel on the screen? Preferably this wouldn't affect other elements on the page, since I still want e.g. text to be scaled appropriately for the device.
I already tried styling the canvas dimensions based on window.devicePixelRatio. That makes the canvases the right size but the contents look even worse. I'm guessing that just scales them down after they're already scaled up incorrectly.
*If you care, because the canvases use dithering and the browsers are doing some kind of lerp rather than nearest-neighbor
For me, only a combination of different 'pixel perfect' techniques helped to archive the results:
Get and scale canvas with a pixel ratio:
pixelRatio = window.devicePixelRatio/ctx.backingStorePixelRatio
Scale the canvas on the resize (avoid canvas default stretch scaling).
multiple the lineWidth with pixelRatio to find proper 'real' pixel line thickness:
context.lineWidth = thickness * pixelRatio;
NOTE: not sure wheter it's valid for all devices
Check whether the thickness of the line is odd or even. add half of the pixelRatio to the line position for the odd thickness values.
x = x + pixelRatio/2;
The odd line will be placed in the middle of the pixel. The line above is used to move it a little bit.
use image-rendering: pixelated;
function getPixelRatio(context) {
dpr = window.devicePixelRatio || 1,
bsr = context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return dpr / bsr;
}
var canvas = document.getElementById('canvas');
var context = canvas.getContext("2d");
var pixelRatio = getPixelRatio(context);
var initialWidth = canvas.clientWidth * pixelRatio;
var initialHeight = canvas.clientHeight * pixelRatio;
window.addEventListener('resize', function(args) {
rescale();
redraw();
}, false);
function rescale() {
var width = initialWidth * pixelRatio;
var height = initialHeight * pixelRatio;
if (width != context.canvas.width)
context.canvas.width = width;
if (height != context.canvas.height)
context.canvas.height = height;
context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
}
function pixelPerfectLine(x1, y1, x2, y2) {
context.save();
context.beginPath();
thickness = 1;
// Multiple your stroke thickness by a pixel ratio!
context.lineWidth = thickness * pixelRatio;
context.strokeStyle = "Black";
context.moveTo(getSharpPixel(thickness, x1), getSharpPixel(thickness, y1));
context.lineTo(getSharpPixel(thickness, x2), getSharpPixel(thickness, y2));
context.stroke();
context.restore();
}
function pixelPerfectRectangle(x, y, w, h, thickness, useDash) {
context.save();
// Pixel perfect rectange:
context.beginPath();
// Multiple your stroke thickness by a pixel ratio!
context.lineWidth = thickness * pixelRatio;
context.strokeStyle = "Red";
if (useDash) {
context.setLineDash([4]);
}
// use sharp x,y and integer w,h!
context.strokeRect(
getSharpPixel(thickness, x),
getSharpPixel(thickness, y),
Math.floor(w),
Math.floor(h));
context.restore();
}
function redraw() {
context.clearRect(0, 0, canvas.width, canvas.height);
pixelPerfectLine(50,50,250,250);
pixelPerfectLine(120,0,120,250);
pixelPerfectLine(122,0,122,250);
pixelPerfectRectangle(10, 11, 200.3, 43.2, 1, false);
pixelPerfectRectangle(41, 42, 150.3, 43.2, 1, true);
pixelPerfectRectangle(102, 100, 150.3, 243.2, 2, true);
}
function getSharpPixel(thickness, pos) {
if (thickness % 2 == 0) {
return pos;
}
return pos + pixelRatio / 2;
}
rescale();
redraw();
canvas {
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
width: 100vh;
height: 100vh;
}
<canvas id="canvas"></canvas>
NOTE: Resize event is not fired in the snipped, so might be not accurate.
The currently marked answer is wrong
There is no way to get a pixel perfect canvas on all devices across all browsers in 2022.
You might think it's working using
canvas {
image-rendering: pixelated;
image-rendering: crisp-edges;
}
But all you have to do to see it fail is press Zoom-In/Zoom-Out. Zooming is not the only place it will fail. Many devices and many OSes have fractional devicePixelRatio.
In other words. Let's say you make a 100x100 canvas (enlarged).
The user's devixePixelRatio is 1.25 (which is what my Desktop PC is). The canvas will then display at 125x125 pixels and your 100x100 pixel canvas will get scaled to 125x125 with nearest neighbor filtering (image-rendering: pixelated) and look really crappy as some pixels are 1x1 pixel big and others 2x2 pixels big.
So no, using
image-rendering: pixelated;
image-rendering: crisp-edges;
by itself is not a solution.
Worse. The browser works in fractional sizes but the device does not. Example:
let totalWidth = 0;
let totalDeviceWidth = 0;
document.querySelectorAll(".split>*").forEach(elem => {
const rect = elem.getBoundingClientRect();
const width = rect.width;
const deviceWidth = width * devicePixelRatio;
totalWidth += width;
totalDeviceWidth += deviceWidth;
log(`${elem.className.padEnd(6)}: width = ${width}, deviceWidth: ${deviceWidth}`);
});
const elem = document.querySelector('.split');
const rect = elem.getBoundingClientRect();
const width = rect.width;
const deviceWidth = width * devicePixelRatio;
log(`
totalWidth : ${totalWidth}
totalDeviceWidth: ${totalDeviceWidth}
elemWidth : ${width}
elemDeviceWidth : ${deviceWidth}`);
function log(...args) {
const elem = document.createElement('pre');
elem.textContent = args.join(' ');
document.body.appendChild(elem);
}
.split {
width: 299px;
display: flex;
}
.split>* {
flex: 1 1 auto;
height: 50px;
}
.left {
background: pink;
}
.middle {
background: lightgreen;
}
.right {
background: lightblue;
}
pre { margin: 0; }
<div class="split">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
The sample above there is 299 CSS pixel wide div and inside there are 3 child divs each taking 1/3 of their parent. Asking for sizes by calling getBoundingClientRect on my MacBook Pro in Chrome 102 I get
left : width = 99.6640625, deviceWidth: 199.328125
middle: width = 99.6640625, deviceWidth: 199.328125
right : width = 99.6640625, deviceWidth: 199.328125
totalWidth : 298.9921875
totalDeviceWidth: 597.984375
elemWidth : 299
elemDeviceWidth : 598
Add them up and you might see a problem. According to getBoundingClientRect each one is about 1/3 width in device pixels (199.328125). You can't actually have 0.328125 device pixels so they'll need to be converted to integers. Let's use Math.round so they all become 199.
199 + 199 + 199 = 597
But according to the browser the size of the parent is 598 device pixels big. Where is the missing pixel?
Let's ask
const observer = new ResizeObserver(entries => {
for (const entry of entries) {
let good = false;
if (entry.devicePixelContentBoxSize) {
// NOTE: Only this path gives the correct answer
// The other paths are imperfect fallbacks
// for browsers that don't provide anyway to do this
width = entry.devicePixelContentBoxSize[0].inlineSize;
height = entry.devicePixelContentBoxSize[0].blockSize;
good = true;
} else {
if (entry.contentBoxSize) {
if (entry.contentBoxSize[0]) {
width = entry.contentBoxSize[0].inlineSize;
height = entry.contentBoxSize[0].blockSize;
} else {
width = entry.contentBoxSize.inlineSize;
height = entry.contentBoxSize.blockSize;
}
} else {
width = entry.contentRect.width;
height = entry.contentRect.height;
}
width *= devicePixelRatio;
height *= devicePixelRatio;
}
log(`${entry.target.className.padEnd(6)}: width = ${width} measure = ${good ? "good" : "bad (not accurate)"}`);
}
});
document.querySelectorAll('.split>*').forEach(elem => {
observer.observe(elem);
});
function log(...args) {
const elem = document.createElement('pre');
elem.textContent = args.join(' ');
document.body.appendChild(elem);
}
.split {
width: 299px;
display: flex;
}
.split>* {
flex: 1 1 auto;
height: 50px;
}
.left {
background: pink;
}
.middle {
background: lightgreen;
}
.right {
background: lightblue;
}
pre { margin: 0; }
<div class="split">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
The code above asks using ResizeObserver and devicePixelContentBoxSize which is only supported on Chromium browsers and Firefox at the moment. For me, the middle div got the extra pixel
left : width = 199
middle: width = 200
right : width = 199
The point of all of that is
You can't just naively set image-rendering: pixelated; image-rendering: crisp-edges
If you want to know how many pixels are in the canvas you may have to ask and you can only find out on Chrome/Firefox ATM
Solutions: TL;DR THERE IS NO CROSS BROWSER SOLUTION IN 2022
In Chrome/Firefox you can use the ResizeObserver solution above.
In Chrome and Firefox you can also compute a fractional sized canvas
In other words. A typical "fill the page" canvas like
<style>
html, body, canvas { margin: 0; width: 100%; height: 100%; display: block; }
<style>
<body>
<canvas>
</canvas>
<body>
will not work. See reasons above. But you can do this
get the size of the container (the body in this case)
multiply by devicePixelRatio and round down
divide by the devicePixelRatio
Use that value for the canvas CSS width and height and the #2 value for the canvas's width and heigh.
This will end up leaving a trailing gap on the right and bottom edges
of 1 to 3 device pixels but it should give you a canvas that is 1x1 pixels.
const px = v => `${v}px`;
const canvas = document.querySelector('canvas');
resizeCanvas(canvas);
draw(canvas);
window.addEventListener('resize', () => {
resizeCanvas(canvas);
draw(canvas);
});
function resizeCanvas(canvas) {
// how many devicePixels per pixel in the canvas we want
// you can set this to 1 if you always want 1 device pixel to 1 canvas pixel
const pixelSize = Math.max(1, devicePixelRatio) | 0;
const rect = canvas.parentElement.getBoundingClientRect();
const deviceWidth = rect.width * devicePixelRatio | 0;
const deviceHeight = rect.height * devicePixelRatio | 0;
const pixelsAcross = deviceWidth / pixelSize | 0;
const pixelsDown = deviceHeight / pixelSize | 0;
const devicePixelsAcross = pixelsAcross * pixelSize;
const devicePixelsDown = pixelsDown * pixelSize;
canvas.style.width = px(devicePixelsAcross / devicePixelRatio);
canvas.style.height = px(devicePixelsDown / devicePixelRatio);
canvas.width = pixelsAcross;
canvas.height = pixelsDown;
}
function draw(canvas) {
const ctx = canvas.getContext('2d');
for (let x = 0; x < canvas.width; ++x) {
let h = x % (canvas.height * 2);
if (h > canvas.height) {
h = 2 * canvas.height - h;
}
ctx.fillStyle = 'lightblue';
ctx.fillRect(x, 0, 1, h);
ctx.fillStyle = 'black';
ctx.fillRect(x, h, 1, 1);
ctx.fillStyle = 'pink';
ctx.fillRect(x, h + 1, 1, canvas.height - h);
}
}
html, body {
margin: 0;
width: 100%;
height: 100%;
/* set to red we can see the gap */
/* set to some other color to hide the gap */
background: red;
}
canvas {
display: block;
image-rendering: pixelated;
image-rendering: crisp-edges;
}
<canvas></canvas>
In Safari there are no solutions: Safari provides neither support for devicePixelContentBoxSize nor does it adjust the devicePixelRatio in response to zooming. On the plus side, Safari never returns a fractional devicePixelRatio, at least as of 2022 but you won't get 1x1 device pixels on Safari if the user zooms.
It's easy:
canvas {
image-rendering: pixelated;
image-rendering: crisp-edges;
}
Browser support isn't great, but so far it's worked on all of the modern browsers I've tested, so I'm happy. I've tested this with Chrome, Firefox, and Safari on Windows, Mac, Linux, iOS, and Android.
This has no effect on normal DPI devices as long as your canvases are being displayed at their normal size. Also, depending on the screen, you get interesting artifacts on high DPI devices, but I think that's unavoidable due to how those displays work. They're still better than the artifacts you get without the style.
I also tried upscaling all of my canvases 200% with nearest-neighbor in javascript and then setting them as background images of normal-sized divs. This is how Apple recommends displaying images for retina devices, so I thought it would give a great result. But you still end up with artifacts and they aren't fixed by zooming in the page so it's actually worse that the simple solution above.

Scaling a 1920 * 1080p photo to be the lock screen image on a 1080p Windows Phone

I want to use the Bing Image of the Day as the background for my app's lock screen image, but I'm having problems getting the image to scale desirably on 1080p devices.
This is an example of a 1080p Bing Image of the Day: http://www.bing.com//az/hprichbg/rb/BeaverMeadow_EN-US12190942812_1920x1080.jpg. It's a 1920 * 1080 photo.
What I do is crop it so that the photo I'm using is 1080 * 1080 pixels, and then create a new lock screen image that's 1080 * 1920. This is the code:
public static void SaveToJpeg(Stream stream)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isostream = iso.CreateFile("lockscreen.jpg"))
{
try
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Cropping image so that only 1080 out of the 1920 horizontal pixels are used.
wb = CropImage(wb, 1080, 1920, 1080, 1080);
// 1080 * 1920 are the phone's dimesions.
Extensions.SaveJpeg(wb, isostream, 1080, 1920, 0, 100);
isostream.Close();
}
catch( Exception e )
{
}
}
}
}
public static WriteableBitmap CropImage(WriteableBitmap source, int phoneWidth, int phoneHeight,
int width, int height)
{
// Based on the phone's width/height and image's width/height, will determine
// the correct x and y offsets.
int xOffset = 0, yOffset = 0;
if( phoneWidth >= source.PixelWidth )
{
xOffset = 0;
}
else
{
xOffset = source.PixelWidth - phoneWidth;
xOffset = xOffset / 2 + xOffset / 4;
}
if (phoneHeight >= height)
{
yOffset = 0;
}
else
{
yOffset = height - phoneHeight;
yOffset = yOffset / 2;
}
var sourceWidth = source.PixelWidth;
// Get the resultant image as WriteableBitmap with specified size
var result = new WriteableBitmap(width, height);
// Create the array of bytes
for (var x = 0; x <= height - 1; x++)
{
var sourceIndex = xOffset + (yOffset + x) * sourceWidth;
var destinationIndex = x * width;
Array.Copy(source.Pixels, sourceIndex, result.Pixels, destinationIndex, width);
}
return result;
}
Unsurprisingly, given that the Bing image's height is 1080 pixels (and not 1920) this is what the lock screen looks like:
And, yes, the custom user control that the lock screen image is created from has its Grid background image stretched to fill:
<Grid.Background>
<ImageBrush
x:Name="Background"
Stretch="Fill"/>
</Grid.Background>
What do I need to do in order to get the Bing image to elegantly fill the screen? That is, I don't want to disproportionately resize (pixelate) the original image in order for it match a 1080p phone's dimensions.
UPDATE: I found an alternative 1080 x 1920 photo for the Bing image of the day (i.e. with exact dimensions of 1080p phone's lock screen): http://www.bing.com//az/hprichbg/rb/BeaverMeadow_EN-US12190942812_1080x1920.jpg.
However using it doesn't seem to fix the underlying problem (note: I'm not cropping anything from this image, but using this image as it is because the dimensions are perfect). See below:
Okay, this was silly. In the lock screen user control's xaml I just had to increase the height of the Grid's final row:
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="0"/>
<RowDefinition Height="30"/>
<RowDefinition Height="160"/>
<RowDefinition Height="18"/>
<RowDefinition Height="1920"/>
</Grid.RowDefinitions>
It was previously set to 900.

Choose the design resolution size for Android devices in Cocos2dx

I'm having trouble when trying to choose design resolution sizes for Android devices.
I just want to have around 4-5 resources (images, background, sprites, etc) but can run in any Android devices. What the suitable design resolution I can do? For example, 320x480, 720x1280, what else?
Can you give me some advice to choose?
Thanks
In your AppDelegate class:
this resolution is for Landscape Mode
bool AppDelegate::applicationDidFinishLaunching()
{
/********************** CCEGLView::sharedOpenGLView()->setDesignResolutionSize() //set design resolution size and mode
********************* CCEGLView::sharedOpenGLView()->getFrameSize() //get screen resolution
*********************CCDirector::sharedDirector()->getWinSize() //get design resolution
********************* CCDirector::sharedDirector()->getVisibleSize() //get design resolution’s visable area size
********************* CCDirector::sharedDirector()->getVisibleOrigin() //get origin of the visable area of design resolution*/
// initialize director
CCDirector* director = CCDirector::sharedDirector();
CCEGLView* EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(1024,614);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
CCLog("Screen Size:%f %f",screenSize.width,screenSize.height);
if(screenSize.height >= 320 && screenSize.height <= 480)
{
CCSize resourceSize = CCSizeMake(800, 480);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Default =%f",resourceSize.height/screenSize.height);
}
else if(screenSize.height >= 540 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.height/screenSize.height);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.height/screenSize.height);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.height/screenSize.height);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}

In cocos2dx using kResolutionShowAll, how can I use the empty borders?

I'm about to develop a game for android with cocos2d-x and I'm having some issues with resolution.
I'm using kResolutionShowAll resolution policy to be sure that all of my image is shown in the device.
But I get a big borders in some devices and I'd like to find out a way to fill those borders with an image, advertisement, or whatever.
I found this: http://www.cocos2d-x.org/forums/6/topics/20512?r=21066 but it doesn't really work.
I don't know if I choose a wrong policy and maybe I should take kResolutionNoBorder instead. Or maybe I misunderstood something about resolution and I'm not in the correct way.
For the support for all devices you can use following code:
**Landscape Mode**
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
director = CCDirector::sharedDirector();
EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);
if(screenSize.height > 480 && screenSize.height < 720 )
{
CCSize resourceSize = CCSizeMake(960, 540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
}
else if (screenSize.height >= 720 && screenSize.height < 800)
{
CCSize resourceSize = CCSizeMake(1280, 720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);
}
else if(screenSize.height > 800)
{
CCSize resourceSize = CCSizeMake(1920, 1080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);
}
else
{
director->setContentScaleFactor(1);
CCLog("Resolution Scale OF S Advance=%f");
}
In the AppDelegate.cpp you can try this.
// initialize director
CCDirector* director = CCDirector::sharedDirector();
CCEGLView* EGLView = CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize = EGLView->getFrameSize();
CCSize designSize = CCSizeMake(800, 480);
EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);

How to scaling big image smoothly in canvas with drawImage?

When I use drawImage to scalle a big image (like 5M), the result looks terrible, is there any easier way that can scalling image with anti-aliasing? MDN has mentioned about this:
Note: Images can become blurry when scaling up or grainy if they're scaled down too much. Scaling is probably best not done if you've got some text in it which needs to remain legible.
EIDT: I wanna scale a image to 90x90, and my code is like this:
that.avatar_height >= that.avatar_width ?
that.context.drawImage($(this)[0], 86, 2, 90, that.avatar_height*90/that.avatar_width) :
that.context.drawImage($(this)[0], 86, 2, that.avatar_width*90/that.avatar_height, 90);
avatar is the image to be scaled.
While scaling the image first get the uploaded image aspect ratio.
/* Calculating aspect ratio */
var imageAspectRatio = imgWidth / imgHeight;
/* Give the needed width and height of the image*/
/*For example you need like this */
var newWidth = 1024;
var newHeight = 768;
if( imgWidth <= 1024 ) {
var newWidth = newHeight * imageAspectRatio;
} else if(imgHeight <= 768) {
var newHeight = newWidth / imageAspectRatio;
} else if( imgWidth >= newWidth ) {
var newWidth = newHeight * imageAspectRatio;
} else if(imgHeight >= newHeight) {
var newHeight = newWidth / imageAspectRatio;
}
If you do this you can't loss you aspect ratio so scaling looks good