AE JavaScript finding Layer Object when none is given - javascript

Attempting to create a 3D shape with children that rotates at an accelerating pace from 0 to 3 seconds, I used the following script
if (time < 3)
Math.pow(time, 2)*30;
That gave me the following error
After Effects warning: Object of type Layer found where a Number,
Array, or Property is needed
Expression disabled.
Error occured at line 0.
Comp: 'Main'
Layer: 15 ('Blue')
Property: 'Y Rotation'
Comp name, layer name and property name are all valid. They point to the property I was trying to edit.
However, what puzzles me is that I fixed that by using the following code.
ctime = time;
if (ctime < 3)
Math.pow(ctime, 2)*30;
The code is now working as intended, and I have no idea why.

If the condition is false, the two would be different since there would be no last statement to use.Try: (time < 3)?Math.pow(time, 2)*30:time;

Math.pow(time^2*30); is simply bad code. Math.pow expects two arguments and you only gave it one. Also ^ is a bitwise operator, there is no power raising operator in JavaScript, you need to use Math.pow.

Related

Persistent negative offset span value when serialising a structure in Borsh generated by Anchor

Using the Anchor framework for blockchain programs on Solana, I defined a structure with four String fields. Besides that, it has a BN.js BigNum, a u32 and a u16. When writing an instruction which takes these as input, that structure needs to be serialised using Borsh. However, passing the structure to the function I've written, I get a (seemingly internal) error which looks like the following:
RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 && <= 1000. Received -4
at validateOffset (buffer.js:111:3)
at Buffer.write (buffer.js:1079:5)
at Blob.encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/buffer-layout/lib/Layout.js:2327:7)
at BNLayout.encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/#project-serum/borsh/dist/lib/index.js:37:26)
at Structure.encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/buffer-layout/lib/Layout.js:1263:26)
at Structure.encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/buffer-layout/lib/Layout.js:1263:26)
at BorshInstructionCoder._encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/#project-serum/anchor/dist/cjs/coder/borsh/instruction.js:87:28)
at BorshInstructionCoder.encode (/home/simeon/dev/freelance/niels_nieuw/node_modules/#project-serum/anchor/dist/cjs/coder/borsh/instruction.js:72:21)
at /home/simeon/dev/freelance/niels_nieuw/node_modules/#project-serum/anchor/dist/cjs/program/namespace/index.js:39:100
at ix (/home/simeon/dev/freelance/niels_nieuw/node_modules/#project-serum/anchor/dist/cjs/program/namespace/instruction.js:46:23) {
code: 'ERR_OUT_OF_RANGE'
}
This error had never before occurred to me. After doing some digging, I found that buffer-layout.js is the code responsible for setting the aforementioned offset value for the layout of the buffer. Upon closer inspection, a span value of -1 indicates that the span is indeterminate and needs to be checked with a call to getSpan(). As far as I understand it, this function should normally return the number of bytes that the data field takes up. However, in my case the function still returns -1; because I have four Strings (which are of this 'indeterminate' kind) the four spans get summed to calculate the total offset. Normally, this is functional code because all span values are expected to be strictly positive, but since mine are somehow negative the resulting offset is negative too. So, for some strange reason my specific structure causes some things to fail somewhere, because Strings have never been an issue in Anchor.
Through adding some console.log()s in various places, I managed to get some more debug info. The following pastebin contains this detailed log. The file starts with about 530 lines of errors revealing which fields are still indeterminate by the time the program instruction gets called. After that you see a first structure which does get encoded correctly, and then after that the bane of my existence.
Does anyone have any pointers as to how I could resolve this problem?
For the reference: the error-causing struct looks like this in Rust:
#[account]
pub struct Reactie {
pub discord_handle : String,
pub twitter_handle : String,
pub cv_url : String,
pub github_handle : String,
pub id_van_vacature : u32,
pub eigenaar : Pubkey,
pub reactie_id : u16,
}
and like this in TypeScript:
interface Reactie {
discord: string,
twitter: string,
cv_url: string,
github: string,
idVanVacature: number,
eigenaar: PublicKey,
reactieID: number
}

Limiting values to a variable in Node.js

I'm not sure if this question makes sense but when I'm using an IDE and for example I type:
x = 5
s = typeof(x)
if (s === ) //Cursor selection after === (before i finished the statement)
the IDE's autocomplete feature gives me a list of the possible values. And if I type value that doesn't exist in that list it's highlighted, and when I execute the program (in other examples, not this one), it throws an error.
I want to achieve a similar functionality with my own variables so that I can only assign specific values to it.
I'd recommend using TypeScript. See this question.
For example, your code will throw a TypeScript error if you try to compare the result of typeof against anything that isn't a proper JavaScript type:
x = 5
s = typeof(x)
if (s === 'foobar') {
}
results in
In a reasonable IDE with TypeScript (such as VSCode), any line of code that doesn't make sense from a type perspective will be highlighted with the error.
If you want to permit only particular values for some variable, you can use | to alternate, eg:
let someString: 'someString' | 'someOtherString' = 'someString';
will mean that only those two strings can occur when doing
someString =
later.
TypeScript makes writing large applications so much easier. It does take some time to get used to, but it's well worth it IMO. It turns many hard-to-debug runtime errors into (usually) trivially-fixable compile-time errors.

Bitshift Operator Used on Object in Javascript

I've been trying to teach myself Javascript lately and I'm noticing several slightly quirky syntax choices used by various different authors. Usually I can figure them out, but this one's stumped me a bit.
The author of the post here creates an empty object, colors, which will contain a set of properties for each background color in the page. Each color property will have a value equal to the total area covered by that color. To do this, he uses the following syntax:
// ...set or override it in the colors object,
// adding the current element area to the
// existing value.
colors[bgColor] = (colors[bgColor] >> 0) + nodeArea;
At this point, the property named by the value of bgColor may or may not be present in the object. The intent of the expression in the parenthesis is presumably to return the running total or 0 if this is the first time the color is seen. My question is, is this the right shift operator being overloaded and I'm searching for the wrong name, or why does the right shift behave this way?
The intent of the expression in the parenthesis is presumably to return the running total or 0 if this is the first time the color is seen. My question is, is this the right shift operator being overloaded and I'm searching for the wrong name, or why does the right shift behave this way?
It's not overloaded (JavaScript doesn't have operator overloading). It's relying on the fact that undefined >> 0 is 0 and anyNumber >> 0 is anyNumber (caveats to follow). If the property doesn't exist yet, looking it up yields undefined, and so >> 0 turns that into 0. If the property is defined and contains a whole number that fits in 32 bits, >> 0 returns the number without changing it. (If the number has a fractional part, it's truncated, and if it doesn't fit in 32 bits, it's wrapped if I'm reading that right, but I don't think that's what the coder is trying to do.) So by doing that, then adding the area, they are indeed adding to a running total (or initializing it if it isn't there yet).
It's mostly a shorthand version of:
if (colors[bgColor]) {
colors[bgColor] += nodeArea;
}
else {
colors[bgColor] = nodeArea;
}
...(as any falsey value >> 0 is 0) but with the added feature that it will always result in a non-NaN number provided nodeArea is a non-NaN number, whereas with some truthy non-number values for colors[bgColor] ({}, for instance), the "long" version above would result in a string or in NaN.
It is just one of the tricks to avoid an if condition. When you create the property with the value in bgColor, you might do something like this
if (colors[bgColor] === undefined) {
colors[bgColor] = 0;
}
colors[bgColor] += nodeArea;
This is is to make sure that the initial value is 0, if the bgColor is not there in colors, otherwise undefined will be the value. So, to avoid that, if we can convert undefined to 0 we can simply avoid that if condition.
console.log(undefined >> 0);
# 0
So, colors[bgColor] >> 0 will be 0 if bgColor is not already defined, otherwise the actual value as it is, since right shifting by zero doesn't change the actual value.
It might be an attempt to cast the color value as a 32-bit value, since Javascript normally stores all values as IIRC 64-bit floats (with 53 bit precision), but on bitwise operations it treats them as 32-bit integers. This might be an attempt to force the value to be treated as such before adding NodeArea.
I think it also forces it to be interpreted as a Number, but the implicit type of NodeArea would have that effect as well, normally.
edit - T.J. Crowder also makes another point which I tried to find from MDN but didn't: the return value of the operation if the property doesn't exist would be 0, thus setting the value as well). In face his whole post makes a good point :P.

