pixi.js retina display doubled in size - pixi.js

I know the same question has been asked by other people before, but that was a long time ago and the API are changed, and they didn't solve my problem.
Here is my problem:
I have a problem about retina display, let's take iPhone 6 for example. The size of the screen is 375x667, and the device pixel ratio is 2, which means the physical pixel is 750x1334 and the CSS pixel is 375x667.
If we want a full screen canvas, what we usually do is to set the canvas size to 750x1334, and then set the actually display size of canvas to 375x667.
This is what I have done in pixi.js(v5.1.1):
let app = new Application({
width: 375,
height: 667,
antialiasing: true,
resolution: 2,
autoDensity: true,
})
I set the resolution : 2, and autoDensity : true, but when I add a sprite with size of 750x1334, it only shows a quarter of the sprite(The size is doubled). It seems that the autoDensity is not working.
What now I am doing is setting the whole stage with scale(0.5):
app.stage.scale.set(0.5);
It temporarily solved my problem, but I feel like this is not the current way to go. You shouldn't use scale for this kind of thing.
What should I do then?

Use CSS to set the canvas size in CSS units, and initialize the renderer using the proper dimensions according to the devices pixel ratio.
let canvas = document.createElement("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let app = new Application({
width:window.innerWidth*window.devicePixelRatio,
height:window.innerHeight*window.devicePixelRatio,
antialiasing: true,
view:canvas
});

Related

Photoshop Script Resize Image, does not update the resolution

I have an image when manually resized to width 1000pts the resolution comes to 261.5pixels/inch
Original Image Dimensions
Manual Update width to 1000pts
The resolution downgraded to 261.5 px/in
When i try the same programmatically with js script the width changes but resolution is same as of the original image
JS Code
document.resizeImage(UnitValue(parseInt(row.width),"pt"), null, null); //working
where row.width = 1000
Image dimensions after executing the js script
How to calculate the resolution of the image automatically and set to 261.5px/inch
The new resolution should be manually calculated.
newResolution = document.resolution * (originalWidth / parseInt(row.width));
document.resizeImage(null, null, newResolution, ResampleMethod.NONE);
document.resizeImage(UnitValue(parseInt(row.width), "pt"), null, null);
If the width is changed, the height will be changed programatically, which caused an impression, ResampleMethod.NONE will set the resolution too.
Not sure if you want to shrink the image or the canvas. I've got with the former.
Use app.activeDocument.resizeImage with a width, height and resolution of your image.
resizemeth = ResampleMethod.BICUBIC;
res = 72;
w = 1000;
h = 673;
app.activeDocument.resizeImage(w, h, res, resizemeth);
Also check what units you are using.
alert(app.preferences.rulerUnits);

How to do svg scaling with Pixi

I've been trying to do SVG scaling with PIXI but the results are not really what I expected them to be. As you can see in the image, the debian logo, which is a SVG file, seems to be blurry and edgy. Am I writing my code wrong:
Refined from https://github.com/kevguy/D3-Svg-Comparison/blob/master/src/components/SvgCompare.vue:
// initialization
this.renderer = new PIXI.Application(800, 600, {backgroundColor: 0x1099bb})
document.getElementById('svg-canvas').appendChild(this.renderer.view)
this.container = new PIXI.Container()
this.stage = this.renderer.stage
this.stage.addChild(this.container)
// appending the svg file
const texture = PIXI.Texture.fromImage(this.chosenImage)
this.svg = new PIXI.Sprite(texture)
this.svg.anchor.x = 0.8
this.svg.anchor.y = 0.8
this.svg.position.x = 400
this.svg.position.y = 300
this.svg.scale.x = this.selectedScale
this.svg.scale.y = this.selectedScale
this.container.addChild(this.svg)
chosenImage is the svg file retrieved by using import * as choesnImage from 'the-file-path'
selectedScale is the selected scaling value which can be changed dynamically thanks to VueJS
You can check out my work here and its corresponding GitHub repo
The bunny logo is to verify when the scaling happens, it only applies to the SVG not the whole canvas.
According to this issue you need to load svg like this to generate scaled svg texture.
PIXI.Texture.fromImage(this.chosenImage, undefined, undefined, newVal)
And need to clean texture cache then create new texture for every new scale change.
PIXI.utils.clearTextureCache()
Modified SvgCompare.vue on gist

