how to solve this issue while typing text is correct after is not correct? - phaser-framework

how to solve this issue while typing text is correct after is not correct. seen in images
After
While Typing
var textInput = this.add.sprite(400, 380, 'textinput').setScale(0.6, 0.6);
const text = this.add.text(0, 0, '', { fixedWidth: 200, fixedHeight: 36 })
text.setOrigin(0.5, 0.5)
text.setInteractive().on('pointerdown', () => {
this.rexUI.edit(text)
})
Phaser.Display.Align.In.Center(text, textInput);

The problem is the parameter { fixedWidth: 200, fixedHeight: 36 }. Since those fixed sizes are used for the calculation, and by default the text is aligned top-right.
If you remove that parameter the text should be align in the center of the sprite. Alternativly you could try setting the alignment of the text, with the function setAlgin (link to documentation), but this only will change the horizontal alignment, not the vertical one.
var config = {
type: Phaser.AUTO,
width: 400,
height: 160,
scene: {
create
}
};
function create () {
let rect1 = this.add.rectangle(150, 80, 100, 50, 0xffffff)
.setOrigin(0);
let rect2 = this.add.rectangle(150, 10, 100, 50, 0xffffff)
.setOrigin(0);
let text1 = this.add.text(0, 160, 'TEXT 1 TEXT 1 TEXT 1')
.setBackgroundColor('#ff00ff');
let text2 = this.add.text(0, 160, 'TEXT 2 TEXT 2', { fixedWidth: 200, fixedHeight: 36 })
.setBackgroundColor('#ff00ff')
.setAlign('center');
Phaser.Display.Align.In.Center(text1, rect1)
Phaser.Display.Align.In.Center(text2, rect2)
}
new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser#3.55.2/dist/phaser.js"></script>

Using Padding { fixedWidth: 200, fixedHeight: 40, padding: 8, })

Related

When i generating and saving images the texts in the images are turns to squares in node js

when i'm generating qr images using easyqrcodejs-nodejs and save it in the azure storage texts in the image turns to squares. why is this happens? i used pdfkit and jspdf as the pdf generator. this also happens when i generating and saving the pdf also
var options = {
text: link,
logo: "https://spadesprodblob.blob.core.windows.net/file-storage/restaurant/1641807876154-lg1.png",
width: 220,
height: 220,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H, // L, M, Q, H
title:`Table#${title}`,
titleFont: "normal normal bold 20px Arial", //font. default is "bold 16px Arial"
titleColor: "#000", // color. default is "#000"
titleBackgroundColor: "#fff", // background color. default is "#fff"
titleHeight: 30, // height, including subTitle. default is 0
titleTop: 10, // draws y coordinates. default is 30
subTitle: wifi,
subTitleFont: "normal normal normal 14px Arial", // font. default is "14px Arial"
subTitleColor: "#000", // color. default is "4F4F4F"
subTitleTop: 320, // draws y coordinates. default is 0
quietZoneColor: "rgba(255, 255, 255,0)",
quietZone: 75,
backgroundImage: 'https://spadesprodblob.blob.core.windows.net/file-storage/restaurant/1641802309587-background.png', // Background Image
backgroundImageAlpha: 1, // Background image transparency, value between 0 and 1. default is 1.
autoColorDark: "rgba(0, 0, 0, .6)",
binary: true,
};
// New instance with options
var qrcode = new QRCode(options);
const qr_code_url = await RestaurantService.uploadImageByPath(imageTemppath);
console.log(QRRes)
const image_base_url = await qrcode.toDataURL().then(data => {
return data
});
var doc = new jsPDF();
doc.addImage(image_base_url, 'PNG', 2, 20, 200, 200);

Russian symbols in PDF (pdfkit)

Сreate a pdf file following the example https://pspdfkit.com/blog/2019/generate-invoices-pdfkit-node/
the problem is that characters in Russian are displayed : "Aô#Cä2CT#C¤0", but should be "Проверка". How can I set encoding to UTF-8?
function createInvoice(invoice, path) {
let doc = new PDFDocument({ margin: 50 });
generateHeader(doc);
doc.end();
doc.pipe(fs.createWriteStream(path));
}
function generateHeader(doc) {
doc
.image("logo.png", 50, 45, { width: 50 })
.fillColor("#444444")
.fontSize(20)
.text("Проверка", 110, 57)
.fontSize(10)
.text("Проверка", 100, 65, { align: "right" })
.text("Проверка", 100, 80, { align: "right" })
.moveDown();
}
I downloaded the font file and include it:
.font(`${__dirname}/arial.ttf`)
thanks!
I think that you have to use a font which support russian characters
Look this thread
https://github.com/foliojs/pdfkit/issues/262

