SlideShare a Scribd company logo
1 of 34
Creating 3D cube with pure CSS3
By
Gaurav Khurana
email at khurana.g@hotmail.com 1
Getting Started
• Let me start with a markup.
• We need a container (we will require another parent
container which I will cover shortly)
• And each div for all sides (front, back, left, right, top,
bottom)
email at khurana.g@hotmail.com 2
Explaining Markup
• We require container which will act as a cube so that
we can rotate it. I will call it cube here on.
• We would require transition property on this cube to
give some animation.
• Moreover, we need to give transform-style :
preserve-3d I will explain later on why we need this.
• For each sides of the cube we have each container
with respective class “front, back, left, right, top,
bottom” respectively.
email at khurana.g@hotmail.com 3
Lets start with our CSS now
• I will be inserting images to all faces/sides of
cubes and they are of dimension 154 x 154px.
• We will give width and height to cube as
154px.
• Each cube face will have width and height
inherit.
• We will now give position relative to cube and
position absolute to each side/face so they
stack upon each other at one place.
email at khurana.g@hotmail.com 4
CSS code
email at khurana.g@hotmail.com 5
CSS code for each face/side
email at khurana.g@hotmail.com 6
Result so far
• The image you are seeing is of last face/side
i.e. bottom in our case image name “6.jpg”.
email at khurana.g@hotmail.com 7
My
beautiful
girlfriend
Lets focus back on code
• Lets add some 3d concept now.
• To bring the cube closer to eyes we translate it
to Z-axis.
email at khurana.g@hotmail.com 8
Result
Not much of a difference? lets see why?
email at khurana.g@hotmail.com 9
Perspective in CSS
• To activate 3D space, an element needs
perspective. This can be achieved by two
ways.
– Using transform property with the perspective as
functional notation.
– Using perspective property.
email at khurana.g@hotmail.com 10
Understanding Perspective
email at khurana.g@hotmail.com 11
Understanding Perspective
email at khurana.g@hotmail.com 12
Result of both ways
email at khurana.g@hotmail.com 13
Perspective
property
applied on
parent
Perspective
Functional
notation
applied on
element
Lets see the difference
• These two formats both trigger a 3-D space, but
there is a difference.
• First functional notation is conventional way of
applying 3-D transform on a single element.
• But when applied on multiple element transformed
element don’t line up as expected. Lets see the
difference
email at khurana.g@hotmail.com 14
Perspective: Property vs Functional
Notation
email at khurana.g@hotmail.com 15
Perspective
Functional
notation to each
element
Perspective
property to parent
element
Something important about
Perspective
• The value of perspective determines the intensity of
3-D effect.
• Think of it as a distance from the viewer to the
object.
• The greater the value, the further the distance, so
the less intense the visual effect. perspective: 2000;
yields a subtle 3-D effect, as if we were viewing an
object from far away. perspective: 100; produces a
tremendous 3-D effect, like a tiny insect viewing a
massive object.
email at khurana.g@hotmail.com 16
Lets get back to our code
• So as we observed it is good idea to use perspective
property on parent element when dealing with
multiple child element for 3-D transform.
email at khurana.g@hotmail.com 17
Result
• No change
email at khurana.g@hotmail.com 18
Looks like she
wants front seat.
Girls I tell you
how adamant
they are 
Why no change?
• Even after we give perspective to our scene, we still
have an issue. The front face should ideally appear
above all other faces, hiding them behind it. But it's
not.
• When we create html element z-index get associated
with each element and the stacking order determine
least element should be given higher z-index.
email at khurana.g@hotmail.com 19
Lets fix this now
• Without an element having the transform-style
property set as preserve-3d, its children are rendered
as flattened, having no stacking context.
• Thus even when we bought the front face closer on
Z-axis, it continued to render it at its original z-index
with no consideration to its position in the 3D space.
email at khurana.g@hotmail.com 20
Result
• No change
email at khurana.g@hotmail.com 21
GOD
You gotta be
kidding me
What’s wrong now?
• Well don’t worry we have our 3-D system setup now.
Lets work on positioning.
• One face at a time. Lets begin with front face.
email at khurana.g@hotmail.com 22
Explaining cordinates
• As you can see, the Z-axis
is coming out of the
screen straight from the
element.
• Hence, when we translate
the front face positively
on the Z-axis, it appears
closer to us.
• A point to note here is
that this coordinate
system is local to this
element. Let's look at that
more closely.
email at khurana.g@hotmail.com 23
• We'll use our front face
again and rotate it a bit
about its Y-axis.
• As you see axis rotated
along with element.
• Z-axis no longer coming
straight towards us
email at khurana.g@hotmail.com 24
Lets understand one more face/side
email at khurana.g@hotmail.com 25
Left face now Left face after rotation
90 deg (vertically so
rotate Y)
Our position is complete now
Nothing to
rotate here.
Simple move
the front face
forward by half
the cube's
length.
email at khurana.g@hotmail.com 26
Result
email at khurana.g@hotmail.com 27
Finally, but
she still hold
on to front by
sharing the
seat :D
Lets add a logic to rotate
• We will use arrow keys to rotate our cube.
email at khurana.g@hotmail.com 28
Now use left arrow key to rotate left
• Notice Something wrong?
• When we press left arrow key,
we should see right face in front.
• We do see it but it is in smaller size.
email at khurana.g@hotmail.com 29
Fixing Perspective.
• If you remember we gave perspective property to
cube container.
• And when we rotated that element just now, the
perspective marked by the vanishing point also
rotated along with it.
• So the vanishing point which was initially somewhere
behind the 2D place came in front of the 2D plane
after the rotation, causing the issue.
email at khurana.g@hotmail.com 30
What we want?
• What we ideally want is that the perspective never
changes and remains constant no matter what
element we transform.
• How do we fix this? We wrap all our elements with
another DIV to which give the perspective property:
email at khurana.g@hotmail.com 31
Lets get back to our code
• So as we observed it is good idea to use perspective
property on parent element when dealing with
multiple child element for 3-D transform.
email at khurana.g@hotmail.com 32
Added parent to Markup
Added css perspective to parent
Result
email at khurana.g@hotmail.com 33
Need code?
Link to code
https://drive.google.com/file/d/0B2qTHtNDrsw
mbDJrR3R6Nkx0dU0/view?usp=sharing
email at khurana.g@hotmail.com 34

