Change font size of variable within After Effects Expression - javascript

I am trying to use an expression in After Effects to combine a number and a unit of measurement, but I want to also set the font size of the unit of measurement within the expression. However, I don't want to create another layer for this. Is there a way to set the font size within the expression?
Expression:
var amount = thisComp.layer("Number Control").effect("Number Control")("Slider");
var unit = thisComp.layer("Unit of Measurement").text.sourceText;
var unitSize = thisComp.layer("Unit of Measurement").text.fontSize;
unit.style.setFontSize(20);
Math.floor(amount) + unit;

Related

How to truncate String with Ellipsis in svg

I am using the d3 library to create Bar Charts. The values on the axes are strings (for example, some names). I have the ability to drag the axis to change the size of the graphs. Resizing the graph causes a recalculation of the line that fit in the previous empty space. Since I'm using d3 (that's working with svg), I can't use the css property to add '...' if the string doesn't fit, I have to do it manually. The function I am using is given below:
protected truncateLabel(
truncatedLabel: Selection<SVGTextElement, unknown, null, undefined>,
textToFit: string,
freeSpacePx: number,
) {
truncatedLabel.node().textContent = textToFit;
const averageWidthOfCharInPx = truncatedLabel.node().textLength.baseVal.value / textToFit.length;
if (truncatedLabel.node().textLength.baseVal.value <= freeSpacePx){
return truncatedLabel;
}
const numberOfCharsThatWillFit = freeSpacePx / averageWidthOfCharInPx;
truncatedLabel.node().textContent = textToFit.slice(0, numberOfCharsThatWillFit - 2) + '...';
return truncatedLabel;
}
Everything works well if there are few bars, but if there are many bars, resizing starts to slow down a lot because of this function.
Is there a better way to check if a string fits and add '...'?
Or any way to optimize my function?

variable not working in p5.js It uses a different variable to the one I had entered in an if loop

Sorry bout this but i'm using p5.js for this. I have a variable named pixel line(3) and var size(10) on line 51 in the if statement it uses var size instead of var pixel please help. The majority of the code is irrelevant for this function.
Using the pixel var to be defined by size but no matter what i try it only registers size.
https://editor.p5js.org/Predat0rHeli0J/sketches/zQ6ONgkoW

Rollercoaster simulation in matlab/javascript

I'm having problem with a project that I'm working on in matlab which i'm then going to implement using javascript. The purpose is to use matlab to get a better understanding of the physics before moving over to javascript. The goal is to create some sort of a Rollercoaster simulation in matlab using differential equations and euler aproximation. Then to animate a block(cart) following the path.
The problem is that I can't get the approximation working on a arbitary path (equation), and I don't understand what I'm doing wrong.
So this is what i'm trying to do:
for t=1:h:tf
%acceleration
ax(i) = (g*cos((-pi/4)))*sin(-pi/4)-((C*ro*A*vx(i)^2*sign(vx(i)))/(2*m))*sin(-pi/4);
ay(i) = (g*cos((-pi/4)))*cos(-pi/4)+((C*ro*A*vy(i)^2*sign(vy(i)))/(2*m))*cos(-pi/4);
%speed
vx(i+1) = vx(i)+h*ax(i);
vy(i+1) = vy(i)+h*ay(i);
%position
x(i+1) = x(i)+h*vx(i);
y(i+1) = y(i)+h*vy(i);
i = i+1;
speed = sqrt(vx(i)^2+vy(i)^2);
plot(x(i),y(i),'or')
pause(1/speed)
end
Where i'm following Newtons second law (F=ma => a = F/m) the only negative force component i'm using now is air resistance.
This is when i'm using a hardcoded path -> y=1-x, and it's working just fine!
But when i try to use an arbitary path, eg. 1/x, the angle is changed all the time, and i've tried things like putting each angle into an angle vector:
%% initial values
vx(1) = 0;
vy(1) = 0;
x(1) = 0;
y(1) = 6.7056;
%Track Geometry Constants
h_start = 6.7056;
l_end = 32;
b = .055;
w = .7;
p = .3;
%% Creating path
path_x = linspace(0, 23, 1000);
path_y = (exp(-b*path_x).*(cos(w*path_x - p)) + 2.2*(exp(-(b)*path_x)));
path_y = path_y*(h_start/max(path_y));
path_x = path_x*(l_end/max(path_x));
%%
alpha = zeros(size(path_y));
for k=1:1:size(path_y)-1
alpha(j) = atan(path_y(k+1)-path_y(k))/(path_x(k+1)-path_x(k));
j= j+1;
end
But this doesn't appear to work.
How can I make this work for an arbitrary path?
Thank you in advance!
There is a pretty simple error in your loop. Two errors, actually.
You are indexing alpha with a variable, j, that doesn't exist and increment it in the loop, instead of just using k, which increments automatically in the loop.
The reason this isn't giving an error is that your loop never runs. Because the size of path_y is not a single number, (the size is 1 x 1000), k=1:1:size(path_y)-1 tries to create a loop that goes from 1 to 0 in steps of positive 1. Since this isn't possible, the loop is skipped. One option is to use length, not size here.
But the most important error: you didn't check your code line by line when it stopped working to confirm that every part of your code was doing what you thought it was when you wrote it. If you'd checked what k=1:1:size(path_y)-1 was outputting, you should have been able to identify this problem very quickly.
Incidentally I think you can avoid the loop entirely (append a zero at the end if you really need this to have the same size as the path variables):
alpha = atan(diff(path_y)./diff(path_x));

