How to use the transform tool to rotate multiple layers? - animation

I have multiple layers that I used for animation.
Lets say I have "1111". Each 1 is a layer. I want each layer rotate 10 degrees.
Is there any way to do these automatically without selecting each one independently.
Thanks.

you can write a script to loop through all the layers in your document and rotate using something similar to below.
var j = 10;
for (var i = 0; i < app.activeDocument.layers.length; i++) {
var lyr = app.activeDocument.layers[i];
lyr.rotate(j)
j = j + 10;
}

Related

Creating Grid in as3

I am trying to make to grid to squares using as3.
I am using nested for loops to create the grid.
import flash.display.Shape;
import flash.display.MovieClip;
var n=10;
var myClip = new MovieClip;
stage.addChild(myClip);
for (var i = 0; i < n * 10; i += 10) {
for (var j = 0; j < n * 10; j += 10) {
var _shape = new Shape;
myClip.addChild(_shape);
_shape.graphics.lineStyle(1);
_shape.graphics.beginFill(0xcccccc);
_shape.graphics.drawRect(i, j, 10, 10);
_shape.graphics.endFill();
}
}
I am applying Zooming and Panning Gestures on myClip. For this Grid size everything works fine. As soon as I increase the value of n, it starts lagging
My game requires a bigger grid. Please help
I'll show some ideas as code, my AS3 is a bit rusty but perhaps it helps.
You can reduce the number of Shape instances or draw the vector based grid graphic to a Bitmap. Since you are zooming and panning it you'll want to smooth it. Depending on performance and how much you zoom you also may want to draw the bitmap at a higher resolution than initially used. See comments in the below.
import flash.display.Shape;
import flash.display.MovieClip;
var n=10;
// do you actually need dynamic binding or a timeline in this?
// if not (also if you don't know what it means) do use the Sprite class instead
var myClip = new MovieClip;
stage.addChild(myClip);
// if you don't need individual shapes move this out of the for loop
// your logic with all of the shapes having the same origin suggests this might be so
var _shape = new Shape;
myClip.addChild(_shape);
for (var i = 0; i < n * 10; i += 10) {
for (var j = 0; j < n * 10; j += 10) {
_shape.graphics.lineStyle(1);
_shape.graphics.beginFill(0xcccccc);
_shape.graphics.drawRect(i, j, 10, 10);
_shape.graphics.endFill();
}
}
// then we can draw the grid to a bitmap
// since you are scaling it I'm drawing it a 300% resolution
var _bmd:BitmapData = new BitmapData(_shape.width*3, _shape.height*3, true, 0);
var _bit:Bitmap = new Bitmap(_bmd);
myClip.addChild(_bit);
_shape.width*=3;
_shape.height*=3;
_bmd.draw(stage);
_bit.smoothing = true;
_bit.width/=3;
_bit.height/=3;
// and remove the vector shape so it won't be rendered
myClip.removeChild(_shape);
// if need be it can be redrawn later

How to make randomly placed ellipses loop in p5.js?