More Related Content

Viewers also liked

Understanding ECMA Script 6 Javascript by Gaurav Khurana
Understanding ECMA Script 6 Javascript by Gaurav KhuranaUnderstanding ECMA Script 6 Javascript by Gaurav Khurana
Understanding ECMA Script 6 Javascript by Gaurav KhuranaGaurav Khurana
 
Geographic information - standards available for describing geographical data
Geographic information - standards available for describing geographical dataGeographic information - standards available for describing geographical data
Geographic information - standards available for describing geographical dataAna Roxin
 
Legacy SAN Technologies Reference Manual
Legacy SAN Technologies Reference Manual   Legacy SAN Technologies Reference Manual
Legacy SAN Technologies Reference Manual EMC
 
Latihan Soal Bilangan Bulat
Latihan  Soal Bilangan BulatLatihan  Soal Bilangan Bulat
Latihan Soal Bilangan BulatAndike96
 

Viewers also liked (10)

Understanding ECMA Script 6 Javascript by Gaurav Khurana
Understanding ECMA Script 6 Javascript by Gaurav KhuranaUnderstanding ECMA Script 6 Javascript by Gaurav Khurana
Understanding ECMA Script 6 Javascript by Gaurav Khurana
 
Armin_Amiressami_Recommendation_Letter
Armin_Amiressami_Recommendation_LetterArmin_Amiressami_Recommendation_Letter
Armin_Amiressami_Recommendation_Letter
 
Web 2.0
Web 2.0Web 2.0
Web 2.0
 
Picasso 2
Picasso 2Picasso 2
Picasso 2
 
Blanca Navidad
Blanca NavidadBlanca Navidad
Blanca Navidad
 
Voyage2
Voyage2Voyage2
Voyage2
 
Geographic information - standards available for describing geographical data
Geographic information - standards available for describing geographical dataGeographic information - standards available for describing geographical data
Geographic information - standards available for describing geographical data
 
Legacy SAN Technologies Reference Manual
Legacy SAN Technologies Reference Manual   Legacy SAN Technologies Reference Manual
Legacy SAN Technologies Reference Manual
 
COLD bro v13
COLD bro v13COLD bro v13
COLD bro v13
 
Latihan Soal Bilangan Bulat
Latihan  Soal Bilangan BulatLatihan  Soal Bilangan Bulat
Latihan Soal Bilangan Bulat
 

Similar to 3d cubes By Gaurav Khurana using CSS3

Tutorial two cubes falling on a floor due to gravity
Tutorial two cubes falling on a floor due to gravityTutorial two cubes falling on a floor due to gravity
Tutorial two cubes falling on a floor due to gravitySMARTLab123
 
2D Art For Realtime 3D Web Game
2D Art For Realtime 3D Web Game 2D Art For Realtime 3D Web Game
2D Art For Realtime 3D Web Game we20
 
2d art for realtime 3d web game
2d art for realtime 3d web game2d art for realtime 3d web game
2d art for realtime 3d web gameSon Aris
 
2D art for real time 3D web game
2D art for real time 3D web game2D art for real time 3D web game
2D art for real time 3D web gameaction.vn
 
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc Vu
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc VuOGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc Vu
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc VuBuff Nguyen
 
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014Mary Chan
 