Is it possible to convert A string that is an equation with a variable into a equation?

I need to convert a string returned from prompt into an equation, however the parseFloat returns as only the first number, and symbols in an equation, and stops at the variable. The variable will always = x. The program is designed to convert an algebraic expression say 15*x(5^4-56)*17/x=15 into an expression, and calculate the value of x. If someone could show me how to do this, it would help dramatically. I am currently using multiple prompts, having the user put in the equation before x, then the equation after x, then it inserts a variable in between the two, and calculates it's value.
Edit:
I have no variables predefined, and it must work in equations where x > 1000, or x != //an integer.
Thanks in advance!
Seems to be a complex problem...
This is a solution for a simple relaxed version of your problem. Hope you can use some components of this.
Constraints:
answer for x should be integers between 0 and 1000
the left hand side of the expression should be proper javascript syntax
var input = prompt("enter the equation"); //eg: x*x+x+1=241
var parts = input.split('=');
//solving equation starts
var x = 0;
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){
x++;
temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this";
//solving equation finishes
alert(ans);
You can replace the "solving equation" part with some numerical methods used in computer science to solve equations (more details here) . You will have to parse the left side of equation and map them to proper javascript expressions (as a string to execute with eval()) if you want to allow users to use your syntax.
Javascript can evaluate strings using the eval function, but the variable as to be defined before hand, and the equation has to be formatted in way that javascript can understand:
var x = 15
var string = "15*x*17/x"
eval(string)
Your example: "15*x(5^4-56)*17/x=15" would not run however, because it would evaluate x(5^4-56) as a javascript expression, which is invalid.
Using all the info, and other mehtods I found about this, I have put together a communinty answer. Anyone is invited to change and/or add their methods to this.
In order to do this with the least work possible for the user and coder, you would implement the following code.
var input = prompt("enter the equation"); //eg: x*x+x+1=241
var parts = input.split('=');
//solving equation starts
var x = 0; //Or the lowest possible value of "x"
var temp = eval(parts[0]);
while (temp != parts[1] && x<1000){ // && x < The highest number to evaluate
x++; //Add the increment (determines the maximum amount of digits) eg x+0.1 for tenths max, x+2 for only even integers etc.
temp = eval(parts[0]);
}
var ans = (x<1000)?"answer is "+x:"this program cannot solve this"; //make sure x< is the same as line 7.
//solving equation finishes
alert(ans);
But, this runs very slowly if you allow tenths, or a range larger than 2000.`
A faster way of running this would be to define arrays allowing any variable (instead of just x) and a different eveulation process such as here. (do the right click view html and click on the first js source to see code) but, this is 2k lines. Both are usable, but the second is more efficient, and can solve multivariate equations.

trying to make gradient using boxes

I need to know how can I specify some variable parameters to my background color??
I'm trying to use JavaScript and i want to make this shape :
I know the parameters,
My approach was to take the initial division, append a child division to it with smaller size and different background color..
However, for this to work i needed to use this : box[i].style.backgroundColor = "rgb(255-10*i, 255-10*i, 255-10*i)"
Which didnt work, And i was told that the rgb only takes integers parameters, so i tried to do this:
color = 255-10*i;
box[i].style.backgroundColor = "rgb(color,color,color)";
which led to the same error.. Any idea how to proceed?
Note: 1- I'm using that code in a for loop to create say 20 nested division with different color and size
2- I'm obliged to do it using javascript with no manipulation with neither html nor css files...
3- I need it as fast as possible( Studying for the exam )
Any help is appreciated..
you need to concatenate the string with the value of color. right now you're just making a string with the word color inside it:
color = 255-10*i;
box[i].style.backgroundColor = "rgb(" + color + "," + color + "," + color + ")";
You are passing the variables as strings
box[i].style.backgroundColor = "rgb(color,color,color)";
Should be
box[i].style.backgroundColor = "rgb("+color+","+color+","+color+")";

Categories

Resources