


Getting Started with the Mojs Animation Library: Explore the Shapes Module
In the previous tutorial, we used mojs to animate different HTML elements on the web page. We use this library mainly to animate div
elements that look like squares or circles. However, you can use the Html
module to animate various elements such as images or titles. If you do intend to use mojs to animate basic shapes, then you should probably use the Shape module from the library.
Shape
module allows you to create basic shapes in the DOM using SVG. All you have to do is specify the type of shape you want to create and mojs takes care of the rest. This module also allows you to animate the different shapes you create.
In this tutorial we will cover the basics of the Shape
module and how to use it to create and animate different shapes.
Creating shapes in Mojs
You need to instantiate the mojs Shape
object to create different shapes. The object will accept different parameters that can be used to control the color, size, angle, etc. of the shape you create.
By default, any shape you create will use the document body as its parent. You can specify any other element as its parent using the parent
attribute. You can also assign a class to any shape you create with the help of the className
attribute. If you skip this property, the library will not assign any default class.
Mojs comes built-in with eight different shapes, so you can create them directly by setting a value for the shape
property. You can set its value to circle
to create a circle, rect
to create a rectangle, and polygon
to create a polygon. You can also draw straight lines by setting the value of shape
to lines
. If the shape
value is cross
, the library will draw two vertical lines; if the shape
value is equal
. Likewise, you can create zigzag lines by setting the property value to zigzag
.
Shape objects also have a points
attribute, which has different meanings for different shapes. It determines the total number of sides in the polygon
shape and the total number of parallel lines in the equal
shape. The points
property can also be used to set the zigzag
number of bends in the line.
As I mentioned before, mojs uses SVG to create all of these shapes. This means that Shape
objects will also have some SVG-specific properties to control the appearance of these shapes. You can set the fill color of a mojs shape using the fill
property. When no color is specified, the library will use the deepink
color to fill the shape. Likewise, you can specify the stroke color of a shape using the Stroke
property. When no stroke color is specified, mojs keeps the stroke transparent. You can control a shape's fill and stroke opacity using the fillOpacity
and StrokeOpacity
properties. They can be any value between 0 and 1.
Mojs also allows you to control other stroke-related properties of the shape. For example, you can use the StrokeDasharray
property to specify a pattern of dashes and gaps in a stroke path. This property accepts strings and numbers as valid values. Its default value is zero, which means the stroke will be a solid line. The width of the stroke can be specified using the StrokeWidth
property. By default, all strokes are 2px wide. You can specify the shape at different line endpoints using the StrokeLinecap
property. Valid values for StrokeLinecap
are butt
, round
, and square
.
By default, any shape you create will be placed in the center of its parent element. This is because the shape's left
and right
properties are both set to 50%. You can change the values of these properties to place elements in different locations. Another way to control the position of a shape is with the help of the x
and y
properties. They determine how much the shape should move horizontally and vertically, respectively.
You can specify the radius of a shape using the radius
property. This value is used to determine the size of a specific shape. You can also specify the size of a shape in a specific direction using radiusX
and radiusY
. Another way to control the size of a shape is with the help of the scale
property. The default value for scale
is 1, but you can set it to any other number you like. You can also use the scaleX
and scaleY
properties to scale a shape in a specific direction.
By default, the origin of all these transformations of a shape is its center. For example, if you rotate any shape by specifying a value for the angle
property, the shape will be rotated about its center. If you want to rotate the shape around another point, you can specify it using the origin
property. This property accepts a string as its value. Setting this to '0% 0%'
will rotate, scale, or translate the shape around its upper left corner. Likewise, setting it to '50% 0%'
will rotate, scale, or translate the shape around the center of its top edge.
You can use all of these properties we just discussed to create a variety of shapes. Here are some examples:
var circleA = new mojs.Shape({ parent: ".container", shape: "circle", left: 0, fill: "orange", stroke: "black", strokeWidth: 5, isShowStart: true }); var circleB = new mojs.Shape({ parent: ".container", shape: "circle", left: 175, fill: "orange", stroke: "red", radiusX: 80, strokeWidth: 25, strokeDasharray: 2, isShowStart: true }); var rectA = new mojs.Shape({ parent: ".container", shape: "rect", left: 350, fill: "red", stroke: "black", strokeWidth: 5, isShowStart: true }); var rectB = new mojs.Shape({ parent: ".container", shape: "rect", left: 500, fill: "blue", stroke: "blue", radiusX: 40, radiusY: 100, strokeWidth: 25, strokeDasharray: 20, isShowStart: true }); var polyA = new mojs.Shape({ parent: ".container", shape: "polygon", top: 300, left: 0, fill: "yellow", stroke: "black", strokeWidth: 10, points: 8, isShowStart: true }); var polyB = new mojs.Shape({ parent: ".container", shape: "polygon", top: 350, left: 175, radiusX: 100, radiusY: 100, fill: "violet", stroke: "black", strokeWidth: 6, strokeDasharray: "15, 10, 5, 10", strokeLinecap: "round", points: 3, isShowStart: true }); var lineA = new mojs.Shape({ parent: ".container", shape: "cross", top: 350, left: 375, stroke: "red", strokeWidth: 40, isShowStart: true }); var zigzagA = new mojs.Shape({ parent: ".container", shape: "zigzag", top: 500, left: 50, fill: "transparent", stroke: "black", strokeWidth: 4, radiusX: 100, points: 10, isShowStart: true }); var zigzagB = new mojs.Shape({ parent: ".container", shape: "zigzag", top: 500, left: 350, fill: "blue", stroke: "transparent", radiusX: 100, points: 50, isShowStart: true });
The shape created by the above code is shown in the CodePen demo below:
Animate shapes in Mojs
You can animate almost any property of the shape we discussed in the previous section. For example, you can animate the number of points in a polygon by specifying different initial and final values. You can also change the shape's origin from '50% 50%'
to any other value, such as '75% 75%'
. Other properties such as angle
and scale
behave the same as in the Html
module.
The duration, delay and speed of different animations can be controlled using the duration
, delay
and speed
properties respectively. The Repeat attribute also works the same way as in the Html
module. If you only want the animation to play once, you can set it to 0. Likewise, you can set it to 3 to play the animation 4 times. All easing values that are valid for the Html
module are also valid for the Shape
module.
The only difference between the animation capabilities of these two modules is that you cannot specify animation parameters individually for properties in the Shape
module. All properties you animate will have the same duration, delay, repeat count, etc.
Here is an example where we animate the x position, scale and angle of a circle:
var circleA = new mojs.Shape({ parent: ".container", shape: "circle", left: 175, fill: "red", stroke: "black", strokeWidth: 100, strokeDasharray: 18, isShowStart: true, x: { 0: 300 }, angle: { 0: 360 }, scale: { 0.5: 1.5 }, duration: 1000, repeat: 10, isYoyo: true, easing: "quad.in" });
One way to control the playback of different animations is to use the .then()
method to specify a new set of properties to animate after the first animation sequence has completely completed. You can specify new initial and final values for all animated properties in .then()
. Here is an example:
var polyA = new mojs.Shape({ parent: ".container", shape: "polygon", fill: "red", stroke: "black", isShowStart: true, points: 4, left: 100, x: { 0: 500 }, strokeWidth: { 5: 2 }, duration: 2000, easing: 'elastic.in' }).then({ strokeWidth: 5, points: { 4: 3 }, angle: { 0: 720 }, scale: { 1: 2 }, fill: { 'red': 'yellow' }, duration: 1000, delay: 200, easing: 'elastic.out' });
Final Thoughts
In this tutorial, we learned how to create different shapes using mojs and how to animate the properties of these shapes.
TheShape
module has all the animation capabilities of the Html
module. The only difference is that properties cannot be animated individually. They can only be animated as a group. You can also control animation playback by using different methods to play, pause, stop, and resume the animation at any time. I detailed these methods in the first tutorial of this series.
If you have any questions about this tutorial, please feel free to leave a comment. In the next tutorial you will learn about the ShapeSwirl
and stagger
modules in mojs.
The above is the detailed content of Getting Started with the Mojs Animation Library: Explore the Shapes Module. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.