Make Your Animations Perform Well - JS Conf Budapest 2017
Make Your Animations Perform Well - JS Conf Budapest 2017 Make Your Animations Perform Well - JS Conf Budapest 2017
Make Your Animations Perform Well - JS Conf Budapest 2017 Anna Migas
 
Screen space reflections on Epsilon Engine
Screen space reflections on Epsilon EngineScreen space reflections on Epsilon Engine
Screen space reflections on Epsilon EngineImanol Fotia
 
EN The Distance Formula by Slidesgo.pptx
EN The Distance Formula by Slidesgo.pptxEN The Distance Formula by Slidesgo.pptx
EN The Distance Formula by Slidesgo.pptxSaurabhDahiya14
 
Coordinate geometry.pptx
Coordinate geometry.pptxCoordinate geometry.pptx
Coordinate geometry.pptxRaunakShah18
 
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...Daniel Barrero
 
Blender and Bezier Curves for 3D Printing
Blender and Bezier Curves for 3D PrintingBlender and Bezier Curves for 3D Printing
Blender and Bezier Curves for 3D PrintingVickyTGAW
 
Request for small research project
Request for small research projectRequest for small research project
Request for small research projectAlbert Poghosyan
 

Similar to 3d cubes By Gaurav Khurana using CSS3 (16)

Tutorial two cubes falling on a floor due to gravity
Tutorial two cubes falling on a floor due to gravityTutorial two cubes falling on a floor due to gravity
Tutorial two cubes falling on a floor due to gravity
 
2D Art For Realtime 3D Web Game
2D Art For Realtime 3D Web Game 2D Art For Realtime 3D Web Game
2D Art For Realtime 3D Web Game
 
2d art for realtime 3d web game
2d art for realtime 3d web game2d art for realtime 3d web game
2d art for realtime 3d web game
 
2D art for real time 3D web game
2D art for real time 3D web game2D art for real time 3D web game
2D art for real time 3D web game
 
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc Vu
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc VuOGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc Vu
OGDC2012 2D Art For Realtime 3D Web Game_Mr. Khanh, Pham Ngoc Vu
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
 
Chapter10
Chapter10Chapter10
Chapter10
 
Make Your Animations Perform Well - JS Conf Budapest 2017
Make Your Animations Perform Well - JS Conf Budapest 2017 Make Your Animations Perform Well - JS Conf Budapest 2017
Make Your Animations Perform Well - JS Conf Budapest 2017
 
Screen space reflections on Epsilon Engine
Screen space reflections on Epsilon EngineScreen space reflections on Epsilon Engine
Screen space reflections on Epsilon Engine
 
EN The Distance Formula by Slidesgo.pptx
EN The Distance Formula by Slidesgo.pptxEN The Distance Formula by Slidesgo.pptx
EN The Distance Formula by Slidesgo.pptx
 
Coordinate geometry.pptx
Coordinate geometry.pptxCoordinate geometry.pptx
Coordinate geometry.pptx
 
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
 
Blender and Bezier Curves for 3D Printing
Blender and Bezier Curves for 3D PrintingBlender and Bezier Curves for 3D Printing
Blender and Bezier Curves for 3D Printing
 
Request for small research project
Request for small research projectRequest for small research project
Request for small research project
 
03 unity 3_d_part_2
03 unity 3_d_part_203 unity 3_d_part_2
03 unity 3_d_part_2
 

