random image coding js - node.js

so i made a bot for discord and i want it to show a random image when i type g!guess. so i made it work with
if (message.content === 'g!guess') {
number = 2;
imageNumber = Math.floor (Math.random() * (number - 1 + 1)) + 1;
message.channel.send ( {files: ["./images/" + imageNumber + ".png"]} )
}
it shows a random image, i actually have only 2 images named 1.png and 2.png.
I'd like to make that when a random image is shows if i type its name i get a message, like i have 2 images a space image and a dog, if the dog image appear and i write 'dog' i get a message, and if i type something else nothing happen, if i write g!guess and the space image appear, if i write 'space' i get the message. I'd like it to show an image and that i have like 20 second to guess the name of it, and when the time finish it show a message like 'Time's out'. I hope its understandable.

If I understand you correctly, you're trying to inform the code that 1.png is a dog image whereas 2.png is a space image. It would be easier to guide you with a copy of the code but here's a high-level walkthrough:
I would go about this by adding an alt tag to each image and reference it in the javascript function.
for example:
<img src="src/to/img.png" alt="dog"/>
Here's how to reference the alt tag using javascript: Finding An image tag using the alt text
hth.

Related

How could I build a resizizing or collapsing Textbox in D3.JS for mitch-tree

Hallo I am currently trying to build a tree in D3.js.
For this I donwloadet the repository of the mitch-tree:
https://github.com/deltoss/d3-mitch-tree
I wantet to ask, if maybe somebody already has expirience with this issue?
So, I build a tree with processcharts.
But I have the problem, that the text hast to fit the boxes right nor, or it will get cut out.
It would be great to be able to fit the boxes to the content, so making them resizeable or, maybe make the boxes collapsable..
Maybe somebody hast a tipp for me, how to reach that goal.
I think I found the part of the code where the cutting/ resizing might be happaning for my textBox.
But I am really not sure..
If I got it right, the textbox resizes already what is inside with this function and cuts the words, that are to much for my box are out:
// D3Plus Textbox with resizing capability
var d3PlusBodyTextBox = new d3PlusTextBox()
.select(element) // Sets the D3Plus code to append to the specified DOM element.
.data(singledOutData)
.text((data, index, arr) => {
return self.getBodyDisplayText.call(self, data);
})
.textAnchor("middle")
.verticalAlign("middle")
.fontSize(13) // in pixels
.x(nodeBodyBoxPadding.left)
.y(recalculatedPaddingTop - nodeBodyBoxHeight / 2)
.width(nodeBodyBoxWidth - nodeBodyBoxPadding.left - nodeBodyBoxPadding.right)
.height(nodeBodyBoxHeight - recalculatedPaddingTop - nodeBodyBoxPadding.bottom)
.ellipsis((text, line) => {
// If text was cut-off, add tooltip
selection.append("title")
.text(self.getBodyDisplayText(data));
return ((text.replace(/\.|,$/g, "")) + "...");
})
.render();
});
Has anyone maybe a tipp for me how to change it in a good and functional way?
Thanks in advance :)

(npm package) gm only edits the last frame of GIF

I'm trying to overlay text on top of a GIF using the gm package. It does overlay the text on top of regular images, however on GIFs it just overlays the last 2 frames. I don't have much experience using this package and Imagemagick.
Here's the code that edits the image.
gm(request(image)) //any image url
.command('convert')
.coalesce()
.font("./impact.ttf")
.dither(false)
.fontSize(Math.round(width/11)) //Width is defined earlier in the code
.stroke("#000000")
.strokeWidth(width/450)
.out('-gravity', 'north')
.out('-size',width+'x','-fill','white','-background', 'transparent', '0')
.out('caption:' + meme[0].toUpperCase().substr(meme[0].indexOf(" ")+1))
.out('-layers','optimize')
.out('-gravity', 'south')
.out('-size',width+'x','caption:' + meme[1].toUpperCase())
.out('-layers','optimize')
.strip()
// If i use these two, it does append all the layers but the text does not wrap like it does using caption
// .drawText(0,1, meme[0].toUpperCase().substr(meme[0].indexOf(" ") + 1), 'North')
// .drawText(0,0, meme[1].toUpperCase(), 'South')
.stream((error, stdout) => {...}
Result of the code
Any help would be appreciated!

Using rotate for images leads to blank pdf with pdfkit

I have asked the question on the repository directly, but in my experience SO is more reactive.
Hey there,
I am trying to create a pdf from photos using pdfkit.
Depending on whether an image is in landscape or portait mode, I want to turn the image around.
This basically means the following (in typescript) :
function toPostscriptPoint(mm: number) {
return mm * 2.8346456693;
}
const document = new PDFDocument({
size: [toPostscriptPoint(156), toPostscriptPoint(106)],
});
document.pipe(fs.createWriteStream('output.pdf'));
document.save();
document.rotate(90);
document.image(
'photos/sample.jpeg',
{ width: toPostscriptPoint(150), fit: [toPostscriptPoint(150), toPostscriptPoint(100)] });
document.restore();
document.end();
What happens though is that the pdf renders completely white. I do see however that something is happening, because the pdf has the size of the input image.
Is rotation for images not supported? What would be possible alternatives? I would like to avoid having to rewrite my files before putting them in the pdf.
Thanks
Alright, after investigation, I can answer my own question :).
I could see that the images were in the pdf somehow because of the size of the file so I dived deeper.
What happened was that the image was rendered out of the viewport. This was due to multiple things:
By default, the origin of a page after rotation in pdfkit is the center of the page! ( See the doc for more info)
The origin is rotated together with the transformation.
The x and y in the image method are actually inverted.
So after getting all this right, the following code shows the image as expected :
function toPostscriptPoint(mm: number) {
return mm * 2.8346456693;
}
const document = new PDFDocument({
size: [toPostscriptPoint(156), toPostscriptPoint(106)],
});
document.pipe(fs.createWriteStream('output.pdf'));
document.save();
document.rotate(90, {origin : [0, 0]});
document.image(
'photos/sample.jpeg',
toPostscriptPoint(0),
toPostscriptPoint(-150),
{ width: toPostscriptPoint(150), height: toPostscriptPoint(100) });
document.restore();
document.end();
Note the :
origin argument in the rotation
toPostscriptPoint(-150) actually takes into account the position of the origin, and corresponds to the X axis.
Hope that helps some later on :).
Its because taking picture from camera , if picture ISO is less than 100 it will be automatically rotated , take picture with iso more than or equal to 100 to avoid autorotation.