Chrome capture visible tab gives messy result

I have a Chrome extension, where I'm using the captureVisibleTab method of the chrome.tabs API to capture screenshots. I have tested the extension on 3 devices (Chromebooks) and I'm getting mixed results. Two of them work perfectly, but one always returns a completely malformed screenshot.
My code:
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
chrome.tabs.get(tabId, function (_tab) {
if (_tab.status == "complete" && _tab.active ) {
chrome.tabs.captureVisibleTab( function(dataUrl){
});
}
});
});
Any ideas what could be the issue on that one device?
EDIT
Example of a bad screenshot:
I suspect that the device pixel ratio is higher on your 3rd device. This was an issue I was having with Retina displays when building a screenshot app. Basically, certain high-resolution displays have a higher ratio pixels per square inch. You're going to want to find window.devicePixelRatio and divide the context scale by that amount.
Assuming that you are using Canvas to draw the screenshot and capture it into an image, this little snippet should help show what you're going to want to do:
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
if(window.devicePixelRatio > 1){
context.scale(1/window.devicePixelRatio, 1/window.devicePixelRatio);
}
context.drawImage(image, 0, 0);
Let me know if that works for you.

How do I read the properties of a KineticJS Sprite other than X and Y coords?

I am new to using KineticJS and am using it to develop a simple 2D game which involves sprites, collision detection etc.
I need to access various properties of my sprite object (let's call it spriteObj here).
I can get to the sprite coordinates using spriteObj.getX() and spriteObj.getY() and this works perfectly.
Now I need to also retrieve the other properties of the sprite, such as height and width, but there does not seem to be a way to do it. Specifically if I have an animation within my sprite, how can I retrieve the current frame and/or the x, y, height, width of that animation frame.
What I really need is something like spriteObj.getWidth() and spriteObj.getHeight() but these do not exist. Any suggestions please? I can see nothing on the html5 tutorials page for this (see here)
Thanks,
Owen
The methods you are looking for do exist, check the KineticJS Sprite API
Here is the KineticJS Sprite Tutorial
Here are the functions you asked for:
Sprite.getX()
Sprite.getY()
Sprite.getWidth()
Sprite.getHeight()
Sprite.getIndex()
Based off of that tutorial I added some code to fit your needs, check the console.log to see the methods in action. For the width and height, although it is not necessary, you have to specify a width and height property on your spriteObj or else it will just return 0.
var blob = new Kinetic.Sprite({
x: 250,
y: 40,
image: imageObj,
animation: 'idle',
animations: animations,
frameRate: 7,
index: 0,
width: 80,
height: 100
});
And a new button to test the getIndex() function:
// stop animation
document.getElementById('stop').addEventListener('click', function () {
alert(blob.getIndex());
}, false);
jsfiddle

tag cloud, layout: horizontal, and Appcelerator Titanium

Please note: This question relates to the Appcelerator Titanium platform, not the stock iOS SDK.
I'm making a tag cloud with a layout: horizontal view. I'm most of the way there, but I can't get the final Titanium.UI.Label on a line to wrap if it doesn't fit. Instead, it gets ellipsized (in a useless manner).
Is there a way I can prevent this on iOS? Seems to work fine on Android.
If you try to set the label width to auto, Titanium will calculate the label width in runtime.
It make sense to get a ellipsized label in horizontal view.
You may need to determine the dynamic label width in your tag cloud case. But just leave it to titanium, you just need to change the dynamic width to static width with this tricky code.
for (var i = 0; i < data.length; i++) {
var label = Ti.UI.createLabel({ text: data[i], width: 'auto', height: 20,left: 5, top: 5});
label.width = label.width + 5; //this determine width in runtime assign back to static width
view.add(label);
}
The iPhone's answer to this is minimumFontSize but that makes no sense in a tag cloud... Have you considered adding this to a horizontal scrollview and setting the contentWidth to auto?
Also does each of your label have it's width set to 'auto'? I imagine setting that would cause the word to overflow the layout and be pushed down to the next line.

Resources