Counting in javascript from math.pow

I got this little piece of code which is used for a google maps.
To 'link' the markers to a filter there is a count.
Each marker has a props which holds a number so it will be able to filter.
at the minute the math.pow is used.
You understand the props for the markers go from 1-2-4-8-16-32 untill you get over millions and billions. What i'd like to do is alter the code so it does 1+1=2 1+2=3 4, 5 ,6 ,7 and so on so i don't have to fill in these HUGE numbers.
The code is here:
$.each(sizer,function(i,b){
props+=($(b).is(':checked'))?Math.pow(2,i):0;
});
If I understand correctly, what you want is this:
$.each(sizer,function(i,b){
props+=($(b).is(':checked')) ? i : 0;
});
There are two things for you to understand:
the syntax of the ternary operator is as follows: condition ? expr1 : expr2. Basicly it is a one-line if/else statement - if the condition is true - execute the first expression, otherwise execute the second expression. You can read more about it on the MDN docs
Math.pow is a built-in javascript function that accepts two numbers - a number and a power of this number. So Math.pow(2, i) means 2 to the power of i. This is something you probably guessed by now.
It is very hard to help more than that, because I don't have the rest of the code at my disposal. For example, how can I know what are the values of sizer? Etc...

I can't figure out the bug in this jQuery

