p5.js blank screen while running the code - p5.js

js user then I explored p5.js and created my first code "hello new world" the screen was blank I was sure it was p5.js's fault but still I tried different code but still was blank screen when runed the code then I copied the library code still it was blank can you tell me why it was happening.

Uhh, could you provide some code and much more importantly your html code, since you need to include the library.
You also need to create the canvas and give it a background color, if an error occurs before p5.js is a thing (if you use a p5.js variable/function in the global scope) it also won't draw the canvas...

Your code was
function draw() {
text("helo new world" 20, 20, 300, 300);
}
It was entirely wrong, it should actually be
function setup() {
createCanvas(400, 400);
}
function draw() {
text("hello new world", 20, 20);
}
Here first you forgot the comma , after the string ( a string is a piece of text surrounded by " " or ' ' )
Second, there should only be two numbers with one string at first, in total three arguments ( arguments are the values in ( ) in calling function, i.e. writing text(); like this is called calling text function, which are separated by commas, i.e. text(argument1, argument2, argument3);
The first argument is the text, which you wrote as "hello new world" then a comma next, the second argument is the x position on the screen, which is the number 20, then a comma, next is the third argument, the y position on the screen, which is again here the number 20, no need of fourth and fifth arguments as there are no extra optional arguments!
You can have a reference of etxt function from here

Related

Using random in JSP5

Does someone know how I can fix this problem?
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[Math.floor(Math.random() * Arguments.length)]
Because of these variables, a random argument is always chosen at the beginning. However, if you are in the program and switch from page to page, it will keep the random value it had at the beginning. I want him to be constantly creating new values.
In the draw function I call the argument like this:
text(Argument, 300,180);
If I understand your question and the comments correctly, you need re-assign Argument to be a different random value each time you switch pages, such as this line every time it switchs:
Argument = Arguments[Math.floor(Math.random() * Arguments.length)];
You don't put var there every time. Give it a value in setup(), then re-assign it when you need another value.
Unless, of course, I completely missed your problem.
In p5.js there is a helpful function called random
so you could use it like this
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[random(0, Arguments.length - 1)]
you can read more about it here
Make argument into a function.
var Arguments [2003,1002,3932,10203,2030,1828912,12912];
var Argument = function(){
return Arguments[ Math.floor( Math.random() * Arguments.length ) ];
}
text(Argument(), 300,180);
If you make a random selection in setup() it will only happen once in your whole script. Also, Don't choose variable names that might already be used by the system (I suspect "arguments" might be.
let args = [2003,1002,3932,10203,2030,1828912,12912];
let a;
function setup() {
createCanvas(400, 400);
a = random(args);
}
function draw() {
background(220);
textSize(30);
text(a,50,70);
}
function mousePressed() {
a = random(args);
}

How to forcefully end an iteration of a for-loop (without stopping the for-loop)?

This may be a stupid question, but here goes. Is there a way to forcefully end an iteration of a for-loop and ignore all other conditional statements within the loop, and move onto the next iteration?
I'm trying to make a tool where whenever a user types a letter, it's printed to the canvas.
I'm using a for loop to do this, with each iteration making the next letter move to the right each time.
Inside the for-loop includes the conditional statements for typing the letter:
function draw(){
}
function keyPressed(){
for(i=0; i<100; i++){
if(keyCode == 65){
text("a", 60 + i*10, 60)
}
...
...
...
}
}
and so on, and so forth. However, this would only work if after each letter it moved onto the next iteration (otherwise the letters would be printed in the same place due to 'i' not increasing, making it unreadable).
Using 'return' at the end of each condition statement doesn't work for me as 'return' ends the entire for-loop, whereas I just want to end that specific iteration.
All help is appreciated, thank you.
It sounds like you're looking for the continue keyword. Here's an example from W3Schools:
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
Here is a linke to try the code yourself.
But taking a step back, this feels like a flawed design. If you want to get the key they user pressed, you don't have to use a bunch of if statements. You can just use the key variable. From the P5.js reference:
function setup() {
fill(245, 123, 158);
textSize(50);
}
function draw() {
background(200);
text(key, 33, 65); // Display last key pressed.
}
Even if that won't work for some reason (if you want to display something for the arrow keys, for example), there are probably better ways to solve this problem than a bunch of if statements in a for loop. For example you could create a mapping from keyCode to the string you want to display, and then call that mapping. Something like:
var m = new Map()
m.set(65, 'a');
m.set(66, 'b');
//..
function keyPressed(){
text(m.get(keyCode), 50, 50);
}

mousePressed method is launching prematurely in p5js

I created the following function using p5.js and the mousePressed method is firing immediately when the page loads. It doesn't wait for me to click the button object to display a paragraph containing the ans variable.
What am I doing wrong?
function setup() {
var ans = generate();
var checkMe = createButton('Check Answer');
checkMe.mousePressed(createP(ans));
}
Let's take a closer look at this line:
checkMe.mousePressed(createP(ans));
This could be split into two lines:
var createPValue = createP(ans);
checkMe.mousePressed(createPValue);
In other words, you're calling the createP() function, and then passing the value returned (which is probably undefined) into the mousePressed() function. I'm surprised this doesn't cause an error in the JavaScript console.
Instead, what you want to do is pass a function as a value into the mousePressed() function. Since you need to use a parameter, you might do that this way:
function callCreateP(){
createP(ans);
}
checkMe.mousePressed(callCreateP);
Notice that callCreateP doesn't have parentheses () after its name when we pass it into the mousePressed() function. That's because we're using it as a value instead of directly calling it.
You could shorten that to this line:
checkMe.mousePressed(function(){ createP(ans); });

How do I get the line of text under the cursor in a TextView in gtk#?

I have a GTK# TextView and I want to read the line of text under the cursor. I don't see a single method that will do that, so I think I need to combine several method calls, like Buffer.GetText, Buffer.GetIterAtOffset, Buffer.CursorPosition, but it's not obvious to me what the right combination is.
TextIter are a bit odd to use. Buffer.CursorPosition gives you the current position.
It's easy to find the end of the line:
var end = Buffer.CursorPosition;
end.ForwardToLineEnd();
To get the first character, there's not symetrical method, so you might try:
var start = Buffer.CursorPosition;
start.BackwardChars(start.LineOffset); // LineOffset gives you the iter offset on the current line.

ZPL - zebra: print justified text block without overwriting last line

I'm using the following command to print a justified text:
^FB1800,3,0,J^FT100,200^A0B,26,26^FH\^FDLONG TEXT TO BE PRINTED, WHICH DOESNT FIT IN ONLY 3 LINES...^FS
The command ^FB1800,3,0,J prints a field block in a width of 1800 dots, maximum 3 lines, justified.
The problem is that if the text exceeds the maximum number of lines, it overwrites the last line! :( That of course makes the text of the last line unreadable.
How can I avoid that? Does anybody know if is there a way to cut the exceeding text?
The documentation says exactly that this happens:
Text exceeding the maximum number of lines overwrites the last line. Changing the font size automatically increases or decreases the size of the block.
For reference: I'm using printer Zebra 220Xi4.
Any help would be appreciated. Thank you!
Take a look at the ^TB command. It is preferred over the ^FB command and truncates if the text exceeds the size defined in the TB params
I had just about the same problem, what fixed it in my case - although not the most elegant way - is to specify a higher number of maximum lines, and then formatting it in a way that only the first 3 are in the visible area.
In your case it would be for example ^FB1800,7,0,J instead of ^FB1800,3,0,J
This at least fixed it for me right away, because I print this text at the bottom of the label. If you need to have it somewhere in the middle or top, there might be some tricks with putting a (white) box on top of the overflow-area, since the Zebra printers seem to render before printing. Hope it helps.
Depending on the higher-level programming language you're using (assuming that you are), you could accomplish the same thing (truncate the text to be printed to a specified number of characters) with code like this (C# shown here):
public void PrintLabel(string price, string description, string barcode)
{
const int MAX_CAPS_DESC_LEN = 21;
const int MAX_LOWERCASE_DESC_LEN = 32;
try
{
bool descAllUpper = HHSUtils.IsAllUpper(description);
if (descAllUpper)
{
if (description.Length > MAX_CAPS_DESC_LEN)
{
description = description.Substring(0, MAX_CAPS_DESC_LEN);
}
}
else // not all upper
{
if (description.Length > MAX_LOWERCASE_DESC_LEN)
{
description = description.Substring(0, MAX_LOWERCASE_DESC_LEN);
}
}
. . .
This is what I'm using; is there any reason to prefer the "raw" ^TB command over this?

Resources