
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to draw on scroll using JavaScript and SVG?
To draw on scroll using JavaScript and SVG, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { height: 2000px; background: #f1f1f1; } svg { position: fixed; top: 15%; width: 400px; height: 210px; margin-left: -50px; } </style> </head> <body> <h1>Scroll Using JavaScript and SVG example</h1> <svg> <path fill="none" stroke="purple" stroke-width="5" id="polygon" d="M 150 350 Q 150 50 250 150 Q 250 550 300 150 Q 350 50 400 300"/> </svg> <script> var polygon = document.getElementById("polygon"); var length = polygon.getTotalLength(); polygon.style.strokeDasharray = length; polygon.style.strokeDashoffset = length; window.addEventListener("scroll", drawPoly); function drawPoly() { var scrollpercent = (document.body.scrollTop + document.documentElement.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight); var draw = length * scrollpercent; polygon.style.strokeDashoffset = length - draw; } </script> </body> </html>
Output
The above code will produce the following output −
On scrolling to the bottom of the page −
- Related Articles
- How to draw grid using HTML5 and canvas or SVG?
- How to draw shapes using SVG in HTML5?
- How to draw an SVG file on an HTML5 canvas?
- How to create SVG graphics using JavaScript?
- How to draw SVG Logo in HTML5?
- How to shrink a header on scroll with CSS and JavaScript?
- How to draw sine waves with HTML5 SVG?
- How to draw a rectangle in HTML5 SVG?
- How to draw a polygon in HTML5 SVG?
- How to draw a polyline in HTML5 SVG?
- How to draw a star in HTML5 SVG?
- How to draw a hollow circle in SVG?
- How to resize a navigation bar on scroll with CSS and JavaScript?
- How to hide a navigation menu on scroll down with CSS and JavaScript?
- How to create a fixed/sticky header on scroll with CSS and JavaScript?

Advertisements