Skip to content

Instantly share code, notes, and snippets.

@inear
Created November 3, 2016 08:23
Show Gist options
  • Save inear/3ba5259cc3e3e5eb74eff80439bd056a to your computer and use it in GitHub Desktop.
Save inear/3ba5259cc3e3e5eb74eff80439bd056a to your computer and use it in GitHub Desktop.
FrameNumerToBinory
{
// Demo Palette.jsx
//
// This script creates and shows a floating palette.
//var bitLayers = new Array();
var bitWidth = 5; // width of the shape representing binary
var bitHeight = 8; // height of the shape representing binary
var bitSpacing = 7; // spacing between shapes
var bitBGHeight = 0; // do not set, calculated below
var bitColor = [1,0,0]; /// color of the shape [R,G,B] Use only full color values: [1,0,0] or [0,1,0] or [0,0,1]. Other values will not work.
var fillColor = [0,0,0]; /// color of the bar [R,G,B]
var bitOffsetFromTop = 3; // shape distance from the top of the bar
var bitOffsetFromBottom = 3; // shape distance from the bottom of the bar
function FrameNumbering(thisObj)
{
bitBGHeight = bitOffsetFromTop+bitHeight+bitOffsetFromBottom; //calculate height of the bar
var firstComp = app.project.item(1);
firstComp.height = firstComp.height+bitBGHeight; //increase the height by the bar height
if(!checkFrames(firstComp)){ // check if the there is enough space for the binary numbers. If shapes and spacing are wide, the numbers may not fit the video
return;
};
var bar = firstComp.layers.addSolid(fillColor,"bitBG", firstComp.width, bitBGHeight, 1) //create the bar
// bitLayers.push(bar);
resetAnchor(bar); // reset anchor for easier positioning
setPosition(bar,0,firstComp.height-bitBGHeight,false); //position the bar to the bottom of the composition
calcBits(firstComp,bitColor); //create shapes
if(bitColor[0] == 1) bitColor = 0; // determine which color is used, and alert it for the AS3, 0=R, 1=G, 2=B. Only solid, full colors can be used
else if(bitColor[1] == 1) bitColor = 1;
else bitColor = 2;
var alertTxt = "Copy these values to the ActionScript file: \n";
alertTxt +="_bitWidth="+bitWidth+";\n";
alertTxt +="_bitHeight="+bitHeight+";\n";
alertTxt +="_bitSpacing="+bitSpacing+";\n";
alertTxt +="_bgHeight="+bitBGHeight+";\n";
alertTxt +="_bitColor="+bitColor+";\n";
alertTxt +="_bitOffsetFromBottom="+bitOffsetFromBottom+";\n";
alertTxt +="_bitOffsetFromTop="+bitOffsetFromTop+";\n";
alertTxt +="_frames="+(firstComp.frameRate*firstComp.duration)+";\n";
alert(alertTxt); //alert used parameters so they can be copied to AS3
}
function setPosition(element,x,y,z){ //sets position of an element
var pos =element.transform.position.value;
if(x !== false) pos[0] = x;
if(y !== false) pos[1] = y;
if(z !== false) pos[2] = z;
element.transform.position.setValue(pos)
}
function resetAnchor(element){ // resets anchor to top left corner
element.transform.anchorPoint.setValue([0,0,0])
}
function checkFrames(comp){ //calculate wether the binary fits the video
var fps = comp.frameRate*comp.duration;
var bitSpace = bitWidth*bitSpacing;
var binaryLen = fps.toString(2).split("").length;
bitSpace = bitSpace*binaryLen;
if(bitSpace > comp.width) {
alert("The video is not wide enough for the bit size and spacing. Required width is "+bitSpace+"px and video is "+comp.width+"px wide.");
return false;
}
return true;
//timeToCurrentFormat(comp.duration,fps,true)
}
function calcBits(comp,bitColor){ //create shapes
var fps = comp.frameRate*comp.duration;
var bin = "1"+fps.toString(2);
var bit;
for(var i =0; i<bin.length;i++){
createBit(comp,bitColor,i)
}
}
function createBit(comp,bitColor,pos){ //create shapes
var gfx = comp.layers.addSolid(bitColor,"bit"+pos, bitWidth, bitHeight, 1)
resetAnchor(gfx);
setPosition(gfx,comp.width-(bitWidth+bitSpacing)*(pos)-bitWidth,comp.height-bitHeight-bitOffsetFromBottom,false);
//add the expression
var expression = 'bitpos = '+pos+';frame = timeToFrames();frame = parseInt(frame,10).toString(2).split("").reverse().join("");if(frame.length < bitpos) bit = "0";else bit = frame.substr(bitpos,1);if(bit == "1") value = 100;else value = 0;';
gfx.opacity.expression = expression;
}
//run the code
FrameNumbering(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment