I'd like to teach students how to program using JavaScript. I don't want to introduce new students to call-backs or any other complex program structure. Looking at Node.js the readline used for standard input uses a call-back. For simple input data, then do a calculation, I'd like a simple equivalent to an input like Python or other similar languages have:
width = input("Width? ")
height = input("Height? ")
area = width * height
print("Area is",area)
Is there some way to do this with JavaScript?
The module readline-sync, (source can be found here, npm page here) will do what you want, it looks like.
If you'd prefer to work at a lower level, it looks like it works by passing the stdin file descriptor (stdin.fd) to the synchronous fs methods. For example:
fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)
There is sget as well, a simpler and bit saner module I wrote that accomplishes what the OP is asking for.
var sget = require('./sget');
var width = sget('Width?'),
height = sget('Height?'),
area = width * height;
console.log('Area is', area);
Related
I am trying to sharpen an image using a sharpening convolution kernel like below:
[[0,-1,0], [-1,5,-1], [0,-1,0]];
Is it possible to define arbitrary matrices like this?
I can do this simply in OpenCV for python by declaring a numpy array like above, but in JavaScript (OpenCV.js) it seems there is no way to do this without using the Mat method and some combination of other methods.
I have tried like this:
let kdata = [[-1,-1,-1], [-1,9,-1], [-1,-1,-1]];
let M = new cv.Mat(kdata);
and by following the example here:
https://docs.opencv.org/master/dd/d6a/tutorial_js_filtering.html
But I can't find anything that allows me to create arbitrary matrices where I have control over each element in the matrix, only methods like eye() or ones() that fill the matrix with some pre-specified values.
Any help in creating arbitrary matrices that can be used with the filter2d method would be very helpful. Thank you!
Ok I just spent the whole day trying to find the solution for this.
let kdata = [-1,-1,-1,-1,9,-1,-1,-1,-1] ;
let M = cv.matFromArray(3,3, cv.CV_32FC1,kdata);
I know this is a very old question but I hope this awnser is helpfull if anyone comes across this in the future.
I have a 3d object which i want to "move" from A to B via script. I am not too sure how to go about it; I don't understand the Facebook documents. Just a short example as a start would be great.
I assume something along the lines:
var object = Scene.root.find("object");
var lastPosX = object.transform.positionX.lastValue;
object.transform.positionX = //NOT SURE HOW TO PUT THE NEW POSITION
What you need to do is use the AnimationModule - here is a simple example of how to do that:
const Animation = require('Animation');
var obj = Scene.root.find("object");
//set up the length of the animations, 1000 = 1 second
var driver = Animation.timeDriver({durationMilliseconds: 1000});
//define the starting and ending values (start at 0, go to 100)
var sampler = Animation.samplers.linear(0, 100);
//create an animation signal to control the object x position
obj.transform.x = Animation.animate(driver, sampler);
//start the animation
driver.start();
Animation in ARS, like many other things, is based around the concept of "Reactive Programming" and working with "Signals" which are values that change over time. It is essential to get a good grasp of what a signal is and how it works to write useful code in ARS. Read through this for an introductory overview: https://developers.facebook.com/docs/ar-studio/scripting/basics
The above is a very basic example, but there is much more interesting, advanced, and complex effects that you can achieve using the AnimationModule, take a look at the documentation here for more information: https://developers.facebook.com/docs/ar-studio/reference/classes/animationmodule/
Hope this helps!
What I want to do is to get an image diff using node.js.
Ultimately I want to have a method expecting e.g. two filepaths or image data outputting the subtraction of both. Somehow like the compare function in imagemagick.
Something like:
var comparison_result = compareModule.compare('./image1.png', './image2.png');
Also, I would like to get the position of the spots in the resulting image that mark the differences.
Like this:
comparison_result.forEach(function(difference) {
console.log("A difference occurred at " + difference.x + "|" + difference.y);
});
I installed node-opencv, however I can not find a documentation that maps basic opencv c++ functions to node.js.
The function I would like to use is cvSub.
I would like to avoid js-imagediff as it works with canvas, has a dependency to "cairo" and I am not sure whether I can access the spots because in the documentation it rather seems like it just returns the difference as an image.
I have never tried to calculate per-element difference by cv::addWeighted() but it may work in practice:
var diff = new cv.Matrix(first.width(), first.height());
diff.addWeighted(first, 1.0, second, -1.0);
In native code (C++), this function can be replaced with the expression below:
diff = first*1.0 + second*(-1.0) + 0.0;
p.s.: node-opencv's authors published a sample code for measuring similarity:
node-opencv / examples / dissimilarity.js
This obviuosly works perfectly:<script>alert(5*8-4)</script>
but i need to solve whatever someone puts inside an input box.
Heres what I thought of doing: I would get the value of the input, into a variable. then I would use
document.write("<script>alert("+theinputvalue+")<script>");
or do this:
var string="<script>alert("+theinputvalue+")<script>";document.write(string);
but nothing works.
Is it even possible to do this? if not, tell my what simple other system I could use.
eventually, I will use it to graph lines like this:
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d")
for(var x=-100; x<100; x=x+.2){
y = .1*(x*x)
ctx.fillRect(x+50, -1*y+50, 2, 2);
}
http://jsfiddle.net/KGgq4/
eval('5*8-4')
will result in 36
I'm not aware of any library that is doing that (this doesn't mean that there are no such it simply means I never actually needed that) but what you should end up doing is to build an automata that will parse input string and transform it to a proper graph with proper transformations. This is not very easy topic and if you want to go this route you should start reading on arithmetic expressions parsing algorithms (sorry I do not have any solution in place).
Or you can cheat and define types of equations that will be selected by user. Once user selects type of equation you should be able show user inputs where user will be able to select coefficients. You can read those coefficients into different variables and apply transformations in your draw procedure (For example if user will select type sin(x) you know that general equation has following formula: y = k*sin(a*x + b) + c. So once it is selected you can allow user to enter k, a, b, c and based on that input calculate appropriate locations of points for your graph.)
Well, third solution could involve "eval ", but usually you should avoid eval at any cost (B/c it is straight forward JavaScript injection which may be an OK for this case but may get you in trouble later in your life. ).
You can use math.js, which comes with an advanced expression parser. It supports definition of variables and functions.
// create an instance of math.js
var math = mathjs();
// evaluate an expression
math.eval('5*8-4'); // 36
// user defined function (returns a native JavaScript function)
var f = math.eval('f(x) = 2*x^2 + 6');
// use the function (for graphing or something)
f(2); // 14
I'm working with a database that has X and Y points per group, it's being used to draw outlines of images.
Right now in my web side this code is what I use to get the points:
var Drawing = $(XML).find('DrawingXML');
alert($(Drawing[1]).text());
Result:
<DrawingPoints>
<Point><X>1</X><Y>2</Y></Point>
<Point><X>2</X><Y>4</Y></Point>
<Point><X>3</X><Y>5</Y></Point>
<Point><X>2</X><Y>2</Y></Point>
<Point><X>0</X><Y>4</Y></Point>
</DrawingPoints>
Using the .replace() call only changes one item so it's usable for something like this:
.replace("</DrawingPoints>","");
but if I want to replace all 'Point' tags I'm out of luck.
My goal is to use the canvas feature to draw the points out so I want it to be parsed like this:
ctx.beginPath();
ctx.moveTo(1,2);
ctx.lineTo(2,4);
ctx.lineTo(3,5);
ctx.lineTo(2,2);
ctx.lineTo(0,4);
ctx.stroke();
I'm not going to use this with IE browsers just Safari/Chrome, if that helps out.
In this case you'll probably save an awful lot of brainache by using a library instead of writing your own code.
I reckon d3 does what you need:
d3.xml
d3.geo.path
Check out this question/answer. It's not Prototype specific and should help you here.
How to parse XML string with Prototype?
Get all your X and Y values at once:
var points = {};
points.X = Array();
points.Y = Array();
var ix = 0;
$(XML).find('DrawingXML DrawingPoints Point X').each(function()
{
points.X[ix++] = $(this).text();
});
$(XML).find('DrawingXML DrawingPoints Point Y').each(function()
{
points.Y[ix++] = $(this).text();
});
This might not be exact, I didn't test it and my Javascript is a bit rusty, but you get the idea.