question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaling a dynamically added svg is not aligned with the background!

See original GitHub issue

Hello, Currently, I am working on an application that draws freehand overlay, saves the overlay to a file. Then, reload the saved overlay from the file to the webpage. I managed to draw the first overlay and save the ‘svg’ element to my machine as a text file. However, when reopening the saved overlay again after clearOverlays() the saved overlay does not align with the background image unless the image is in the original zooming size. The problem I am facing is that when zooming in/out the image, the overlay keeps its original size while the image behind is resizing correctly. Bellow is code segments of the process, any help or suggestion is appreciated. Two screenshots of the process are attached, the 1st is after drawing the overlay, while the 2nd is after saving and reloading the overlay.

show the image and start drawing the overlay

this.viewer = OpenSeadragon({
            id: "seadragon-viewer",
            prefixUrl: "//openseadragon.github.io/openseadragon/images/",
            tileSources: [{
                type : 'image',
                url: 'back_image.jpg'
                 }]
            });
            viewer.initializeAnnotations();

Saving the drown overlay:

<button type="button" onclick="save_mask()">Save mask</button>
            <script>
                function save_mask() {
                    var svgElement = document.getElementsByTagName('svg');
                    // let height =  svgElement[0].style.height;
                    // let width =  svgElement[0].style.width;
                    let clonedSvgElement = svgElement[0].cloneNode(true);

                    function download(filename, text) {
                        var element = document.createElement('a');
                        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
                        element.setAttribute('download', filename);
                        element.style.display = 'none';
                        document.body.appendChild(element);
                        element.click();
                        document.body.removeChild(element);
                        }
                        download('mask.svg', clonedSvgElement.outerHTML);
                }
            </script>

Here, when I get the fixed size of the loaded overlay:

<script type="text/javascript">
                function readSingleFile(e) {
                var file = e.target.files[0];
                if (!file) {
                    return;
                }
                var reader = new FileReader();
                        reader.onload = function(e) {
                            var contents = e.target.result;
                            displayContents(contents);
                        };
                    reader.readAsText(file);
                }
                function castString2HTML(str) {
                    let temp = document.createElement('div');
                    temp.innerHTML = str;
                    let htmlObject = temp.firstChild;
                    return htmlObject;
                }
                function displayContents(contents) {
                    var element = document.getElementById('file-content');
                    new_svg = castString2HTML(contents);
                    viewer.clearOverlays();
                    viewer.addOverlay(new_svg);
                }
                document.getElementById('file-input')
                .addEventListener('change', readSingleFile, false);
            </script>

image image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shyamkumaryadavcommented, Feb 2, 2022

So, You only need the SVG path so next time you load the image, want to load the SVG path too?

You are using addOverlay & clearOverlays which is not related to openseadragon-annotations.

const annotations = new OpenSeadragon.Annotations({ viewer });

using annotations you can use getAnnotations() to get path data and setAnnotations(data) to set annotation data on image load

0reactions
iangilmancommented, Mar 2, 2022

@magednasan Excellent! You might consider making a pull request with the changes to that project… Even if the owner isn’t maintaining it, other people might value the code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I make my div's svg background image scale ...
Scale the background image to be as large as possible so that the background area is completely covered by the background image.
Read more >
Scaling of SVG backgrounds - CSS: Cascading Style Sheets
This article describes how scaling of SVG images is handled when using these ... background: url(100px-wide-no-height-or-ratio.svg); ...
Read more >
How to Scale SVG | CSS-Tricks
Here, she shares an epic guide to scaling SVG, covering all the ways you might want to do that. It's not nearly as...
Read more >
Resizing an SVG When the Window is Resized in d3.js - Chartio
When developing with SVG, it can often be difficult to scale SVG objects when the containing frame or even the entire browser window...
Read more >
Dynamically Changing preserveAspectRatio — Using SVG ...
For the x- and y-alignment options, the value attribute for each <option> is the exact keyword that will be used in the final...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found