Recently uploaded

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Recently uploaded (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

3d cubes By Gaurav Khurana using CSS3

  • 1. Creating 3D cube with pure CSS3 By Gaurav Khurana email at khurana.g@hotmail.com 1
  • 2. Getting Started • Let me start with a markup. • We need a container (we will require another parent container which I will cover shortly) • And each div for all sides (front, back, left, right, top, bottom) email at khurana.g@hotmail.com 2
  • 3. Explaining Markup • We require container which will act as a cube so that we can rotate it. I will call it cube here on. • We would require transition property on this cube to give some animation. • Moreover, we need to give transform-style : preserve-3d I will explain later on why we need this. • For each sides of the cube we have each container with respective class “front, back, left, right, top, bottom” respectively. email at khurana.g@hotmail.com 3
  • 4. Lets start with our CSS now • I will be inserting images to all faces/sides of cubes and they are of dimension 154 x 154px. • We will give width and height to cube as 154px. • Each cube face will have width and height inherit. • We will now give position relative to cube and position absolute to each side/face so they stack upon each other at one place. email at khurana.g@hotmail.com 4
  • 5. CSS code email at khurana.g@hotmail.com 5
  • 6. CSS code for each face/side email at khurana.g@hotmail.com 6
  • 7. Result so far • The image you are seeing is of last face/side i.e. bottom in our case image name “6.jpg”. email at khurana.g@hotmail.com 7 My beautiful girlfriend
  • 8. Lets focus back on code • Lets add some 3d concept now. • To bring the cube closer to eyes we translate it to Z-axis. email at khurana.g@hotmail.com 8
  • 9. Result Not much of a difference? lets see why? email at khurana.g@hotmail.com 9
  • 10. Perspective in CSS • To activate 3D space, an element needs perspective. This can be achieved by two ways. – Using transform property with the perspective as functional notation. – Using perspective property. email at khurana.g@hotmail.com 10
  • 11. Understanding Perspective email at khurana.g@hotmail.com 11
  • 12. Understanding Perspective email at khurana.g@hotmail.com 12
  • 13. Result of both ways email at khurana.g@hotmail.com 13 Perspective property applied on parent Perspective Functional notation applied on element
  • 14. Lets see the difference • These two formats both trigger a 3-D space, but there is a difference. • First functional notation is conventional way of applying 3-D transform on a single element. • But when applied on multiple element transformed element don’t line up as expected. Lets see the difference email at khurana.g@hotmail.com 14
  • 15. Perspective: Property vs Functional Notation email at khurana.g@hotmail.com 15 Perspective Functional notation to each element Perspective property to parent element
  • 16. Something important about Perspective • The value of perspective determines the intensity of 3-D effect. • Think of it as a distance from the viewer to the object. • The greater the value, the further the distance, so the less intense the visual effect. perspective: 2000; yields a subtle 3-D effect, as if we were viewing an object from far away. perspective: 100; produces a tremendous 3-D effect, like a tiny insect viewing a massive object. email at khurana.g@hotmail.com 16
  • 17. Lets get back to our code • So as we observed it is good idea to use perspective property on parent element when dealing with multiple child element for 3-D transform. email at khurana.g@hotmail.com 17
  • 18. Result • No change email at khurana.g@hotmail.com 18 Looks like she wants front seat. Girls I tell you how adamant they are 
  • 19. Why no change? • Even after we give perspective to our scene, we still have an issue. The front face should ideally appear above all other faces, hiding them behind it. But it's not. • When we create html element z-index get associated with each element and the stacking order determine least element should be given higher z-index. email at khurana.g@hotmail.com 19
  • 20. Lets fix this now • Without an element having the transform-style property set as preserve-3d, its children are rendered as flattened, having no stacking context. • Thus even when we bought the front face closer on Z-axis, it continued to render it at its original z-index with no consideration to its position in the 3D space. email at khurana.g@hotmail.com 20
  • 21. Result • No change email at khurana.g@hotmail.com 21 GOD You gotta be kidding me
  • 22. What’s wrong now? • Well don’t worry we have our 3-D system setup now. Lets work on positioning. • One face at a time. Lets begin with front face. email at khurana.g@hotmail.com 22
  • 23. Explaining cordinates • As you can see, the Z-axis is coming out of the screen straight from the element. • Hence, when we translate the front face positively on the Z-axis, it appears closer to us. • A point to note here is that this coordinate system is local to this element. Let's look at that more closely. email at khurana.g@hotmail.com 23
  • 24. • We'll use our front face again and rotate it a bit about its Y-axis. • As you see axis rotated along with element. • Z-axis no longer coming straight towards us email at khurana.g@hotmail.com 24
  • 25. Lets understand one more face/side email at khurana.g@hotmail.com 25 Left face now Left face after rotation 90 deg (vertically so rotate Y)
  • 26. Our position is complete now Nothing to rotate here. Simple move the front face forward by half the cube's length. email at khurana.g@hotmail.com 26
  • 27. Result email at khurana.g@hotmail.com 27 Finally, but she still hold on to front by sharing the seat :D
  • 28. Lets add a logic to rotate • We will use arrow keys to rotate our cube. email at khurana.g@hotmail.com 28
  • 29. Now use left arrow key to rotate left • Notice Something wrong? • When we press left arrow key, we should see right face in front. • We do see it but it is in smaller size. email at khurana.g@hotmail.com 29
  • 30. Fixing Perspective. • If you remember we gave perspective property to cube container. • And when we rotated that element just now, the perspective marked by the vanishing point also rotated along with it. • So the vanishing point which was initially somewhere behind the 2D place came in front of the 2D plane after the rotation, causing the issue. email at khurana.g@hotmail.com 30
  • 31. What we want? • What we ideally want is that the perspective never changes and remains constant no matter what element we transform. • How do we fix this? We wrap all our elements with another DIV to which give the perspective property: email at khurana.g@hotmail.com 31
  • 32. Lets get back to our code • So as we observed it is good idea to use perspective property on parent element when dealing with multiple child element for 3-D transform. email at khurana.g@hotmail.com 32 Added parent to Markup Added css perspective to parent