The page having problems is...
http://schnell.dreamhosters.com/index.php?page=gallery#
I use Firebug to debug my jQuery and other code tidbits and it's been proving very useful for Javascript/jQuery debugging. However, at the same time, it's been one of the most frustrating debugging experiences I've ever gone through. I'm not sure why but sometimes it seems like I can copy someone else's methodology from a tutorial, character for character, and yet still come up with errors.
In any case, the problem here is that Firebug claims there is a bug on line 20 of the source.
missing : after property id
[Break on this error] $('#table').animate({"left: " + attr + "px"}, 2000);\n
This bug seems like a huge load to me because the colon is right there! And this is why debugging jQuery/Javascript is such a pain sometimes. The error messages are rather convoluted and sometimes don't even make a lick of sense to me. Or maybe that's just Firebug.
Either way, the goal I'm going for here is that I'm trying to dynamically change the animate function such that the more you click the left arrow, the further left the grid of images is shifted (due to the nature of the CSS 'left' property). I have Javascript variables and a hidden input tag to help hold essential values, but the major hurdle is getting the animate function to recognize these variables. Near as I can tell it will only accept string literals for arguments on how to animate and the documentation doesn't help me because it doesn't discuss the use of variables with animate, as if it's impossible.
Well, let's just say I don't like impossible, he likes to get in my way a lot.
The object literal passed to the animate function is not well formed, it should be:
$('#table').animate({left: attr + "px"}, 2000);
Edit: Looking closely to your code, you are also trying to get a value from an input with id = "count", and you have a missing # character to have an ID selector:
var count = +$('#count').val(); // get #count value as Number
You are also incrementing this count variable, but you should first convert it to Number, because the value attribute of input elements are string. (I did it using the unary plus operator on the right-hand side of the assignment).
You have to convert it to a number, because if you add two variables and one of them is a string, concatenation will take place:
"1" + 1 == "11"
Try:
$('#table').animate({left: attr}, 2000);
The "px" units of measurement here aren't necessary. That aside, the above is the correct creation of an anonymous object. You were just putting a string inside curly braces.

Categories

Resources