Making processing.js sketch get value from external JS variable - javascript

I have a game written in Javascript with the Processing.js library. I want to make highscores for that game, and for that I want the user to be able to type a name in an input tag in the page HTML.
How can I make my Processing.js sketch "see" the variable containing the value of the input field?

You should check out the Processing.js documentation, specifically the JavaScript Quick Start, specifically the Writing Documents that Combine Processing and JavaScript Code section.
There are three main ways to approach this: you can write Processing code that accesses an external JavaScript variable, or you could write JavaScript code that does something like call a setter function in your Processing code, or you could write JavaScript code directly in your Processing code.
Any of those approaches would work fine for what you're trying to do. If you still can't get it working, please post a MCVE of what you tried in a new question, and we'll go from there. Good luck.

Related

how can i use PHP functions (Gutenberg WordPress) inside JavaScript

sorry for asking a very basic question...
i am new at WordPress/Gutenberg, and I am not familiar with PHP. trying to write a custom block with javascript at Gutenberg, but I found most of the functions available only work at PHP. how I can use these functions in javascript ..
like .. get_post_meta() , or get_the_tags()
thank you
Yes, PHP functions can be used inside Gutenberg blocks by creating a dynamic block which calls a PHP function to render the content. This enables use of all the available PHP & WordPress functions like get_post_meta() etc.
The Developer documentation has an example block code that shows how this is implemented and is a great place to get started. Also, the <ServerSideRender> is a useful component that enables rendering of live PHP inside the block editor.
Reviewing the source code of an existing core block (eg. latest posts) that uses PHP to render may also help you get started with building your own block.
There is a workaround since PHP outputs text you can use it to write Javascript code that will be executed on the page load or triggered by an event. Just don't forget to surround your js code with script tags

How to have an action respond to javascript in PHP?

I'm currently working with a Kohana project and am trying to implement endless scrolling. I'm trying to use the method Ryan Bates shows in the following Ruby on Rails video:
https://youtu.be/PQX2fgB6y10?t=2m29s
At 3:21 he says the action won't respond to javascript and proceeds to create a js.erb file. What is the PHP equivalent to this step going forward? Where would I place the php file equivalent to his index.js.erb?
To copy what he did, just create a PHP file that generates Javascript with the content you want to append like he did with render(). You can call this file whatever you want, but following his convention, it would be index.js.php.
So for example:
$('#products').append('<?php render_elements(); ?>');
$('#pagination').replaceWith('<?php render_pagination(); ?>');
Since you're already this far in his tutorial, I assume that you have the code to render the elements you want to display already.
It may be easier however to use other AJAX methods to achieve the same thing though.

Retrieve file properies using Javascript

I want to build a javascript function that can look in specific file locations, and find if the file is there, and when it was put there. If it is no longer there, I want to see if I can get the time it was taken out (and when it was put in, if possible). Are there already written functions for doing this, or will I have to develop them myself?

How to access one JS file from a function inside a JS file

So, I am making a "ModReady" version of my JavaScript game where you can easily mod a game. The point is to click a button inside a ModReady version of a game, type the filepath of the mod you want to run, and the JavaScript executes it.
I have a problem though. I don't think you can run a separate .js file from inside a function.
Is this possible to do?
Depends on where the game is relative to the mod authors file. The short answer is yes, but you have to be a little cunning if the mods script doesn't exist in the same place as yours.
The safest and easiest method would be to offer a version of the code base which others can download and mod themselves.
Alternatively theres several methods of running code cross browser, i wouldn't advise letting other peoples code reside on your server.
The simplest method would be similar to the way code pen or JSfiddle work, you have a text box which the mod adds the code to. Then either run your game in an iframe with the text box contents included in a script element, or store the string in a HTML 5 local storage element, load the game page querying the local storage and add the local storage string to a new script element.

How/when/where to include external javascript

I'm looking for some advice on the best way to hold my JavaScript (jQuery) functions.
I am developing in MVC/razor and therefore have a layout page. I include my jQuery library and an external JavaScript file in here so it's available in every single page.
This is working well, but I am now becoming very aware of the fact that I am adding almost 300 lines of JS to EVERY page, where maybe half of that is used in any one of these pages.
One function is not in the external file and instead sits inside the HTML because I need to use variables set in my razor code.
I have a couple of questions around this arrangement:
Is placing JS inside the HTML generally acceptable when variables set using razor are used? There does not appear to be a clean way of passing a variable into an external js file
Should I split my functions down in to individual JS files and just include what is needed for each page in the site?
If I were to split them into multiple files, how would that work with jQuery's (document).ready ? Do I need to use that if all the JavaScript I am including is to be used?
I'm sure this will more a matter of opinion than a black and white answer, but I want to consider all my options before moving on. Even though it works fine as is, I can't help but feel there is a better/cleaner way.
Remember once a user lands on your homepage and loads the javascript file it will be cached in their browser so subsequent pages will not download the Javascript again.
I would definitely keep the js separate, you could have a snippet on each page that initialise the JS that that particurlar view needs. Put something like the below in the views that need to run JS
$(document).ready(function() {
mysite.mypage();
});
Then the function mysite.mypage() can be defined in the external JS file.
300 lines isnt the end of the world, I would say its probably too early to be worryign about optimisation.
You could always look at minifying that JS file to decrease the size. A quick and easy way to do this is here:
http://www.minifyjavascript.com/
Have you ever heard of require.js? http://requirejs.org/ I find it really useful.
It's a module loader so you are able to split all of your JS code into individual files and load only the ones you need on each page.
I don't know about passing a variable to an external JS file, I don't think its possible / the 'right' way.
You can make each external JS file into a function that accepts and returns parameters. Then in the page you need to use it:
- include the file dependancy
- call the function
Thats what I do, seems like your 2nd suggestion.
for the $(document.ready) question its really up to you. You don't have to use it but its useful for some things , check out this overview:
http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

Categories

Resources