Enchanting Hexagons: A Dazzling Display of Mesmerizing Animations with HTML, CSS & JS
Stelvin Saji
?? Author of Expressive Emojis ???. Curator of 'Smart Coding' with 55K+ subscribers ????. Front-end developer crafting responsive designs with JavaScript ??. Using React & Redux ????, Firebase ??, and Tailwind CSS ????.
??? Get ready to dive into a world of enchantment as we embark on a mesmerizing journey of creating Enchanting Hexagons! ??? Brace yourself for a dazzling display of captivating animations that will leave you spellbound. ??
? In this magical tutorial, we'll weave the spell using the potent combination of HTML, CSS, and JavaScript. ??? Get ready to witness hexagons come alive with charm and charisma! ???? Let's infuse our code with magic and create a truly enchanting web experience! ???
HTML Code :
??? Hexagonal Harmony: Unlocking the Enchantment of Hexagons ???
Let's embark on a journey where the bedrock of our creation lies in the robust structure of HTML. Picture this foundation as the canvas on which we'll paint our digital masterpiece. Now, enters CSS, the virtuoso responsible for adding a touch of glamour.
Yet, the true enchantment comes alive through the wizardry of JavaScript. Here, the code transcends mere instructions, transforming into a magical force that breathes life into our creation.
The output of the HTML Code :
???We are now poised to enhance the visual appeal of the webpage by incorporating some CSS magic that will truly make it sparkle! ??? To elevate the aesthetics and create a professional touch, we'll infuse the page with a touch of style. Let's embark on this journey to elevate our web presence to new heights! ????
CSS Code :
???? CSS Aesthetics: Elevating Web Design with Style & Charm! ???
Elevate your web presentation to new heights with a design that effortlessly commands attention on any device! Bid farewell to awkward gaps as your HTML and body elements seamlessly embrace 100% of the viewport height. Embrace a flawless aesthetic by setting margins to 0, ensuring your content captivates without distraction.
Ditch the scrolling dilemmas with a touch of overflow: hidden, ensuring your content stays polished and perfectly contained. Say hello to a sleek user experience where every element takes center stage, unencumbered by unwanted spacing.
And the grand finale? Picture a backdrop of sophistication in a deep, customizable black. Tailor the RGB values to your brand's essence, crafting a visually stunning canvas that not only complements but enhances your content.
The output of the CSS Code :
All right, buckle up! ?? Time to inject some JavaScript magic into the mix and bring our animation to life! ? Get ready for a visual feast as we unleash the power of code to make things move and groove on your screen. ?? Let the animation extravaganza begin! ????
JS Code :
???? Canvas Magic: Understanding the Core of HTML5 Graphics ???
The first line establishes a connection to an HTML <canvas> element using document.querySelector('canvas'). This line is essentially grabbing the canvas element from the HTML document and assigning it to the variable cnv.
The second line initiates a 2D rendering context with getContext('2d'). The ctx variable now holds the drawing functions and properties needed to manipulate the canvas. It's like having a powerful paintbrush ready to create graphics on our canvas.
The next three lines (let cw, ch, cx, cy) declare variables for the canvas width (cw), canvas height (ch), canvas center x-coordinate (cx), and canvas center y-coordinate (cy). These will prove crucial for positioning and sizing elements within the canvas.
?? Understanding the Canvas Coordinate System ???
The canvas, like any artist's canvas, has its coordinate system. The top-left corner is the origin (0,0), with positive x extending to the right and positive y extending downward. The cx and cy variables will help us find the center of the canvas.
??? Adapting to Change: Dynamic Canvas Resizing in Action ??????
The function resizeCanvas serves as the architect behind the canvas's ability to adjust to varying window sizes. Here's a breakdown of its actions:
The resizeCanvas() function is called immediately when the script runs, ensuring the canvas is appropriately sized right from the start.
The window.addEventListener('resize', resizeCanvas) line ensures that every time the user resizes the browser window, the resizeCanvas function is called again. This responsiveness guarantees that the canvas dynamically adjusts to the new window dimensions, providing a seamless and visually pleasing user experience.
??? Why Dynamic Canvas Resizing Matters ??
????? Palette of Hexagons: Hexagon Animation Configurations ????
We'll start by dissecting a configuration object that serves as the mastermind behind the mesmerizing visual experience.
?? Unleashing Creativity ???
领英推荐
This configuration object encapsulates the essence of a visually striking hexagon animation. Tinkering with these parameters opens the door to a myriad of artistic possibilities, allowing developers and artists alike to shape and mold their visual masterpieces.
?????? Brushing the Canvas: The DrawHexagon Function Unveiled ?????
Here, we're dissecting a function that acts as the artisan's brush, painting the canvas with hexagonal strokes of creativity. Behold the drawHexagon function, a key player in crafting the mesmerizing hexagon animation.
?? The Canvas Comes Alive ??
As we witness the drawHexagon function in action, we gain insight into the meticulous craftsmanship behind hexagon animations. This function serves as the brushstroke that paints the canvas with vibrant colors, shadows, and intricate patterns.
????Choreography of Hexagons: The Dance of the Hexagon Class ????
Here, we're unraveling the dance of hexagons orchestrated by the Hexagon class, a pivotal piece in the canvas performance.
? The Hexagon Ballet ??
The Hexagon class exemplifies the dynamic and ever-changing nature of web artistry. Each instance is a dancer, gracefully moving across the canvas, changing directions, and, in a poetic finale, exiting the stage.
?????Orchestrating the Ensemble: Generating Directions & Hexagons????
Now we're unraveling the behind-the-scenes script that populates the stage with directions and brings forth the ensemble of hexagons.
??? Generating Directions:
?? Bringing Forth the Hexagon Ensemble:
?? The Choreography Continues ??
These lines of code contribute to the dynamic and evolving nature of the canvas performance. This dirsList ensures hexagons have predefined directions to follow, creating a cohesive movement pattern. The addHexagon function, with its randomness, adds an element of spontaneity to the ensemble, ensuring that each hexagon enters the stage at its own pace.
???? Enlivening the Canvas: The Rhythmic Pulse of refreshHexagons ???
Now we're gonna implement a key function that orchestrates the lively rhythm of the hexagon ensemble on the canvas. Let's explore the dance moves encapsulated in the refreshHexagons function.
?? The Dance of Hexagons Continues ??
The refreshHexagons function serves as the heartbeat of the canvas, ensuring each hexagon plays its part in the synchronized dance. Redrawing, moving, changing direction, and assessing vitality, these actions harmonize to create a dynamic and visually captivating spectacle.
???? The Eternal Ballet: Initiating the Canvas Loop ????
We conclude our exploration by uncovering the mesmerizing canvas loop that orchestrates the perpetual dance of hexagons. Behold the enchanting loop function.
?? The Eternal Ballet Continues ??
The loop function embodies the heartbeat of your canvas, orchestrating the ongoing performance of hexagons. With each frame, the canvas is refreshed, new hexagons join the dance, and the existing ones gracefully evolve.
Final Output :
Follow for more content ??
????????????????????????????????????
Join the Discord Server ??
?? Discord: Click Here!
Aspiring Web developer
10 个月Woow....... It's amazing.....Thanks for sharing Stelvin Saji. Do you upload these codes on github ??
Pre-Finalite at MITS-DU Gwalior
10 个月just looking like a wow!!!