Raphael - Transform after Serialization

I am using Raphael to draw some paths. Each path has an associated rectangle [container] the size and position of the bounding box. I am using the container for dragging both shapes.
In the move callback, I update the both positions so they both move together.
This all works great until I serialize. I am only serializing the path, then creating the container on the fly after deserialization.
Immediately after converting to json and back, things look fine. I can print out the current transform of the path and it looks correct. Doing any transform on the path after this results in the path being reset and moved to 0,0.
Here is a fiddle that shows the problem.
If you move the rect, you can see both objects move together.
If you click 'Save/Load', things look fine, and the path prints the same.
If you now drag, the path gets reset to 0,0. Printing shows the transform has been reset from 0,0.
I am trying to find out how to make the path move as it did before serialization. Is something getting lost in the process? Or is there an internal state that needs to be updated?
Raphael.JSON serialises data stored in the elements. It does not preserve temporary data stored in the paper object so something does indeed get lost in the process when calling R.clear(). For example drag events bound to elements are not preserved.
However the main issue here is with your drag function, notice how dragging the square a second time applies the transformation from the top left of the paper. I suggest using Raphael.FreeTransform (which you already included in the Fiddle) to handle this.
I wrote both Raphael.JSON and Raphael.FreeTransform plugins and have struggled with the same issues. I'm currently working on an application that lets you save save and restore the state of the paper (similar to what you're doing) and it works fine. If you need any help feel free to open an issue on Github.
You need to capture the initial transform offsets of your elements when the drag starts and use those as the basis for your drag-move transforms. Consider the following:
var start_x, start_y;
cont.drag(function(x, y, e)
{
p.transform('t' + ( start_x + x ) + ',' + ( start_y + y ) );
cont.transform('t' + ( start_x + x ) + ',' + ( start_y + y ) );
},
function( x, y )
{
var start_bbox = p.getBBox();
start_x = start_bbox.x;
start_y = start_bbox.y;
console.log("Drag start at %s,%s", start_x, start_y );
} );
I've staged this in a fiddle located here.
Unfortunately, there is still an issue with the path -- it's offset is being incremented by the difference between it's bounding box y value and the y axis (a difference of 12, to be precise) each time drag is used. I'm not sure where that's coming from exactly.

How can I achieve this kind of List in BlackBerry?

I am making my first application for BlackBerry (OS 6.0+) and I have to define a screen like in the image below. I have read about RichList but its not allowed to be added below the other components.
What I want is the part (list) below the wide ButtonField. The white boxes should contain images and the scribbles in front of it should contain text. This "list items" are populated dynamically.
As you can see, this very-much resembles the layout of a RichList, but I can't seem to add it to the VerticalFieldManager that I have used as the main container of components.
So if anyone can help me get this screen, I'm really grateful! Thanks in advance.
You need to implement ListFieldCallback and overwrite all methods. There you will see "drawListRow" method. You need to draw Images on left and text on write on this method index wise. like this:-
ListData ld = (ListData) rows.elementAt(index);
g.drawText(ld.name, 30+extraSpaceDelete, y
+ (listItemHeight - g.getFont().getHeight()) / 2);
g.drawText(ld.address, 30+extraSpaceDelete, (y
+ (listItemHeight - g.getFont().getHeight()) / 2)+(g.getFont().getAscent()+10));
g.drawBitmap(0, y + listItemH - 3, Display.getWidth(), 3,
separator_line, 0, 0);

Resources