Exclusively for the readers of Designer Daily, the people at Vecteezy have designed this cool set of 10 vector icons related to the topic of printing. All vectors in the pack can be fully customized, they come in the following format: AI, EPS, PNG, or SVG, which makes usable for both print and the web quickly.
The future is near. Are you ready for a world of cybernetic organisms?
Learn how to transform your photos into cool cyborgs in the tutorial below. I’ll show you how to use techniques like Frequency Separation to remove skin imperfections before bringing our cyborg to life!
Every great photo manipulation needs a collection of good stocks on deck as well as some inspiring reference art. For this cyborg, I was inspired by the popular style of manipulation seen in work from artists like Michael Oswald.
So to complete our cyborg, we’ll need to keep a few important notes in mind:
Cyborgs have clear, human features…
… though their bodies are usually broken down into several bionic components.
They’ll integrate modern technology into their design.
They can come in any race.
And they sometimes take over the world. Simple right?
Be deliberate about your stock choices. For our subject I’ve chosen this Woman Stock. Though the image is no longer available, you can find many incredible stocks like it on Envato Market.
The reason why I chose this stock is because of the woman’s intense gaze. So try to find images with commanding body language that you can easily manipulate down the line. Beauty and glamor photography work great for this, although you may have to deal with simple edits later (like removing skin cream from her face).
2. How to Airbrush Your Photo
The cyborgs we’ve come to know in the movies have smooth, perfect-looking skin, most likely because they’re made out of metals or other advanced materials. So to achieve this look in photography, we’ll turn to the
technique of Frequency Separation.
Step 1
Open your reference into Photoshop, changing the dpi to 300. Create a New Group named Retouching. Control-J to Duplicate your reference, placing the copy into the group.
Step 2
Before we smooth out her skin, we’ll need to remove the white dots of skin cream on her face. So select the Polygonal Tool (L) and make a selection on the left side of her face.
Copy and Paste that selection onto a New Layer, and Flip it by going to Edit > Transform Flip Horizontal. Place the skin copy over the white dots, using the Eraser Tool (E) to erase any harsh edges.
Merge the layers together. Clean up the skin with the Stamp Tool (S). First select the tool and hold Alt to source the skin, and then use the sourced skin at 30-50% Opacity to blend it into the skin area. We’ll clean up any other imperfections next.
Control-J to Duplicate the revised stock. Name the top layer High Frequency and the bottom layer Low Frequency. Turn Off the Visibility of the High Frequency layer, and then select the Low Frequency layer and go to Filter > Blur > Gaussian Blur, setting the Radius to any number that blurs the details of your portrait. Here, I set mine to 7.
Turn the VisibilityOn for the High Frequency layer. With this layer selected, go to Image > Apply Image and enter the following settings:
Make sure the Blending is set to Subtract and the Layer is set to Low Frequency. Then plug in the numbers above for the Scale and Offset. After you hit OK, set the Layer Blend Mode for the Low Frequency layer to Linear Light.
Step 4
What we have done is separated the texture (high frequency) from the tones and colors (low frequency). Now we can airbrush the skin to make it look smoother.
With the High Frequency layer selected, grab the Polygonal Lasso Tool (L) and create a selection of the skin, making sure that the selection’s Feather is set to 25-30. Go to Filter > Blur > Gaussian Blur, and blur the skin. Continue doing this all around her face, changing the Radius each time depending on how much you want to soften the skin and lessen the look of pores.
Merge the frequency layers together when you’re finished.
Here is the model with much smoother skin. We’ll continue to adjust it as we proceed.
3. How to Create the Cyborg Sketch
Step 1
Let’s move on to the sketch! It’s important to note that you’ll need a Graphics Tablet as we’ll be employing some important digital drawing/painting techniques later down the line.
Select the Brush Tool (B) and use a Hard Round Pressure Opacity Brush to begin drawing your cyborg details. Keep the Flow and Opacity set at 100% but make sure the Pen Pressure for Opacity option is checked.
Draw simple gray lines over the model indicating the areas where you would like to break down her robotic components. Try to be cognizant of how the human body relates to certain parts moving or connecting together.
Study other cyborg references or look up robots for indications of where it might be a good idea to place these lines.
Step 2
Switch over to a much harder brush, with an Opacity and Hardness of 100%. Starting with the face and hand, make those lines much crisper.
Continue to push forward by making some of those lines thicker. This is when we begin to fully develop the model as an actual robot.
The hands and neck, for instance, have components that are painted black to represent the spaces needed for those moving pieces. Try to understand the possible functions behind your details for a more believable result.
Use the Eraser Tool (E) to erase the parts of the gray sketch that are still showing through underneath. Concentrate on creating super clean lines to the best of your ability because this is a machine after all.
To create more of a robotic expression, fill in the eyes with some black as well, using the Brush Tool (B).
Here is the cyborg so far. Let’s move on to the background scene.
4. How to Build the Cyborg Background
Step 1
Use the Magic Wand Tool (W) to select the white background of the original reference and hit Delete on your keyboard to remove it.
Then Copy and Paste your Metal Texture onto a New Layer below the woman layer. Adjust the position so that the circular part fits behind the model’s head.
With the texture selected, go to Filter > Blur > Gaussian Blur and blur the background with a Radius of 5 pixels.
Step 2
Copy and Paste the Electric Circuit Stock onto a New Layer. Adjust the position so that the lines flow from behind her head, and set the Layer Blend Mode to Overlay.
Just like before, blur the circuit stock by going to Filter > Blur > Gaussian Blur, with a Radius of 5 pixels.
5. How to Add Details and Color to the Cyborg
Step 1
For a more cohesive look, let’s get rid of her blonde hair. Create a New Layer and select the Brush Tool (B). Set the Hardness to 50-80% and start to paint black all over the hair using the same brush as before. Set the Layer Blend Mode to Color Burn and adjust the Opacity to 87%.
Step 2
Let’s tackle the skin again. Create a New Layer and use a Soft Round Brush with 30-50% Opacity and 0% Hardness to soften her skin with more color. Hold the Alt key over her skin to set that color as your Foreground Color, and then begin painting with it in the nearby areas.
Continue to do this with the rest of her face and body. Imagine that her skin is made out of hard, reflective materials. Try to make her skin as silky as possible without losing too many details.
Step 3
Incorporate more technology. Use the Polygonal Lasso Tool (L) to cut out the USB portfrom its reference. Paste it onto the canvas on a New Layer and create two more copies. Position each port side by side and Merge the layers together.
Step 4
Let’s add color! Add a New Adjustment Layer of Color Lookup above all your other layers. Set the 3DLUT File to Crisp_Winter.look.
Next, add a Levels Adjustment Layer with the following settings for the RGB and Blue Channels to intensify the lighting and give a blue tint to the scene.
Add another Color Lookup Adjustment Layer. This time set the 3DLUT File to Crisp_Warm.look. Place it above the Levels layer for more contrast. Lower the Opacity to 70% and set the Blend Mode to Darken.
Things are getting pretty intense.
Step 5
Use the Rectangular Marquee Tool (M) to select an area of the Blue Gem Stock and Paste it onto a New Layer. Position the stone over the triangle and onto her forehead. Add a Layer Mask to the gem layer and paint black onto it to shape the stone into a triangle.
Set a New Layer as a Clipping Mask to the stone. Use a dark blue color #00197a to paint an inner shadow along the edges of the triangle.
Step 6
Fill in the rest of the metal details with solid color. Feel free to experiment with which colors you like best.
6. How to Finish the Cyborg Manipulation
Step 1
This next series of steps involves a lot of painting, so a graphics tablet is necessary. Dedicate New Layers to highlights and shadows.
Here’s a side-by-side look as the cyborg gets highlights and then shadows.
Step 2
Start using a Hard Round Brush with an Opacity of 80-100% to make your details crisper.
Notice that I’ve added light tan highlights #ece6f0 around the black robotic components. This will help give them more volume and sheen. Continue to evolve the shading of your cyborg by paying attention to the edges of the skin surface.
Paint darker skin tones, #6a4f65 particularly within the dark spaces of her fingers, to make them look as if there’s real thickness in the material used to create her. Continue this effect with the rest of the body, especially with her chest and arm.
Step 3
Add more character with unique details. Create bolts for her forehead, make the stone more 3D, and even draw the symbols for the USB port.
Step 4
Add a New Adjustment Layer of Curves. Adjust the curves for the RGB and BlueChannels to bump up the intensity of the lighting.
Let’s add some warmth. Follow up with a Color Lookup Adjustment Layer. Set the 3DLUT File to FallColors.look and lower the Opacity to 25%.
Step 5
Finish your manipulation by diving back into the details. Fix the neck and collar bone areas to give them more volume.
Switch over to a Hard Round Brush with 100% Hardness to clean up the edges of your manipulation.
I also elaborated on the robotic details found in the black spaces of her neck and fingers. Sometimes you can make up components like this as long as they generally make sense.
Step 6
Finish with one more layer of highlight. Set a New Layer to Overlay and use a light pink color #cfc0e1 to paint soft highlights onto her chest, shoulders, and hand. The arrows indicate the areas that were affected the most.
Check out the final result below!
Congratulations, You’ve Made It!
This tutorial requires you to put all your Photoshop knowledge to the test! By incorporating photo retouching, photo manipulation, and finally, digital painting techniques you can achieve an incredibly realistic human cyborg.
The key to this project is to stay patient so that you can fine tune the results as much as possible. Make your lines super sharp so that she appears more like a machine than a human.
I hope you’ve enjoyed this tutorial. Feel free to leave me any questions in the comments and post your results, no matter your skill level.
And tackle more exciting photo manipulations like this one by checking out some of my other tutorials below:
China’s first domestically designed large solar-powered unmanned plane reached above 20,000 meters in altitude on its test flight in the country’s northwest regions recently.
The drone was developed by the China Academy of Aerospace Aerodynamics (CAAA), it’s developers kept the exact size of the drone as a secret, but it is believed to be about 14 meters long with a 45 meter wingspan according to earlier prototypes.
I came across a couple suchanimations a while ago and this gave me the idea of creating my own versions with as little code as possible, no external libraries, using various methods, some of which take advantage of more recent features we can use these days, such as CSS variables. This article is going to guide you through the process of building these demos.
Before anything else, this is the animation we’re trying to achieve here:
The desired result: a rotating ☯ symbol, with its two lobes increasing and decreasing in size.
No matter what method we choose to use to recreate the above animation, we always start from the static yin and yang shape which looks as illustrated below:
First off, we have a big circle of diameter d. Inside this circle, we tightly fit two smaller circles, each one of them having a diameter that’s half the diameter of our initial big circle. This means that the diameter for each of these two smaller circles is equal to the big circle’s radius r (or .5*d). Inside each of these circles of diameter r we have an even smaller concentric circle. If we are to draw a diameter for the big circle that passes through all the central points of all these circles – the line segment AB in the illustration above, the intersections between it and the inner circles split it into 6 equal smaller segments. This means that the diameter of one of the smallest circles is r/3 (or d/6) and its radius is r/6.
Knowing all of this, let’s get started with the first method!
Plain old HTML + CSS
In this case, we can do it with one element and its two pseudo-elements. The how behind building the symbol is illustrated by the following animation (since the whole thing is going to rotate, it doesn’t matter if we switch axes):
The actual element is the big circle and it has a top to bottom gradient with a sharp transition right in the middle. The pseudo-elements are the smaller circles we place over it. The diameter of one of the smaller circles is half the diameter of the big circle. Both smaller circles are vertically middle-aligned with the big circle.
So let’s start writing the code that can achieve this!
First of all, we decide upon a diameter $d for the big circle. We use viewport units so that everything scales nicely on resize. We set this diameter value as its width and height, we make the element round with border-radius and we give it a top to bottom gradient background with a sharp transition from black to white in the middle.
Now let’s move on to the smaller circles which we create with pseudo-elements. We give our element display: flex and make its children (or pseudo-elements in our case) middle aligned with it vertically by setting align-items: center. We make these pseudo-elements have half the height (50%) of their parent element and make sure that, horizontally, they each cover half of the big circle. Finally, we make them round with border-radius, give them a dummy background and set the content property just so that we can see them:
.☯ {
display: flex;
align-items: center;
/* same styles as before */
&:before, &:after {
flex: 1;
height: 50%;
border-radius: 50%;
background: #f90;
content: '';
}
}
All that’s left to do before we get the static symbol is to give these two pseudo-elements borders. The black one should get a white border, while the white one should get a black border. These borders should be a third of the pseudo-element’s diameter, which is a third of half the diameter of the big circle – that gives us $d/6.
.☯ {
/* same styles as before */
&:before, &:after {
/* same styles as before */
border: solid $d/6 white;
}
&:after {
/* same styles as before */
border-color: black;
}
}
This is because, vertically, the border adds up to the height instead of being subtracted out of it. Horizontally, we haven’t set a width, so it gets subracted from the available space. We have two fixes possible here. One would be to set box-sizing: border-box on the pseudo-elements. The second one would be to change the height of the pseudo-elements to $d/6 – we’ll go with this one:
We now have the basic shape, so let’s move on to the animation! This animation involves going from the state where the first pseudo-element has shrunk to let’s say half its original size (which would mean a scaling factor $f of .5) while the second pseudo-element has expanded to take up all available space left – meaning to the diameter of the big circle (which is twice its original size) minus the diameter of the first circle (which is $f of its original size) to the state where the second pseudo-element has shrunk to $f of its original size and the first pseudo-element has expanded to 2 - $f of its original size. The first pseudo-element circle scales relative to its leftmost point (so we need to set a transform-origin of 0 50%), while the second one scales relative to its rightmost point (100% 50%).
$f: .5;
$t: 1s;
.☯ {
/* same styles as before */
&:before, &:after {
/* same styles as before */
transform-origin: 0 50%;
transform: scale($f);
animation: s $t ease-in-out infinite alternate;
}
&:after {
/* same styles as before */
transform-origin: 100% 50%;
animation-delay: -$t;
}
}
@keyframes s { to { transform: scale(2 - $f) } }
We now have the shape changing animation we’ve been after:
However, there’s still one more thing we can do to make the compiled CSS more efficient: eliminate redundancy with CSS variables!
white can be written in HSL format as hsl(0, 0%, 100%). The hue and the saturation don’t matter, any value that has the lightness 100% is white, so we just set them both to 0 to make our life easier. Similarly, black can be written as hsl(0, 0%, 0%). Again, the hue and saturation don’t matter, any value that has the lightness 0% is black. Given this, our code becomes:
the x component of our transform-origin is calc(0*100%) for the first pseudo-element and calc(1*100%) for the second one
our border-color is hsl(0, 0%, calc((1 - 0)*100%)) for the first pseudo-element and hsl(0, 0%, calc((1 - 1)*100%)) for the second one
our background is hsl(0, 0%, calc(0*100%)) for the first pseudo-element and hsl(0, 0%, calc(1*100%)) for the second one
our animation-delay is calc(0*#{-$t}) for the first pseudo-element and calc(1*#{-$t}) for the second one
This means we can use a custom property that acts as a switch and is 0 for the first pseudo-element and 1 for the second:
.☯ {
/* same styles as before */
&:before, &:after {
/* same styles as before */
--i: 0;
border: solid $d/6 hsl(0, 0%, calc((1 - var(--i))*100%));
transform-origin: calc(var(--i)*100%) 50%;
background: hsl(0, 0%, calc(var(--i)*100%));
animation: s $t ease-in-out calc(var(--i)*#{-$t}) infinite alternate;
}
&:after { --i: 1 }
}
This eliminates the need for witing all these rules twice: all we need to do now is flip the switch! Sadly, this only works in WebKit browsers for now because Firefox and Edge don’t support using calc() as an animation-delay value and Firefox doesn’t support using it inside hsl() either.
Canvas + JavaScript
While some people might think this method is overkill, I really like it because it requires about the same amount of code as the CSS one, it has good support and good performance.
We start with a canvas element and some basic styles just to put it in the middle of its container (which is the body element in our case) and make it visible. We also make it circular with border-radius so that we simplify our job when drawing on the canvas.
Alright, now let’s move on to the JavaScript part! Before anything else, we need to get the canvas element, the 2D context and set the canvas element’s width and height attributes (things we draw on the canvas would appear stretched otherwise). Then, we’re going to need to have a radius for our big circle. We get this radius to be half the computed size of the canvas element and, after we do that, we translate our context such that we bring the 0,0 point of our canvas dead in the middle (it’s originally in the top left corner). We make sure we recompute the radius and the width and height attributes on each resize because, in the CSS, we made the canvas dimensions depend on the viewport.
const _C = document.querySelector('canvas'),
CT = _C.getContext('2d');
let r;
function size() {
_C.width = _C.height = Math.round(_C.getBoundingClientRect().width);
r = .5*_C.width;
CT.translate(r, r);
};
size();
addEventListener('resize', size, false);
After we’ve done this, we can move on to drawing on the canvas. Draw what? Well, a shape made out of three arcs, as shown in the illustration below:
In order to draw an arc on a 2D canvas, we need to know a few things. First off, it’s the coordinates of the central point of the circle this arc belongs to. Then we need to know the radius of this circle and the angles (relative to the x axis of the local coordinate system of the circle) at which the start and end points of the arc are located. Finally, we need to know if we go from the start point to the end point clockwise or not (if we don’t specify this, the default is clockwise).
The first arc is on the big circle whose diameter is equal to the canvas dimensions and, since we’ve placed the 0,0 point of the canvas right in the middle of this circle, this means we know both the first set of coordinates (it’s 0,0) and the circle radius (it’s r). The start point of this arc is the leftmost point of this circle – this point is at -180° (or -π). The end point is the rightmost point of the circle, which is at 0° (also 0 in radians). If you need a refresher of angles on a circle, check out this helper demo.
This means we can create a path and add this arc to it and, in order to see what we have so far, we can close this path (which in this case means connecting the end point of our arc to the start point with a straight line) and fill it (using the default fill, which is black):
Now let’s move on to the second arc. The coordinates of the central point of the circle it’s on are .5*r,0 and its radius is .5*r (half the radius of the big circle). It goes from 0 to π, moving clockwise in doing so. So the arc we add to out path before closing it is:
Now we have one more arc left to add. The radius is the same as for the previous one (.5*r) and the first set of coordinates is -.5*r,0. This arc goes from 0 to -π and it’s the first arc not to go clockwise, so we need to change that flag:
Next, we’re going to add the black circle to this path. We’re not going to create another path because the aim is to group all shapes with the same fill into the same path for better performance. Calling fill() is expensive, so we don’t want to do it more often than we really need to.
A circle is just an arc from 0° to 360° (or from 0 to 2*π). The central point of this circle coincides to that for the last arc we’ve drawn (-.5*r, 0) and its radius is a third of that of the previous two arcs.
CT.arc(-.5*r, 0, .5*r/3, 0, 2*Math.PI);
Now we’re getting really close to having the full symbol:
All that’s left to do is create a white circle, symmetrical to the black one with respect to the y axis. This means need to switch to a white fill, start a new path and then add an arc to it using almost the same command we used to add the black circle shape – the only difference is that we reverse the sign of the x coordinate (this time, it’s +, not -). After that, we close that path and fill it.
For the animation, we want to go from the state where the first of the smaller arcs has shrunk to half is original radius (so we use a scaling factor F of .5) and the other one has expanded accordingly to the state where these initial radii are reversed.
In the initial state, given that the radius of the smaller arcs is initially .5*r, then the radius of the first of them after being scaled down by a factor F is r1 = F*.5*r. Since the radii of the smaller circles need to add up to the radius of the big circle r, we have that the radius of the second one of the smaller circles is r2 = r - r1 = r - F*.5*r.
In order to get the x coordinate of the origin of the first smaller arc for the initial state, we need to subtract its radius from the x coordinate of the point it starts at. This way, we get that this coordinate is r - r1 = r2. Similarly, in order to get the x coordinate of the origin of the second smaller arc, we need to add up its radius to the coordinate of the point it ends at. This way, we get that this coordinate is -r + r2 = -(r - r2) = -r1.
The initial vs. the final state of the animation (live demo).
For the final state, the values of the two radii are reversed. The second one is F*.5*r, while the first one is r - F*.5*r.
With every frame of our animation, we increase the current radius of the first smaller arc from the minimum value (F*.5*r) to the maximum value (r - F*.5*r) and then we start decreasing it to the minimum value and then the cycle repeats while also scaling the radius of the other smaller arc accordingly.
In order to do this, we first set the minimum and maximum radius in the size() function:
const F = .5;
let rmin, rmax;
function size() {
/* same as before */
rmin = F*.5*r;
rmax = r - rmin;
};
At any moment in time, the current radius of the first of the smaller arcs is k*rmin + (1 - k)*rmax, where this k factor keeps going from 1 to 0 and then back up to 1. This sounds similar to the cosine function on the [0, 360°] interval. At 0°, the value of the cosine is 1. Then it starts decreasing and it keeps doing so until it gets to 180°, when it reaches its minimum value of -1, after which the value of the function starts increasing again until it gets to 360°, where it’s again 1:
Alright, but the values of the cosine function are in the [-1, 1] interval and we need a function that gives us values in the [0, 1] interval. Well, if we add 1 to the cosine, then we shift the whole graph up and the values are now in the [0, 2] interval:
[0, 2] isn’t [0, 1], so what we still need to do here is divide the whole thing by 2 (or multiply it with .5, same thing). This squishes our graph to the desired interval.
Good, but what’s up with that angle? We don’t have an angle going from 0° to 360°. If we’re going to use requestAnimationFrame, we just have the number of the current frame, which starts at 0 and then keeps going up. Well, at the beginning, we set a total number of frames T for one animation cycle (the first arc going from the minimum radius value to the maximum radius value and then back again).
For every frame, we compute the ratio between the number of the current frame (t) and the total numeber of frames. For one cycle, this ratio goes from 0 to 1. If we multiply this ratio with 2*Math.PI (which is the same as 360°), then the result goes from 0 to 2*Math.PI over the course of a cycle. So this is going to be our angle.
const T = 120;
(function ani(t = 0) {
let k = .5*(1 + Math.cos(t/T*2*Math.PI)),
cr1 = k*rmin + (1 - k)*rmax, cr2 = r - cr1;
})();
The next step is to put inside this function the code that actually draws our symbol. The code for beginning, closing, filling paths, changing fills stays the same, as does the code needed for creating the big arc. The things that change are:
the radii of the smaller arcs – they’re cr1 and cr2 respectively
the x coordinates of the central points for the smaller arcs – they’re at cr2 and -cr1 respectively
the radii of the black and white circles – they’re cr2/3 and cr1/3 respectively
the x coordinates of the central points of these circles – they’re at -cr1 and cr2 respectively
Before we actually start animating the radii of the arcs, we still need to take care of a couple more things. First of all, if we start the animation right now, we’re just going to be drawing how the shape looks for each frame over what we’ve drawn for the previous frames, which is going to create one big mess. In order to avoid this, we need to clear the canvas for each frame, before drawing anything on it. What we clear is the visible part, which is inside the rectangle of canvas dimensions whose top left corner is at -r,-r:
CT.clearRect(-r, -r, _C.width, _C.width);
The second little problem we need to fix is that we’re switching to a white fill, but at the start of the next frame, we need a black one. So we need to make this switch for each frame before the beginning of the first path:
CT.fillStyle = 'black';
Now we can actually start the animation:
requestAnimationFrame(ani.bind(this, ++t));
This gives us the morphing animation, but we still need to rotate the whole thing. Before tackling that, let’s look at the formula for k once more:
let k = .5*(1 + Math.cos(t/T*2*Math.PI))
T and 2*Math.PI are constant throughout the animation, so we can just take that part out and store it as a constant angle A:
const T = 120, A = 2*Math.PI/T;
(function ani(t = 0) {
let k = .5*(1 + Math.cos(t*A));
/* same as before */
})();
Now for every frame, we can also rotate the context by A after clearing the canvas.
Not too exciting, so let’s move on to drawing something on the SVG canvas. Just like in the canvas case, we’ll be drawing a path made up of the same three arcs (the big one with a radius that’s half the size of the SVG viewBox and the two smaller ones with a radius that’s half of that of the big arc in the static case) and two small circles (with a radius that’s a third of that of the smaller arc they share their central point with).
So we start by picking a radius r value and using it to set the viewBox on the svg element:
- var r = 1500;
svg(viewBox=[-r, -r, 2*r, 2*r].join(' '))
The next step is to add the path made up of the three arcs. Creating a path in SVG is a bit different from canvas. Here, the shape is described by the path data d attribute, which, in our case, is made up of:
a „move to” (M) command after which we specify the coordinates of the start point of our path (also the start point of the big arc in this case)
an „arc to” (A) command for each of our arcs after which we describe our arcs; each of these arcs starts from the end point of the previous arc or, in the case of the first arc, from the start point of our path
Let’s take a closer look at the components of an „arc to” (A) command:
the radius of our arc along the x axis of its system of coordinates – this is equal to r in the case of the big arc and to .5*r in the case of the two smaller ones
the radius of our arc along the y axis of its system of coordinates – this is equal to the one along the x axis in the case of circular arcs as we have here (it’s only different for elliptical arcs, but that’s beyond the scope of this article)
the rotation of our arc’s system of coordinates – this only influences the arc’s shape in the case of elliptical arcs, so we can safely always take it 0 to simplify things for circular arcs
the large arc flag – this is 1 if our arc is greater than half a circle and 0 otherwise; since our arcs are exactly half a circle, they’re not greater than haf a circle, so this is always 0 in our case
the sweep flag – this is 1 if the arc goes clockwise between its start and its end point and 0 otherwise; in our case, the first two arcs go clockwise, while the third doesn’t, so the values we use for the three arcs are 1, 1 and 0
the x coordinate of the arc’s end point – this is something we need to determine for each arc
the y coordinate of the arc’s end point – also something we need to determine for each arc
At this point, we already know most of what we need. All we still have to figure out are the coordinates of the arcs’ endpoints. So let’s consider the following illustration:
The structure of the three arc shape with coordinates of arc endpoints (live demo).
From the illustration above we can see that the first arc (the big one) starts at (-r,0) and ends at (r,0), the second one ends at 0,0 and the third one ends at (-r,0) (also the start point of our path). Note that the y coordinates of all these points remain 0 even if the smaller arcs’ radii change, but the x coordinate of the second arc’s endpoint only happens to be 0 in this case when the radii of the smaller arcs are exactly half of the big one. In the general case, it’s r - 2*r1, where r1 is the radius of the second arc (the first of the smaller ones). This means we can now create our path:
Next, we’re going to animate the shape of our path and the size and position of our two small circles using JavaScript. This means that the first thing we do is get these elements, get the radius R of the big circle and set a scaling factor F that gives us the minimum radius (RMIN) down to which the arcs can be scaled. We also set a total number of frames (T) and a unit angle (A).
const _P = document.querySelector('path'),
_C = document.querySelectorAll('circle'),
_SVG = document.querySelector('svg'),
R = -1*_SVG.getAttribute('viewBox').split(' ')[0],
F = .25, RMIN = F*R, RMAX = R - RMIN,
T = 120, A = 2*Math.PI/T;
The animation function is pretty much the same as in the canvas case. The only thing that’s different is the fact that now, in order to change the path shape, we change its d attribute and, in order to change the small circles’ radii and positions, we change their r and cx attributes. But everything else works exactly the same way:
There’s one more method of doing this, although it involves changing things like the path data from CSS, which is something only Blink browsers support at this point (and they’re not even matching the latest spec).
It’s also a bit breakable because we need to have the same radius value both in the SVG viewBox attribute and as a Sass variable.
- var r = 1500;
svg(viewBox=[-r, -r, 2*r, 2*r].join(' '))
path
circle
circle
We could access the value of this radius from the CSS, but only as a custom property, if we were to do something like this:
- var r = 1500;
svg(viewBox=[-r, -r, 2*r, 2*r].join(' '))
style :root { --r: #{r} }
However, while this may be very helpful in some cases, it is useless here, as we currently have no way of putting CSS variables into the path data string we build with Sass. So we’re stuck with having to set the same value both in the viewBox attribute and in the Sass code.
The basic styles are the same and we can create the path data with Sass in a way that’s similar to the Pug method:
For the two small circles, we set their radii and positions along the x axis. We also need to make sure one of them is white:
circle {
r: $r1/3;
cx: $r2;
&:nth-child(2) { fill: white }
&:nth-child(3) {
r: $r2/3;
cx: -$r1
}
}
We now have the static shape:
The static yin and yang shape (live demo, Blink only).
In order to get the effect we’re after, we need the following animations:
a morphing animation of the path shape, where the radius of the first of the smaller arcs goes from the minimum possible radius ($rmin: .25*$r) to the maximum possible one ($rmax: $r - $rmin) and then back, while the radius of the last arc goes from $rmax to $rmin and back again; this can be done with a keyframe animation from one extreme to the other and then using the alternate value for animation-direction
another alternating animation that scales the radius of the first small circle from $rmin/3 up to $rmax/3 and then back down to $rmin/3 again; the second small circle uses the same animation only delayed by the value of a normal animation-duration
a third alternating animation that moves the central points of the two small circles back and forth; in the case of the first (white) small circle, it moves from $rmax down to $rmin; in the case of the second (black) circle, it goes from -$rmin down to -$rmax; what we can do here to unify them is use a CSS variable as a switch (it only works in WebKit browsers, but setting the path data or the circle radii or offsets from the CSS doesn’t have better support either)
So let’s first see the morphing @keyframes. These are created by setting pretty much the same path data as before, only replacing $r1 with $rmin and $r2 with $rmax for the 0% keyframe and the other way around for the 100% one:
The three arcs shape morphing (live demo, Blink only).
Next step is to move on to scaling and moving the two small circles. The scaling @keyframes follow the same pattern as the morphing ones. The radius value is $rmin/3 at 0% and $rmax/3 at 100%:
circle { animation: s $t ease-in-out infinite alternate }
And now the radii of the two small circles are animated:
The three arcs shape morphing and the small circles scaling (live demo, Blink only).
It’s a start, but we have a number of problems here. First of all, the second small circle should decrease in size when the first one is growing bigger and the other way around. We fix this by setting an animation-delay that depends on a CSS variable we initially set to 0 and then switch to 1 on the second small circle:
circle {
--i: 0;
animation: s $t ease-in-out calc(var(--i)*#{$t}) infinite alternate
&:nth-child(3) { --i: 1 }
}
As mentioned before, using calc() as an animation-delay value only works in WebKit browsers, but setting r from the CSS has even poorer support, so the animation-delay is not the biggest problem we have here. The result can be seen below:
The three arcs shape morphing and the small circles scaling (live demo, Blink only).
This is much better, but we still nedd to animate the positions of the small circles along the x axis. The way we do this is with a set of @keyframes that make cx go from $rmax to $rmin and back again for the first small circle and from -$rmin to -$rmax and back for the second one. In these two cases, we have both a different order and a different sign, so we need to come up with a keyframe animation that satisfies both.
Getting around the order problem is the easy part – we use the same animation-delay as we did for the scaling radii animation.
But what about the sign? Well, we use our custom property --i again. This is 0 for the first small circle and 1 for the second one, so we need a function that takes in --i and gives us 1 when this variable’s value is 0 and -1 for a value of 1. The simplest one that comes to mind is raising -1 to the power --i. Sadly, that’s not possible with CSS – we can only have arithmetic operations inside calc(). However, calc(1 - 2*var(--i)) is another solution that works and it’s not much more complicated. Using this, our code becomes:
What we have looks like a sudden flip at 50% in between the two end values, not a smooth animation. This is not what we wanted, so it looks like we need to abandon this tactic.
We have another option here though: combining cx with transform. The two small circles are always positioned such that the distance between their central points is $r. So what we can do is position the second of the small circles at -$r, then translate them both by a distance that’s between $rmax and $rmin:
One more thing we can do here to simplify the code is get rid of the initial $r1 and $r2 values and replace them with those in the 0% keyframe of each animation:
The final step is to make the SVG element itself rotate infinitely:
svg { animation: r 2*$t linear infinite }
@keyframes r { to { transform: rotate(1turn) } }
The finished loading animation can be seen in this Pen.
So there you have it – one loading animation, four different methods of recreating it from scratch for the web. Not everything we’ve explored in here is usable in practice today. For example, the support for the last method is really poor and the performance is awful. However, exploring the limits of what’s becoming possible these days was a fun exercise and a great learning opportunity.