JS: Move a div following a defined trajectory/path - javascript
I'm trying to do an animation: move a life-belt on water very slowly, floating on the sea across waves and wind.
For code, I would like to make a div following a path. I'm using Popmotion for moving and reversing the animation (with the option {yoyo: true}).
I saw this example but a random position is a bit tricky and creates a "stop" before running again the animation, this is not like a realtime random trajectory.
http://jsfiddle.net/Xw29r/375/
Is it possible to do something like this in javascript/SVG?
I actually created Popmotion so I want to help you in the right direction here.
To get the non-straight lines you can play around with asymmetric easing. For example the first movement there, if you gave y an easeIn and x and easeOut the line would bend, maybe not completely to your liking but you can play around with different easing strengths to get a good combination.
You could also look into using the Simulate action instead of Tween, maybe the spring physics with a high initial x movement and playing around with spring and friction properties.
You don't have to use yoyo, you could write an onComplete function that starts the actor again using new randomised properties.
Hopefully some of these tips are helpful to you!
Related
Canvas HTML5 animation with GSAP/d3.js
I want to create a HTML5 canvas animation likely the one on this site: https://flowstudio.co/. I have started with GSAP, but it looks like creating something like this, is really a big task. I have to create mostly every point/move singular and i have no idea if there is a faster/better way. Currently i only have looked at GSAP without plugins. Is there some special tool/(GSAP) plugin that can help to create this? Or should i maybe use d3.js? I also tried to find an tutorial for this, but it looks like there is nothing for this more advanced case. Thanks for the help!
The example you provided is using THREE.js and I would suggest you to use it too since you want to operate in 3D space also. When you want to animate a large ammount of points you will need to use a vertex shader. That's because vertex shader will allow you to calculate all of the points positions in one step (thanks to parallel computing on the GPU), whereas doing it the 'normal way' (on the CPU) is very bad on performance, since every single point has to be calculated one by one. (here you can see the difference) The way you animate the points is a little different than you might think- you don't want to apply animation to every. single. point... Instead you will need three things that you will pass to the shader: -array containing starting points position, -array containing final points position, -blend parameter (just a float variable taking values from 0 to 1). Then you use GSAP to animate only the blend parameter, and shader does the rest (for example when the blend parameter is 0.5 the point position is exactly halfway between starting position and final position that you provided to the shader) The example you provided is also using some kind of Perlin Noise function which you will have to implement in the shader also. Its a lot to bite at one time but here's some great tutorials from Yuri Artyukh which will help you achieve something similiar: https://www.youtube.com/watch?v=XjZ9iu_Z9G8&t=5713s https://www.youtube.com/watch?v=QGMygnzlifk https://www.youtube.com/watch?v=RKjfryYz1qY https://www.youtube.com/watch?v=WVTLnYL84hQ&t=4452s Hope it helps and...good luck!
Pixi.js: How is this complex series of position/alpha/scale numbers generated?
Can anyone explain how this file was generated, what is was made with? It's a huge amount of y position, alpha and scale points, one for each frame, for animating sprites on the pixi canvas. Some elements have maybe 800 position points? I'm thinking each y point was not typed individually, but maybe it was :) Seems like it would have been tough to refine the animation if each number was typed rather than generated some other way. http://flashvhtml.com/js/site/ScrollMap.js For instance: var rockets = {rockets:[{startFrame:0, endFrame:100, position:[1.15,5,3.14,2.25,10,3.14,3.3,15,3.14,4.3,20,3.14,5.3,25,3.14,6.25,30,3.13,7.15,35,3.13,8.05,40,3.13,8.9,45,3.13,9.75,50,3.13,10.55,53,3.13,11.35,54,3.13,12.15,53,3.13,12.9,50,3.13,13.65,45,3.13,14.4,38,3.13,15.1,29,3.13,15.8,18,3.13,16.5,5,3.13,17.2,-9.95,3.13,17.85,-26.9,3.13,18.5,-45.85,3.13,19.15,-66.8,3.13,19.8,-89.75,3.13,20.45,-114.7,3.13,21.05,-141.65,3.13,21.65,-170.6,3.13,22.25,-201.55,3.13,22.85,-234.5,3.13,23.45,-269.45,3.13,24.05,-306.4,3.13,24.6,-345.35,3.13,25.15,-386.3,3.13,25.7,-429.25,3.13,26.25,-474.2,3.13,26.8,-521.15,3.13,27.35,-570.1,3.13,27.9,-621.05,3.13,28.45,-674,3.13,29,-728.95,3.13,29.55,-785.9,3.13,30.05,-844.85,3.13,30.55,-905.8,3.13,31.05,-968.75,3.13,31.55,-1033.7,3.13,32.05,-1100.65,3.13,32.55,-1169.6,3.13,33.05,-1240.55,3.13,33.55,-1313.5,3.13,34.05,-1388.45,3.13,34.55,-1465.4,3.13,35.05,-1544.35,3.13,35.55,-1625.3,3.13,36.05,-1708.25,3.13,36.55,-1793.2,3.13,37.05,-1880.15,3.13,37.55,-1969.1,3.13,38.05,-2060.05,3.13,38.55,-2153,3.13,39.05,-2247.95,3.13,39.55,-2344.9,3.13,40.05,-2443.85,3.13,40.55,-2544.8,3.13,41,-2647.75,3.13,41.45,-2752.7,3.13,41.9,-2859.65,3.13,42.35,-2968.6,3.13,42.8,-3079.55,3.13,43.25,-3192.5,3.13,43.7,-3307.45,3.13,44.15,-3424.4,3.13,44.6,-3543.35,3.13,45.05,-3664.3,3.13,45.5,-3787.25,3.13,45.95,-3912.2,3.13,46.4,-4039.15,3.13,46.85,-4168.1,3.13,47.3,-4299.05,3.13,47.75,-4432,3.13,48.2,-4566.95,3.13,48.65,-4703.9,3.13,49.1,-4842.85,3.13,49.55,-4983.8,3.13,50,-5126.75,3.13,50.45,-5271.7,3.13,50.9,-5418.65,3.13,51.35,-5567.6,3.13,51.8,-5718.55,3.13,52.25,-5871.5,3.13,52.7,-6026.45,3.13,53.15,-6183.4,3.13,53.6,-6342.35,3.13,54.05,-6503.3,3.13,54.5,-6666.25,3.13,54.95,-6831.2,3.13,55.4,-6998.15,3.13,55.85,-7167.1,3.13,56.3,-7338.05,3.13,56.75,-7511,3.13,57.2,-7685.95,3.13]}, and also the position, scale and alpha here: mc89:{view:'cloudFade', depth:3, startFrame:488, endFrame:540, position:[1,1,0.95,1.2,0.95,1.2,0.95,1.2,1,1,0.95,1.2,0.95,1.2,0.95,1.2,0.95,1.2,0.95,1.2,0.95,1.2,0.95,1.2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], scale:[20.718994140625,15.5989990234375,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718994140625,15.5989990234375,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718368530273438,15.597808837890625,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375,20.718994140625,15.5989990234375], alpha:[0,0.25,0.5,0.75,1,0.875,0.75,0.625,0.5,0.375,0.25,0.125,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}, It kind of looks like skrollr.js meets greensock on steroids. It's from the brilliant http://flashvhtml.com site. Anyhow, I'd like to know how it was put together.
Flash v HTML says that this file was produced using Adobe Edge Animate (it's in their "Findings" section). Back then, it was known for producing "brute-force" animation sheets. I'm not sure if they use a more programmatic approach nowadays, but GreenSock is a much lighter approach to making animations of this sort. The third video in this page shows the animation process. It'll export the same style of file you linked to, chock-full of x, y, alpha, etc values.
Position resizable circles near each other
I am working on this browser-based experiment where i am given N specific circles (let's say they have a unique picture in them) and need to position them together, leaving as little space between them as possible. It doesn't have to be arranged in a circle, but they should be "clustered" together. The circle sizes are customizable and a user will be able to change the sizes by dragging a javascript slider, changing some circles' sizes (for example, in 10% of the slider the circle 4 will have radius of 20px, circle 2 10px, circle 5 stays the same, etc...). As you may have already guessed, i will try to "transition" the resizing-repositioning smoothly when the slider is being moved. The approach i have tried tried so far: instead of manually trying to position them i've tried to use a physics engine- The idea: place some kind of gravitational pull in the center of the screen use a physics engine to take care of the balls collision during the "drag the time" slider event i would just set different ball sizes and let the engine take care of the rest For this task i have used "box2Dweb". i placed a gravitational pull to the center of the screen, however, it took a really long time until the balls were placed in the center and they floated around. Then i put a small static piece of ball in the center so they would hit it and then stop. It looked like this: The results were a bit better, but the circles still moved for some time before they went static. Even after playing around with variables like the ball friction and different gravitational pulls, the whole thing just floated around and felt very "wobbly", while i wanted the balls move only when i drag the time slider (when they change sizes). Plus, box2d doesn't allow to change the sizes of the objects and i would have to hack my way for a workaround. So, the box2d approach made me realize that maybe to leave a physics engine to handle this isn't the best solution for the problem. Or maybe i have to include some other force i haven't thought of. I have found this similar question to mine on StackOverflow. However, the very important difference is that it just generates some n unspecific circles "at once" and doesn't allow for additional specific ball size and position manipulation. I am really stuck now, does anyone have any ideas how to approach this problem? update: it's been almost a year now and i totally forgot about this thread. what i did in the end is to stick to the physics model and reset forces/stop in almost idle conditions. the result can be seen here http://stateofwealth.net/ the triangles you see are inside those circles. the remaining lines are connected via "delaunay triangulation algorithm"
I recall seeing a d3.js demo that is very similar to what you're describing. It's written by Mike Bostock himself: http://bl.ocks.org/mbostock/1747543 It uses quadtrees for fast collision detection and uses a force based graph, which are both d3.js utilities. In the tick function, you should be able to add a .attr("r", function(d) { return d.radius; }) which will update the radius each tick for when you change the nodes data. Just for starters you can set it to return random and the circles should jitter around like crazy.
(Not a comment because it wouldn't fit) I'm impressed that you've brought in Box2D to help with the heavy-lifting, but it's true that unfortunately it is probably not well-suited to your requirements, as Box2D is at its best when you are after simulating rigid objects and their collision dynamics. I think if you really consider what it is that you need, it isn't quite so much a rigid body dynamics problem at all. You actually want none of the complexity of box2d as all of your geometry consists of spheres (which I assure you are vastly simpler to model than arbitrary convex polygons, which is what IMO Box2D's complexity arises from), and like you mention, Box2D's inability to smoothly change the geometric parameters isn't helping as it will bog down the browser with unnecessary geometry allocations and deallocations and fail to apply any sort of smooth animation. What you are probably looking for is an algorithm or method to evolve the positions of a set of coordinates (each with a radius that is also potentially changing) so that they stay separated by their radii and also minimize their distance to the center position. If this has to be smooth, you can't just apply the minimal solution every time, as you may get "warping" as the optimal configuration might shift dramatically at particular points along your slider's movement. Suffice it to say there is a lot of tweaking for you to do, but not really anything scarier than what one must contend with inside of Box2D. How important is it that your circles do not overlap? I think you should just do a simple iterative "solver" that first tries to bring the circles toward their target (center of screen?), and then tries to separate them based on radii. I believe if you try to come up with a simplified mathematical model for the motion that you want, it will be better than trying to get Box2D to do it. Box2D is magical, but it's only good at what it's good at.
At least for me, seems like the easiest solution is to first set up the circles in a cluster. So first set the largest circle in the center, put the second circle next to the first one. For the third one you can just put it next to the first circle, and then move it along the edge until it hits the second circle. All the other circles can follow the same method: place it next to an arbitrary circle, and move it along the edge until it is touching, but not intersecting, another circle. Note that this won't make it the most efficient clustering, but it works. After that, when you expand, say, circle 1, you'd move all the adjacent circles outward, and shift them around to re-cluster.
Javascript & Canvas: Endless random animation of slightly morphing circle?
I'm completely new to canvas and animating objects with it. I did a little bit of research (e.g. I found RaphaelJS) however I couldn't find any general answer or tutorial on how to create a "morphing" circle. The image I posted here is what I would like to do: I'd like to create one circle that is endlessly animated via a randomizer and is slightly morphing its contours. I know this might be not a "real" question for this forum, however I just wonder if anyone could provide a few tipps or tricks on how to do something like that. By "how to do something like that" I'm speaking actually about the technique on how to morph a circle. Do I have to "mathematically" create a circle with dozens of anchor-points along the edge that are influenced by a randomized function? I would really appreciate some starting help with this. Thank you in advance.
A circle can be reasonably well approximated by 4 cubic curves (one for each quarter and the control points on the tangents - google for the correct length of the control segments or calculate them yourself - see here. You could then randomly animate the control points within a small radius to get a wobbling effect.
Do I have to "mathematically" create a circle with dozens of anchor-points along the edge that are influenced by a randomized function? Yes, you do, although it should not be necessary to create "dozens". You may find the .bezierCurveTo() and .quadraticCurveTo() functions useful to provide smooth interpolated curves between control points.
When you can use a raster image then for every point you can displace it along the x-axis with a sin function. You can run the same function along the y-axis but instead to simply displace the pixel you can double it. This should give you a morphing circle but it also works with other shapes.
How to script animation sequences in three.js?
I'm just getting started with three.js and I'm trying to make a very basic animation sequence. I've gone through the basic examples and see how to make objects move and spin, but I don't quite get how to structure a series of animation sequences. For example... Show 3 cubes and move them to a new position (ideally staggered a bit so as not to move at the same time) THEN once they've all arrived, make two of them rotate an axis THEN move them to a new position etc... There is no user interaction with what I'm building - it's just a series of movements that happen in sequence. Do people use something like Frame.js for this sort of thing or is there something simpler that I'm overlooking?
Try out director.js - simple but effective. https://github.com/zz85/ThreeLabs/blob/master/Director.js There is somewhere a longer showcase, can't find the link, though.
Tween.js is one choice: https://github.com/sole/tween.js/. Tutorial: http://learningthreejs.com/blog/2011/08/17/tweenjs-for-smooth-animation/ Example: http://learningthreejs.com/data/tweenjs_for_smooth_animation/tweenjs_for_smooth_animation.html