How to add Header and footer content to pdfkit for node.js

I would like to generate a pdf using node js (express). I need to add header and footer to every page with page numbers. Any help would be appreciated.
Thanks.
Adding a Footer on all pages
doc.addPage()
let bottom = doc.page.margins.bottom;
doc.page.margins.bottom = 0;
doc.text('Page 1', 0.5 * (doc.page.width - 100), doc.page.height - 50,
{
width: 100,
align: 'center',
lineBreak: false,
})
// Reset text writer position
doc.text('', 50, 50)
doc.page.margins.bottom = bottom;
let pageNumber = 1;
doc.on('pageAdded', () => {
pageNumber++
let bottom = doc.page.margins.bottom;
doc.page.margins.bottom = 0;
doc.text(
'Pág. ' + pageNumber,
0.5 * (doc.page.width - 100),
doc.page.height - 50,
{
width: 100,
align: 'center',
lineBreak: false,
})
// Reset text writer position
doc.text('', 50, 50);
doc.page.margins.bottom = bottom;
})
You can do this :
doc.text('This is a footer', 20, doc.page.height - 50, {
lineBreak: false
});
Adding content to every page using doc.on('pageAdded'... hook has the nasty side effect of hijacking your position (doc.x/doc.y) while filling in a page. Additionally, you have to set the autoFirstPage: false flag in order to inject your hook prior to first page creation.
I find using bufferPages mode and then making global edit to the pages at the end much more graceful/logical.
const doc = new PDFDocument({
bufferPages: true
});
doc.text("Hello World")
doc.addPage();
doc.text("Hello World2")
doc.addPage();
doc.text("Hello World3")
//Global Edits to All Pages (Header/Footer, etc)
let pages = doc.bufferedPageRange();
for (let i = 0; i < pages.count; i++) {
doc.switchToPage(i);
//Header: Add page number
let oldTopMargin = doc.page.margins.top;
doc.page.margins.top = 0 //Dumb: Have to remove top margin in order to write into it
doc
.text(
`Page: ${i + 1} of ${pages.count}`,
0,
(oldTopMargin/2), // Centered vertically in top margin
{ align: 'center' }
);
doc.page.margins.top = oldTopMargin; // ReProtect top margin
//Footer: Add page number
let oldBottomMargin = doc.page.margins.bottom;
doc.page.margins.bottom = 0 //Dumb: Have to remove bottom margin in order to write into it
doc
.text(
`Page: ${i + 1} of ${pages.count}`,
0,
doc.page.height - (oldBottomMargin/2), // Centered vertically in bottom margin
{ align: 'center' }
);
doc.page.margins.bottom = oldBottomMargin; // ReProtect bottom margin
}
doc.end();
about this library, I suggest to read the PDF documentation, it is a lot must complete that the online HTML doc.
Warning : To be able to write outside the main content area, you have to set height and width on text's function params.
so as seen pdf doc you can do :
const doc = new PDFDocument({bufferPages: true})
//addPage X times
const range = doc.bufferedPageRange();
for( let i = range.start; i < (range.start + range.count); i++) {
doc.switchToPage(i);
doc.text(`Page ${i + 1} of ${range.count}`,
200,
doc.page.height - 40,
{ height : 25, width : 100});
}
this works for me
const doc = new PDFDocument({bufferPages: true})
const range = doc.bufferedPageRange();
for (let i = range.start; i <= (doc._pageBufferStart +
doc._pageBuffer.length - 1); i++) {
doc.switchToPage(i);
doc.font('Times-Roman').fontSize(8).text('Footer', 90,
doc.page.height - 40, {
lineBreak: false
});
}

Graph search for element by element's name in jointJS

I have a problem in Rappid/jointJS
I have in stencil.js 4 shapes(2 basic.Circle and 2 basic.Rect) with names START(basic.Circle), END(basic.Circle), Activity(basic.Rect) and Workitem( basic.Rect) and I want in my main.js from all my graph to get the basic shape with name(I mean with attrs text ) "Activity".
This is the Stencil description for "Activity" :
new joint.shapes.basic.Rect({ size: { width: 5, height: 3 },
attrs: {
rect: {
rx: 2, ry: 2, width: 50, height: 30,
fill: '#0000FF'
},
text: { text: 'Activity', fill: '#ffffff', 'font-size': 10,
stroke: '#000000', 'stroke-width': 0 }
}
}),
How wil I get it? The only way I can search in my graph so far is if a cell has type basic.Circle(use of get('type') === 'basic.Circle')). but with type Circle I have two items:Activity and Workitem.
Is it so difficult to search for the graph element with name : "Activity"?
Thank you in advance
You can obtain all the elements (except for links) from following method
var allElement = graph.getElements()
Next if you want to obtain elements with 'Activity' do as follows
var activityElements = [];
allElement.forEach(elem => {
var textVal = elem.attributes.attrs.text.text;
if(textVal !== undefined && textVal === 'Activity') {
activityElements.push(elem);
}
});
Now the activityElements array will contain all the elements you require.
I solved my problem by taking element data in JSON format:
_.each(this.graph.getElements(), function(element) {
if(element.attributes.attrs["text"]["text"] == "Activity"){
//alert("YEAHHHHHH");
}
});
you could use the api on element as well, element.attr('text') returns the text object from the shape: { text: 'Activity', fill: '#ffffff', 'font-size': 10,
stroke: '#000000', 'stroke-width': 0 }
You could also set an "id" attribute to your shape and use graph.getCell('id_name_goes_here'); which would be much simpler if you didn't mind adding an id field to each shape.

