How To Animate A Drawing Using Point Over Frames Procesing
I've recently started exploring the generative art and creative coding space, and it's been a lot of fun, but boy is in that location a lot to larn. At that place are, however, tons of cracking artists and creators out in that location that have been very inspiring to follow. Big thanks to the following people who share and so openly with the community in educational and motivational ways:
- Zach Lieberman (@zachlieberman)
- dave (@beesandbombs)
- Tyler Hobbs (@tylhobbs)
- Anders Hoff (@inconvergent)
- Matt DesLauriers (@mattdesl)
- Jaume Sanchez Elias (@thespite)
- Jessica In (@shedrawswithcode)
- Saskia Freeke (@sasj_nl)
Afterwards following these amazing people and seeing the continuous flow of their work, I got the itch to begin trying to create things myself and share them on Instagram (@pbeshasketch). I tried Processing, openFrameworks, 3.js, and plain old SVG before finally giving p5.js a shot (still curious to attempt TouchDesigner!)
While it's straightforward to save a notwithstanding image from p5.js with the saveCanvas() function, the documentation suggests because a third-party library like CCapture.js for saving longer animations. It seems there is some defoliation over how to get it to piece of work judging past issue #69 on the CCapture.js repo, so I thought I'd share an approach that worked for me.
Export PNGs with CCapture.js
- View the full version of this code
- View the live demo
Permit's say nosotros take created a sketch in p5.js that animates in a loop for 3 seconds:
Our arroyo volition be save each frame of the loop equally a PNG file then sew together them together in a video with ffmpeg. To hands download each frame from the browser, we'll apply CCapture.js.
To exercise so, we offset need to set an explicit frame charge per unit and instantiate CCapture.
// the frame rate (frames per second) var fps = 30 ; // the canvass capturer instance var capturer = new CCapture ( { format: 'png' , framerate: fps } ) ;
When nosotros're prepare to record, we'll phone call capturer.start()
. In this example, nosotros record immediately, but y'all could also have the recording brainstorm in response to a button click or other action.
// setup the drawing function setup ( ) { createCanvas ( 540 , 540 ) ; // this is optional, but lets the states come across how the animation will // look in browser. frameRate (fps) ; // UPDATE: 2020-01-22 this has been moved to describe() // offset the recording // capturer.start(); }
The nice part nigh CCapture.js is that we can plan our sketches according to elapsed time and it volition ensure every possible frame is rendered, even if individual frames take longer to draw in browser than our desired frame rate can keep up with.
All we demand to practice to get our PNGs per frame is to call capturer.capture()
on each depict telephone call. This function takes the canvass DOM node as its argument, and at the time of writing, p5.js assigns an ID to it called defaultCanvas0. The code to capture a frame from our sketch is:
capturer. capture (document. getElementById ( 'defaultCanvas0' ) ) ;
Finally, we phone call capturer.finish()
and capturer.save()
when nosotros're done recording and CCapture.js will prompt us to download a .tar
file containing our images. See the full draw function below:
// needed to decrease initial millis before first draw to // begin at t=0. var startMillis; // draw function depict ( ) { // UPDATE: 2020-01-22 start must happen hither in newer versions of p5.js if (frameCount === 1 ) { // start the recording on the outset frame // this avoids the code freeze which occurs if capturer.beginning is called // in the setup, since v0.9 of p5.js capturer. outset ( ) ; } if (startMillis == zip ) { startMillis = millis ( ) ; } // duration in milliseconds var duration = 3000 ; // compute how far we are through the animation every bit a value // between 0 and ane. var elapsed = millis ( ) - startMillis; var t = map (elapsed, 0 , duration, 0 , 1 ) ; // if we accept passed t=one and then end the animation. if (t > ane ) { noLoop ( ) ; console. log ( 'finished recording.' ) ; capturer. stop ( ) ; capturer. relieve ( ) ; return ; } // actually draw drawCircles (t) ; // ... excerpted for clarity // handle saving the frame console. log ( 'capturing frame' ) ; capturer. capture (certificate. getElementById ( 'defaultCanvas0' ) ) ; }
Creating a video with ffmpeg
At this point, we should have a folder full of PNG files, one for each frame in our animation.
To combine these into a .mp4 video file, nosotros can utilize a control-line tool called ffmpeg. We need a few components to construct the command to run:
- Frame rate: xxx (see
fps
in the code) - Dimensions: 540x540 (should match
createCanvas()
in the code) - Frame filenames:
"%07d.png"
(incrementing numbers, 7 numbers long) - Quality (CRF): 17 (run across ffmpeg docs, only 17–28 is reasonable, 0 is lossless)
Open a vanquish to the binder containing the frame files and run this command:
ffmpeg -r 30 -f image2 -s 540x540 -i "%07d.png" -vcodec libx264 -crf 17 -pix_fmt yuv420p output.mp4
When you run this, you'll run across a whole agglomeration of messages from ffmpeg while information technology's doing its magic, but afterwards y'all'll have a file named output.mp4 in the same directory equally the frame files.
Uploading to Instagram
I had no idea how to get the video I just made posted to Instagram since you lot are restricted to posting from your phone. My solution was to utilise Dropbox as a proxy to become my video file on to my phone, which would let me post it to Instagram.
- Put the video in a Dropbox folder from your computer
- Open the Dropbox app on your phone and select the video file
- Export to your Instagram feed via the Dropbox app
Done and done!
I'm still figuring out what the all-time format for uploading to diverse social media platforms is, simply so far I've had some luck with 1080x1080 at 30fps for Instagram and 1280x1280 at 30fps for Twitter.
Bonus: Creating a GIF with ImageMagick's catechumen
While we're here, why not cover the command to generate a GIF from the frame files. To practice this, we can use ImageMagick'south wonderful convert control.
All nosotros demand to run the command is to figure out the frame delay in hundredths of a 2d. The frame delay is the reciprocal of the frame rate (~sixteen.67ms at 60fps, ~33.33ms at 30fps). Nosotros can compute this as follows:
frame delay | = 100 × 1 / frame rate |
= 100 / 30 | |
= 3.33 |
Now in a shell in the same binder where the frame files are, run:
catechumen -delay three.33 -loop 0 *.png output.gif
VoilĂ ! A gif has been born (see output.gif).
Adjacent Steps
I've been trying out a number of things so far as I begin exploring this infinite, and CCapture.js seemed to piece of work pretty well with p5.js in this footling experiment. Next, I'd similar to investigate Matt DesLaurier'southward canvas-sketch to run across if it simplifies matters (plus it just seems actually cool).
- View the code used in this post
- View the live demo
If you accept any questions, suggestions, or other comments, I'd honey to hear them! Go out a message hither or tweet at me @pbesh.
Source: https://peterbeshai.com/blog/2018-10-28-p5js-ccapture/
Posted by: harvardwithereas1986.blogspot.com
0 Response to "How To Animate A Drawing Using Point Over Frames Procesing"
Post a Comment