I'm trying to make a loop in p5.js that will draw small ellipses across the canvas. I've done something like this before, but the code was different.
There, when I wanted to try a loop, all I had to type was:
for (var i = 0; i < 200; i++) {}
The manual I'm using (Make: Getting Started with p5.js) tells me that the code to do this is similiar. This was an example it gave for drawing a bunch of lines:
for (var i = 20; i < 400; i += 8) {
line(i,40,i+60,80)
}
However, when I enter this code to even test it, it doesn't work. Can someone explain how to draw multiple small ellipses on the screen (I have variables set in place for the x and y coordinates of the ellipses so that they will be random)?
EDIT: This is a more full version of the code:
function draw() {
noStroke();
fill(fishCr,fishCg,fishCb);
arc(ellX,ellY,ellW,ellH,0,180);
arc(ellX+5,ellY-10,ellW/1.5,ellH/1.5,arcEl,50);
arc(ellX-45,ellY+20,ellW/1.5,ellH*1.5,340,110);
arc(ellX-60,ellY-10,ellW/1.5,ellH*2,arcT,40);
fill(0,200,255,0.5); //this is the start of the code in
//question
for (var i = 0; i < 200; i++) {
ellipse(bubX,bubY,5,5);
}
}
Please try to be more specific than saying it doesn't work. What exactly happens? What do you see in the JavaScript console?
You should also read up on a tutorial on for loops. Here is one I wrote for Processing, but the basic ideas are the same.
Your first for loop starts at 0 and increases by 1 until it reaches 200. Your second for loop starts at 20 and increases by 8 until it reaches 400.
Also note that you have an extra opening curly brace { in your second for loop.
If you still can't get it figured out, please post a MCVE that shows the problem. Good luck.
Edit: Take a look at your for loop:
for (var i = 0; i < 200; i++) {
ellipse(bubX,bubY,5,5);
}
Here you're drawing 200 circles, but you're drawing all of them at the same location, based on the bubX and bubY variables. You probably want to pass in random values here instead of the same variables every time.
createCanvas(500, 500);
function setup() {
for (var i = 0; i < 200; i++) {
ellipse(random(0, width), random(0, height), 5);
}
}
This creates a Canvas and draws 200 small circles at random locations.
Is this waht you are looking for?

How to determine a shape touches another shape or not using kineticjs?

I have a number of shapes in my kinetc layer. If I drag and drop a shape somewhere else in the layer, how to determine the dropped shape touches another shape or not?
The thing you need to do is create a mathematical representation of the shapes you have. For most simple collision detection, you can use bounding-boxes.
Basically, if you have a circle, you can create a representation of it as being bounded in a box.
Then if you have a square, you can check if the bounding box of the square (which is the square itself) is intersecting with the bounding box of the circle.
I wrote an answer to this a while ago: HTML5 / kineticJS getIntersection function implementation
function checkCollide(pointX, pointY, objectx, objecty, objectw, objecth) { // pointX, pointY belong to one rectangle, while the object variables belong to another rectangle
var oTop = objecty;
var oLeft = objectx;
var oRight = objectx+objectw;
var oBottom = objecty+objecth;
if(pointX > oLeft && pointX < oRight){
if(pointY > oTop && pointY < oBottom ){
return 1;
}
}
else
return 0;
};
used like this:
var children = layer.getChildren();
for( var i=0; i<children.length; i++){ // for each single shape
for( var j=0; j<children.length; j++){ //check each other shape
if(i != j){ //skip if shape is the same
if(checkCollide(children[i].getX(), children[i].getY(), children[j].getX(), children[j].getY(), children[j].getWidth(), children[j].getHeight()))
alert('top left corner collided');
}
}
}
This works great if the shape you have is a rectangle of some sort, but not that great if you have two circles, as they have a radius. So this suffices for a quick check of collision, next you need another function which will check collisions more precisely.
You can also try using kineticjs with box2d, there are a bunch of tutorials out there on the topic.

d3js, apply force to array of objects

Im currently trying to apply a force with d3.js to an array of objects. But the force only gets applied to the last object. The array contains 5 svg elements.
The code for applying the force looks like this.
for (i = 0; i < arr.length; i++) {
var force = d3.layout.force()
.nodes(arr)
.links([])
.size([svgWidth, svgHeight]);
arr[i] = force;
}

I want to draw a grid on the Windows Phone 7 using XNA

I am trying to draw a grid on the screen of a Windows Phone; it will help me better position my sprites on the screen rather than guessing locations on the screen.
I have found several examples of a grid (2d or 3d) using XNA 3.0, but unfortunately the architectures are different and so the code doesnt work in XNA 4.0
Does anyone have something that could work?
Thanks!
You can download a PrimitiveBatch class here http://create.msdn.com/en-US/education/catalog/sample/primitives and use the code below to generate an appropriate grid as a texture.
PrimitiveBatch primitiveBatch;
private Texture2D GenerateGrid(Rectangle destRect, int cols, int rows, Color gridColor, int cellSize)
{
int w = (int)(cols * gridCellSize);
int h = (int)(rows * gridCellSize);
float uselessWidth = destRect.Width - w;
float uselessHeigth = destRect.Height - h;
Rectangle bounds = new Rectangle((int)(uselessWidth / 2) + destRect.X, (int)(uselessHeigth / 2) + destRect.Y, w, h);
RenderTarget2D grid = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
GraphicsDevice.SetRenderTarget(grid);
GraphicsDevice.Clear(Color.Transparent);
primitiveBatch.Begin(PrimitiveType.LineList);
float x = bounds.X;
float y = bounds.Y;
for (int col = 0; col < cols + 1; col++)
{
primitiveBatch.AddVertex(new Vector2(x + (col * gridCellSize), bounds.Top), gridColor);
primitiveBatch.AddVertex(new Vector2(x + (col * gridCellSize), bounds.Bottom), gridColor);
}
for (int row = 0; row < rows + 1; row++)
{
primitiveBatch.AddVertex(new Vector2(bounds.Left, y + (row * gridCellSize)), gridColor);
primitiveBatch.AddVertex(new Vector2(bounds.Right, y + (row * gridCellSize)), gridColor);
}
primitiveBatch.End();
GraphicsDevice.SetRenderTarget(null);
return grid;
}
One quick and dirty way you can do it (as it is for debugging the quicker the better) is to simply create a texture of a grid that is of the same resolution you are running your XNA game at (if you are running it at the same resolution as the phone, this will be 480x800). Most of the texture will simply be an alpha map and with grid lines of one pixel, you could create multiple resolutions or you can repeat a small texture of a single pixel cross dividing a section of the screen that is divisible by the resolution you are running at.
The draw method will be something as below and be called everyframe.
This code can declared inside your game class
Texture2D gridTexture;
Rectangle gridRectangle;
This code should be in your LoadContent method
//Best to use something like a png file
gridTexture = content.Load<Texture2D>("pathto/mygridtexture");
gridRectangle = new Rectangle(0,0,resolutionX,resolutionY);
This method should be called from your Draw method last to ensure it is on top assuming you are just using the standard spriteBatch.Begin() to render sprites (first if you are doing FrontToBack rendering).
public void DrawGrid()
{
spriteBatch.Draw(gridTexture, gridRectangle, Color.White);
}
This grid will remain stationary throughout the running of your application and should be useful when trying to line up your UI or objects that have relative positions in your game.
HTH.
You may want to take a look at the XPF project by RedBadger. It enables you to use XAML style layout in an XNA project.

Resources