How to evenly distribute the components by width in a Row?

I'm using Appcelerator Titanium and want to make an Android application. I created a TableView with 4 rows. I want to put 3 labels into each row and I want my labels to be evenly distributed. (1st label must be at the left, 2nd at the center, and 3rd at the right of the row.)
Thank you.
For left, center, right, you can use relative positioning and text alignment to very simply make your rows. This approach works well regardless of how wide the current screen is (ie, this works on tablet, phone, TV, etc).
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var rows = [];
for (var i = 0; i < 10; i++) {
var row = Ti.UI.createTableViewRow();
row.add(Ti.UI.createLabel({
text: 'Left ' + i, textAlign: 'left',
color: '#000',
left: 10
}));
row.add(Ti.UI.createLabel({
text: 'Center ' + i, textAlign: 'center',
color: '#000'
}));
row.add(Ti.UI.createLabel({
text: 'Right ' + i, textAlign: 'right',
color: '#000',
right: 10
}));
rows.push(row);
}
win.add(Ti.UI.createTableView({
data: rows
}));
win.open();
Another option would be to use percent widths, like left: '0%', width: '33%', then left: '33%', width: '33%', etc.
Or you could say the first label is from left: 0, width: 200. The second is left: 200, width: 50, and the third is from left: 250, right: 0. That would give you a third label that is elastic so it can take up al the space.
Yet another option (that I don't recommend you take) would be to use Ti.Platform.displayCaps.platformWidth and position the row elements based on that. But that would be very fragile to orientation changes.
It all depends on your content. With these in hand, you should be able to handle your particular use case.
Here is how I recently solved this issue. With 3 separate views and labels inside each row. This worked very well for me! All of them are based on percents, so it should work on all resolutions. Good luck!
for (var i = 0; i < 4; i++) {
var row = Ti.UI.createTableViewRow({
height: 'auto',
});
var view1 = Ti.UI.createView({
left : 0,
width : "33.33%",
backgroundColor:'red',
height:40
});
var label1 = Ti.UI.createLabel({
text: 'here',
color:"#fff",
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT
});
view1.add(label1);
var view2 = Ti.UI.createView({
left : "33.33%",
width : "33.33%",
backgroundColor : "white",
height:40
});
var label2 = Ti.UI.createLabel({
text: 'there',
color:"#fff"
});
view2.add(label2);
var view3 = Ti.UI.createView({
left : "66.66%",
width : "33.33%",
backgroundColor: "blue",
height:40
});
var label3 = Ti.UI.createLabel({
text: 'Everywhere',
color:"#fff"
});
view3.add(label3);
row.add(view1);
row.add(view2);
row.add(view3);
}
try this code.
var win = Ti.UI.createWindow({
});
var table = Ti.UI.createTableView({
backgroundColor:'blue',
top:50,
height : 160
});
win.add(table);
var data = [];
for (var i = 0; i < 4; i++) {
var row = Ti.UI.createTableViewRow({
height : 40
});
var label1 = Ti.UI.createLabel({
text : 'label1',
top : 5,
left : 10,
width : 80,
height : 30
});
row.add(label1);
var label2 = Ti.UI.createLabel({
text : 'label2',
top : 5,
left : 130,
width : 80,
height : 30
});
row.add(label2);
var label3 = Ti.UI.createLabel({
text : 'label3',
top : 0,
left : 240,
width : 80,
height : 30
});
row.add(label3);
data.push(row);
}
table.data = data;
win.open();
Best luck..

Resources